項目中用到了許多截圖分享到第三方的地方,遇到了很多坑,類似:截圖失敗、獲取不到緩存圖片、截取RecyclerView長圖中ImageView同步加載、長圖獲取緩存出現OOM等,本次就遇到的所有問題進行匯總,并提交Demo到github。
簡單的截圖
從網上搜索到的結果,大部分都是 setDrawingCacheEnable(true) ; 然后從緩存中獲取到數據 view.getDrawingCache(), 代碼如下:
Bitmap cacheBitmap = null;
cacheView.setEnabled(false);
// 首先獲取一個View,
View view = getUiView().mActivity.getWindow().getDecorView();
// 設置可以從緩存中獲取到數據
view.setDrawingCacheEnabled(true);
// 獲取到bitmap
cacheBitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
但是官網中對該使用方法標識已經廢棄了:
隨著API 11中硬件加速渲染的引入,視圖繪制緩存基本上已過時。通過硬件加速,中間緩存層在很大程度上是不必要的,并且很容易導致性能的凈損失由于創建和更新圖層的成本;所以官網建議我們使用PixelCopy,不過PixelCopy需要傳入SurfaceView或者Window,我們可以看看setDrawingCacheEnabled(true) 之后獲取getDrawingCache()的源碼:
/**
* private, internal implementation of buildDrawingCache, used to enable tracing
*/
private void buildDrawingCacheImpl(boolean autoScale) {
// ...... 省略部分代碼
// 以下判斷當前創建的Bitmap大小是否比系統最大值還大,如果超過則拋出異常
final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
final long drawingCacheSize =
ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
if (width > 0 && height > 0) {
Log.w(VIEW_LOG_TAG, getClass().getSimpleName() + " not displayed because it is"
+ " too large to fit into a software layer (or drawing cache), needs "
+ projectedBitmapSize + " bytes, only "
+ drawingCacheSize + " available");
}
destroyDrawingCache();
mCachingFailed = true;
return;
}
// ...... 省略部分代碼
// 清除內存數據
if (bitmap != null) bitmap.recycle();
try {
// 創建bitmap
bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
width, height, quality);
bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
} catch(OutOfMemoryError e) {
// ...... 省略部分代碼
}
}
// 創建Canvas
Canvas canvas;
if (attachInfo != null) {
canvas = attachInfo.mCanvas;
if (canvas == null) {
canvas = new Canvas();
}
// 將bitmap設置到畫布中
canvas.setBitmap(bitmap);
// Temporarily clobber the cached Canvas in case one of our children
// is also using a drawing cache. Without this, the children would
// steal the canvas by attaching their own bitmap to it and bad, bad
// thing would happen (invisible views, corrupted drawings, etc.)
attachInfo.mCanvas = null;
} else {
// This case should hopefully never or seldom happen
canvas = new Canvas(bitmap);
}
// ...... 省略部分代碼
// 畫數據
draw(canvas);
}
通過以上代碼,所以在stackflow中有人總結出:直接通過View創建一個Bitmap,然后設置給Canvas,再通過View的draw方法傳入Canvas:
public void setBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
}
當然,上面的兩種方法,都是基于View已經被測量出來,顯示在界面上,只需要拿到數據即可,大多數情況下,我們需要使用LayoutInflater來獲取一個View,這時候我們是獲取不到View的寬高的,所以需要我們手動去measure和layout;
從layout中加載的View截圖
從View的加載過程我們得知,View要繪制到界面上,需要經歷onMeasure()、onLayout()、onDraw(),因為從layout中加載的View,沒有執行這幾個過程,所以無法得到View的緩存圖,必須得要我們手動去執行這幾個方法:
public void getViewFromLayout() {
// 獲取到View
View view = LayoutInflater.from(getUiView().mActivity).inflate(R.layout.view_layout, null, false);
// 調用measure() 方法,測量View的寬高
view.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(DensityUtil.getHeight(), View.MeasureSpec.EXACTLY));
// 調用layout() 方法,確定View及其子View的位置
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
// 繪制出View
view.draw(canvas);
}
上面的代碼比第一種平常的截圖就多了兩個方法,measure和layout,measure方法需要傳入View的寬高,一般設置一個確定的值即可。
RecyclerView截圖
網上有很多例子和教程來教大家怎么截取RecyclerView的圖片的,但是現在基本上RecyclerView都是有圖片的,所以涉及到異步去加載圖片;然而,當使用Stack Overflow中代碼時,截取到的獲取還只是你設置到圖片上的加載中的圖片,我找了許久,才知道應該用同步的加載圖片方法,當所有的圖片加載完之后獲取到截圖;
當然,這些同步加載圖片的方法,因為是耗時線程,需要將代碼放到異步線程中,然后處理完之后切換到同步線程中,我這里使用了Rxjava2中的defer方法:
// 分享 截圖
Observable
.defer(new Callable<ObservableSource<Bitmap>>() {
@Override
public ObservableSource<Bitmap> call() throws Exception {
return new ObservableSource<Bitmap>() {
@Override
public void subscribe(Observer<? super Bitmap> observer) {
// MVP模式,從p層獲取到RecyclerView的截圖Bitmap數據
observer.onNext(mPresenter.getRecyclerViewScreenSpot(mRecyclerView));
}
};
}
})
.compose(applyAsySchedulers())
.as(bindLifecycle())
.subscribe(new Consumer<Bitmap>() {
@Override
public void accept(Bitmap bitmap) throws Exception {
// 拿到結果之后,處理成自己想要的圖片,
mPresenter.getShareSpecimenFile(shareSpecimenBean, bitmap);
}
});
defer方法傳入了一個Callable,當前執行的代碼在異步線程中,compse方法注冊在異步中,訂閱在主線程中,所以就起到了切換線程的作用;拿到Bitmap之后,因為我這里需要將Bitmap嵌套到另外一個View,分享另外一個從LayoutInflater中加載的View,所以得在主線程中執行:
getRecyclerViewScrrentSpot(mRecyclerView):
@Override
public Bitmap getRecyclerViewScreenSpot(RecyclerView recyclerView) {
//獲取設置的adapter
LoadMoreWrapper adapter = (LoadMoreWrapper) recyclerView.getAdapter();
//創建保存截圖的bitmap
Bitmap bigBitmap = null;
if (adapter != null) {
//獲取item的數量
int size = adapter.getItemCount();
//recycler的完整高度 用于創建bitmap時使用
int height = 0;
//獲取最大可用內存
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// 使用1/8的緩存
final int cacheSize = maxMemory / 8;
//把每個item的繪圖緩存存儲在LruCache中
LruCache<String, Bitmap> bitmapCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
//手動調用創建和綁定ViewHolder方法,
RecyclerView.ViewHolder holder =
adapter.createViewHolder(recyclerView, adapter.getItemViewType(i));
adapter.onBindViewImageSync(holder, i);
//測量
holder.itemView.measure(
View.MeasureSpec.makeMeasureSpec(recyclerView.getWidth() / 3,
View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//布局
holder.itemView.layout(0, 0,
holder.itemView.getMeasuredWidth(),
holder.itemView.getMeasuredHeight());
//開啟繪圖緩存
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmapCache.put(String.valueOf(i), drawingCache);
}
//獲取itemView的實際高度并累加
if ((i + 1) % 3 == 0) {
height += holder.itemView.getMeasuredHeight() +
DensityUtil.dp2px(holder.itemView.getContext(), 20);
}
if ((i + 1) % 3 != 0 && (i + 1) == size) {
height += holder.itemView.getMeasuredHeight() +
DensityUtil.dp2px(holder.itemView.getContext(), 20);
}
}
//根據計算出的recyclerView高度創建bitmap
bigBitmap = Bitmap.createBitmap(recyclerView.getMeasuredWidth(),
height, Bitmap.Config.ARGB_8888);
//創建一個canvas畫板
Canvas canvas = new Canvas(bigBitmap);
//當前bitmap的高度
int top = 0;
int left = 0;
//畫筆
Paint paint = new Paint();
for (int i = 0; i < size; i++) {
Bitmap bitmap = bitmapCache.get(String.valueOf(i));
canvas.drawBitmap(bitmap, left, top, paint);
if ((i + 1) % 3 == 0) {
left = 0;
top += bitmap.getHeight() + DensityUtil.dp2px(getUiView(), 20);
} else {
left += bitmapCache.get(String.valueOf(i)).getWidth();
}
}
}
return bigBitmap;
}
上面的代碼是模板代碼,網上搜基本上都是這樣的寫法;目的是為了解決OOM的問題,所以這里模擬了Adapter加載數據的情況,讓它在后臺執行加載數據,需要注意的是,在measure和layout的時候,需要根據你布局的實際情況來測量和布局,我這里的是GridLayoutManager, 所以每次測量的時候都是一行三列,實際邏輯還是得跟你的界面來;
在獲取到RecyclerView的holder之后,調用了adapter.onBindViewImageSync(holder, i);這是我自定義的一個同步獲取方法,目的是同步加載ImageView的圖片,其他的數據都是和onBindView()重載的方法一樣,我這里使用的是Glide加載圖片,4.x的標本和3.x標本不同,基本上使用還是差不多的:
try {
File file = Glide.with(this)
.load(sampleListBean.getImgUrl())
.downloadOnly(100, 100)
.get(5, TimeUnit.SECONDS);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ivPlant.setImageBitmap(bitmap);
L.d("圖片加載成功! ==== " + position + " " + bitmap.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
當然,這里有耗時操作,在截圖的時候,需要彈出提示框加載;
截圖完之后,因為項目需求美觀,分享出去的圖片并不是一張赤裸裸的長圖,需要制作一張精美的圖片,但是當RecyclerView得數據很多時,我使用了getDrawingCache() 時,獲取到的數據為空,報錯信息:
Log.w(VIEW_LOG_TAG, getClass().getSimpleName() + " not displayed because it is"
+ " too large to fit into a software layer (or drawing cache), needs "
+ projectedBitmapSize + " bytes, only "
+ drawingCacheSize + " available");
這是getDrawingCache()方法中判斷當前創建緩存圖片大小和系統最大圖片大小,超過最大時報錯,所以,我修改成Canvas之后便可以了;隨后將制作的圖片保存到本地:
File file = FileUtils.getInstance().getOwnCacheDirectory(getUiView(), Constants.PLANT_CACHE_DIR);
File takePhotoFile = new File(file, "specimenShare.png");
FileOutputStream fout = null;
try {
fout = new FileOutputStream(takePhotoFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
通過保存后的文件,便可以分享到QQ、微信了!
總結
- getDrawingCache() 方法已經過時,谷歌推薦使用PixelCopy中的API使用,目前我沒有試過,有使用過的朋友方便提交代碼?
- 可以使用Canvas來解決OOM的問題
- 異步線程中同步加載圖片方法
- RecyclerView截圖需要判斷當前LayoutManager