一、需求
微信分享模板图片
二、做法
创建layout.xml并做布局调整,采用view转bitmap的形式分享该图片通用的做法:
view.setDrawingCacheEnabled(true)
Canvas canvas = new Canvas(bmp);
view.draw(canvas);
三、疑问
view是不可见的,是否生成的图片也是黑乎乎的一张呢? 如果不做处理结果就是黑乎乎!
好在我们还有measure、layout两个工具view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view
.layout(0, 0,
view.getMeasuredWidth(),
view.getMeasuredHeight());
测量和位置调整完后,就可以看到生成的结果了。
四、应用
ScrollView包裹下如何生成Bitmap?
ScrollView下生成Bitmap需要稍作处理:
① 对ScrollView进行measure测量,layout布局调整scrollView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
scrollView.layout(0,0,
scrollView.getMeasuredWidth(),
scrollView.getMeasuredHeight());
② layout内容填充
fillShareContent(scrollView , shareContentParams);
这一步必须提前完成,否则layout中的控件可能会有截断的问题
③ 重新对ScrollView进行measure测量,layout布局调整int measuredWidth = View.MeasureSpec.makeMeasureSpec(scrollView.getMeasuredWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(scrollView.getChildAt(0).getHeight(), View.MeasureSpec.AT_MOST);
scrollView.measure(measuredWidth, measuredHeight);
scrollView.layout(0, 0, scrollView.getMeasuredWidth(), scrollView.getMeasuredHeight());
④ 网络图片
处理网络图片
⑤ 生成Bitmap
五、问题
注意:TextView中不可以设置singleLine , 改用lines,否则会出现TextView内容截断的问题