关于图片生成我们并不陌生,有些时候,图片能代替文字,更好的表达自己想说的,比如那些动态斗图,长图之类的。如果你不喜欢斗图,对不起,我.......
在写这个项目的时候也搜过一些资料,什么截屏啊,通过列表控件,比如RecyclerView,glidView等之类的,通过获取到每一项的itemView.....不说了,区别有些大,每个组件获取itemView方法都不一样。
不要和我说什么listview,还是RecyclerView组件分析,
老夫生成图片就是一个字,
画!!!
分析如何获取组件item?不存在的,
项目开始到结束,出现item分析
算我输
我整了个小项目出来,下面是项目地址
项目地址:https://github.com/niyige/LongImgCreateProject
项目apk:https://github.com/niyige/LongImgCreateProject/tree/master/apk
该项目实现了,生成长图,保存图片到相册,调用系统分享相关功能,里面涉及到android版本的注意事项,那就先说注意事项吧。
事项一:android6.0对于权限方面有一些改动,在清单文件里面注册了相关权限,还需要app启动的时候动态授权(项目里面有相关处理方案)。
事项二:android7.0对于文件共享方面做出了一些改动,官方解释:Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则会报FileUriExposedException 异常。
事项二解决方案:要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。进行此授权的最简单方式是使用 FileProvider 类。如需了解有关权限和共享文件的详细信息,请参阅共享文件。
https://developer.android.com/about/versions/nougat/android-7.0-changes.html#accessibility
下面是生成长图的重要概念:
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap btm = view.getDrawingCache()
想深究上面的实现:点这里,https://www.jianshu.com/p/09e32f10b394
如果想要获取某个view的视图(bimap),android提供了上面的方法来实现,这意味着,你可以获取到任意一个布局的bitmap。
本项目就是通过构建长图所需的所有组件,然后计算总height,根据总height,创建一个bitmap(longBitmap),然后通过canvas画上去,最后保存一张图。
下面是把所有组件的bitmap画到longBitmap的实现代码:
public static Bitmap createBigBitmap(Activity activity, View view, List<String> mList) {
int allItemsHeight = 0;
int itemWidth = view.getWidth();
List<Bitmap> bitmaps = new ArrayList<>();
//如果有头部就写上
View headerView = activity.getLayoutInflater().inflate(R.layout.header_layout, null);
headerView.measure(View.MeasureSpec.makeMeasureSpec(itemWidth, View.MeasureSpec.EXACTLY), 0);
headerView.layout(0, 0, itemWidth, headerView.getMeasuredHeight());
headerView.setDrawingCacheEnabled(true);
headerView.buildDrawingCache();
bitmaps.add(headerView.getDrawingCache());
allItemsHeight += headerView.getMeasuredHeight();
for (int i = 0; i < mList.size(); i++) {
View childView = LayoutInflater.from(activity).inflate(R.layout.content_layout, null);
childView.measure(View.MeasureSpec.makeMeasureSpec(itemWidth, View.MeasureSpec.EXACTLY), 0);
childView.layout(0, 0, itemWidth, childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bitmaps.add(childView.getDrawingCache());
allItemsHeight += childView.getMeasuredHeight();
}
//如果有尾部就写上
View footerView = activity.getLayoutInflater().inflate(R.layout.footer_layout, null);
footerView.measure(View.MeasureSpec.makeMeasureSpec(itemWidth, View.MeasureSpec.EXACTLY), 0);
footerView.layout(0, 0, itemWidth, footerView.getMeasuredHeight());
footerView.setDrawingCacheEnabled(true);
footerView.buildDrawingCache();
bitmaps.add(footerView.getDrawingCache());
allItemsHeight += footerView.getMeasuredHeight();
Bitmap bigBitmap = Bitmap.createBitmap(itemWidth, allItemsHeight, Bitmap.Config.ARGB_4444);
Canvas bigCanvas = new Canvas(bigBitmap);
bigCanvas.drawColor(ContextCompat.getColor(activity, R.color.bg_f2f2f2));
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int iHeight = 0;
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bmp = bitmaps.get(i);
bigCanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
bmp.recycle();
}
return bigBitmap;
}
生成长图的bitmap后,保存到文件里面。在上面的实现中,直接获取需要显示在长图的布局,直接inflate的布局,然后设置数据(如果是直接inflate的布局,记得要 measure,layout,否则获取到的height是0)。这里的实现,跟那些列表类的控件没有半毛钱关系,想画的布局,直接填充。
下面是项目展示:
首页:
生成后保存到相册并弹分享框:
分享到QQ:
时隔多年,记录下,证明我曾经 经历过,这是我逝去的青春。