扯扯淡
最近研究了几个便签app,主要是做富文本编辑。通过root的手机可以了解了锤子便签和魅族便签的存储方式,其中魅族便签采用的应该是recyclerView的多种布局方案,当然我也选用的这种方案,同步对便签的不断完善,这个过程可以涉及很多知识的深入处理,图片、音视频等等,全面知识的融合。
开始截图
在网上找了一些解决方案,实践了其中一种对每个item进行截图然后拼到一个大的bitmap中,但是这样会遇到单个item过长引起oom,系统为了避免这个问题的出现,直接在buildDrawingCacheImpl方法中进行了限制,这样获取的DrawingCache就为null
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;
}
然后只能采取滚动截图的方式进行截图,引用网上的图,原理如此
上代码(还需要review,重构)
@MainThread
public void screenShot(List<EditBaseCell> data, RecyclerView mRecyclerView) {
if (data == null || data.isEmpty() || mRecyclerView == null) {
return;
}
Paint paint = new Paint();
bitmapList = new ArrayList<>();
int measuredHeight = mRecyclerView.getMeasuredHeight();
WLog.e(TAG, "measureHeight:" + measuredHeight);
int shotHeight = 0;
//每个item计算高度时需要重新onBindViewHolder
for (EditBaseCell cell : data) {
shotHeight += cell.getHeight(mRecyclerView);
}
while (mRecyclerView.canScrollVertically(-1)) {
mRecyclerView.scrollBy(0, -measuredHeight);
}
//绘制截图的背景
Bitmap bigBitmap = Bitmap.createBitmap(mRecyclerView.getMeasuredWidth(), shotHeight, Bitmap.Config.RGB_565);
Canvas bigCanvas = new Canvas(bigBitmap);
Drawable lBackground = mRecyclerView.getBackground();
if (lBackground instanceof ColorDrawable) {
ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
int lColor = lColorDrawable.getColor();
bigCanvas.drawColor(lColor);
}
int drawOffset = 0;
while (mRecyclerView.canScrollVertically(1)) {
WLog.e(TAG, "drawOffset" + drawOffset);
//每次重新获取新的布局
mRecyclerView.setDrawingCacheEnabled(true);
// getDrawingCache()中已经调用
// mRecyclerView.buildDrawingCache();
Bitmap bitmap = mRecyclerView.getDrawingCache();
//调用这个方法会销毁当前的bitmap cache
// mRecyclerView.setDrawingCacheEnabled(false);
bigCanvas.drawBitmap(bitmap, 0, drawOffset, paint);
drawOffset += measuredHeight;
mRecyclerView.scrollBy(0, measuredHeight);
}
//不足一屏时的处理
int top = measuredHeight - (shotHeight - drawOffset);
WLog.e(TAG, "last" + top);
if (top > 0) {
mRecyclerView.setDrawingCacheEnabled(true);
Bitmap bitmap = mRecyclerView.getDrawingCache();
bigCanvas.drawBitmap(bitmap, new Rect(0, top, bitmap.getWidth(), bitmap.getHeight()),
new Rect(0, drawOffset, bigBitmap.getWidth(), bigBitmap.getHeight()), paint);
}
//恢复位置,可以先放置一张截图,或者是创建一个新的recyclerView来截图,同时可以截图时进行不同的处理
// while (mRecyclerView.canScrollVertically(-1)) {
// mRecyclerView.scrollBy(0, -measuredHeight);
// }
// mRecyclerView.scrollBy(0, scrollOffset);
// int i1 = scrollOffset / measuredHeight;
// for (int i = 0; i < i1; i++) {
// mRecyclerView.scrollBy(0, measuredHeight);
// }
// 保存图片,回调主线程
Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
ImageUtil.saveBitmap(DirsUtils.getDir(DirsUtils.PICS) + "screenShot.jpeg", bigBitmap);
e.onNext(true);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
ToastUtils.makeTextShort("screen shot ok");
mRecyclerView.destroyDrawingCache();
BitmapTools.recycleBitmap(bigBitmap);
// for (Bitmap bitmap:bitmapList){
// BitmapTools.recycleBitmap(bitmap);
// }
//
// bitmapList = null;
}
});
}
代码中只是粗略的实现了截图的功能,测试截图达到5M很长的图片依然不会出现crash的现象,在内存回收方面,虽然在内存不足时会回收,但是不能进行一个及时的回收,还需要继续跟进。
getDrawingCache获取的bitmao在自己处理回收时会发生RuntimeException,通过分析源码可以看到,系统是有自己的回收处理的,可以不再进行处理。
任务还在继续
- 截图时,隐藏或规避滚动,尝试新建一个recyclerView处理截图的数据并且方便给便签加上水印等处理,类似锤子便签中分享图片的处理
- 加入padding,margin的处理
- 提取更通用的截图工具类