簡介#
代碼是從github的:https://github.com/wuapnjie/StickerView fork 來
StikerView --- 貼紙 view (托拽购岗、縮放烹困、合成保存等功能)
![](https://github.com/wuapnjie/StickerView/raw/master/screenshots/stickerview.gif)
擴展介紹- android M 之后的權(quán)限代碼處理#
Google Play Services and Runtime Permissions
android 權(quán)限的處理方式:
在android M(6.0) 開始韭邓,權(quán)限的確認需要在用戶首次使用時確認(之前是安裝的時候確認)。那么相應(yīng)的代碼邏輯也要發(fā)生改變了----- 主要就是用戶同意權(quán)限怎么著辜御,不同意怎么著 if...else 的處理方式擒权。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
// permission has been granted, continue as usual
Location myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_LOCATION) {
if(grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// We can now safely use the API we requested access to
Location myLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
} else {
// Permission was denied or request was cancelled
}
}
}
代碼實現(xiàn)分析#
主activity就是 MainActivity extends AppCompatActivity 沒有什么特別,需要注意的就是上面提到的android M 之后的權(quán)限處理剖效。
實現(xiàn)其基本功能的就是 class StickerView extends ImageView
對外借口最重要的就是 addSticker 咒林,add的最終類是自定義的Sticker 里面都有Matrix
public void addSticker(Sticker sticker)
public void addSticker(Bitmap stickerBitmap)
public void addSticker(Bitmap stickerBitmap)
- 內(nèi)部操作就是由 onTouchEvent 推動的一系列 view 的變化在操作
- 主要的放大縮小垫竞,位移等操作都是由Matri來實現(xiàn)
- 保存則是由 BitmapUtil 類提供的兩個函數(shù)完成欢瞪。