Android实现保存长图、长布局效果源码

原创 yxy_No.1   2018-12-27 05:31  阅读 47 次
摘要:

Android实现将布局内容保存长图、支持长布局保存分享

/**
* view截图,webview和scrollview(scrollview需要传入子view)之类的view能够截取整个长度的bitmap,
* 如果webview内容很多,view.draw(Canvas)方法会很耗时,在子进程中操作会有额外的问题,所以会暂时阻塞
* UI主线程,求方法~
*/
public static Bitmap viewShot(final View view){
if (view == null)
return null;
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(measureSpec, measureSpec);

if (view.getMeasuredWidth()<=0 || view.getMeasuredHeight()<=0) { L.e("ImageUtils.viewShot size error"); return null; } Bitmap bm; try { bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); }catch (OutOfMemoryError e){ System.gc(); try { bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); }catch (OutOfMemoryError ee){ L.e("ImageUtils.viewShot error", ee); return null; } } Canvas bigCanvas = new Canvas(bm); Paint paint = new Paint(); int iHeight = bm.getHeight(); bigCanvas.drawBitmap(bm, 0, iHeight, paint); view.draw(bigCanvas); return bm; }

本文地址:https://www.dandroid.cn/?p=5858
关注我们:请关注一下我们的今日头条号:扫描二维码大安卓源码网的头条号,头条号:大安卓源码
版权声明:本文为原创文章,版权归 yxy_No.1  所有,欢迎分享本文,转载请保留出处!
开车速度很快,老司机专属

发表评论


表情