Android获取View的内容图片
16lz
2021-01-26
1 获取当前可见的View的图片,类似于手机截屏View dView = getWindow().getDecorView();//获取DecorViewdView.setDrawingCacheEnabled(true);//生成View的副本,是一个BitmapBitmap bmp = dView.getDrawingCache();//获取View的副本,就是这个View上面所显示的任何内容2 获取任意View的内容图片
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);Canvas c = new Canvas(b);//使用bitmap构建一个Canvas,绘制的所有内容都是绘制在此Bitmap上的Drawable bgDrawable = v.getBackground();bgDrawable.draw(c);//绘制背景view.draw(c);//绘制前景return bitmap;如果是ListView,ScrollView,Webview等高度不固定的View,还需要手动计算出来高度来创建Bitmap,其他步骤相同
for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight();}// 创建对应大小的bitmapbitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.ARGB_8888);
示例代码
//组合viewprivate Bitmap combineView(View topView, WebView centerView, View bottomView) { float topWidth = topView.getWidth(); float topHeight = topView.getHeight(); float centerWidth = centerView.getWidth(); float centerHeight = centerView.getContentHeight() * centerView.getScale(); float bottomWidth = bottomView.getWidth(); float bottomHeight = bottomView.getHeight(); float width = Math.max(topWidth, Math.max(centerWidth, bottomWidth));//取最大的宽度作为最终宽度 float height = topHeight + centerHeight + bottomHeight;//计算总高度 Bitmap topBitmap = Bitmap.createBitmap((int) width, (int) topHeight, Bitmap.Config .ARGB_8888);//顶部图片 topView.draw(new Canvas(topBitmap)); Bitmap centerBitmap = Bitmap.createBitmap((int) width, (int) centerHeight, Bitmap.Config .ARGB_8888);//中部图片 centerView.draw(new Canvas(centerBitmap)); Bitmap bottomBitmap = Bitmap.createBitmap((int) width, (int) bottomHeight, Bitmap.Config .ARGB_8888);//底部图片 bottomView.draw(new Canvas(bottomBitmap)); Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);//底图 Canvas canvas = new Canvas(bitmap);//画布 Paint paint = new Paint(); canvas.drawBitmap(topBitmap, 0, 0, paint);//画顶部 canvas.drawBitmap(centerBitmap, 0, topHeight, paint);//画中间 canvas.drawBitmap(bottomBitmap, 0, topHeight + centerHeight, paint);//画底部 return bitmap;}
更多相关文章
- android选择一张本机图片
- Android获取SHA1指纹
- android 获取摄像头像素
- 系统相册分析(GallerPicker)
- android几种图像之间转换
- Android(安卓)程式开发:(七)处理变化 —— 7.3 检测屏幕的方向
- Android(安卓)OpenGL ES 开发教程(11):绘制三角形Triangle
- Android(安卓)图片处理 之 Bitmap
- Android改变图片背景颜色tint(着色)或 backgroundTint