`
弄月吟风
  • 浏览: 197149 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android视图的截图

阅读更多

在pc上的截图软件很多,但是android上的比较少,所以就自己写了一个,下面时截图软件的核心代码private Bitmap getViewBitmap(View v) { // 将一个View转化成一张图片

		v.clearFocus(); // 清除视图焦点
		v.setPressed(false);// 将视图设为不可点击

		boolean willNotCache = v.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存
		v.setWillNotCacheDrawing(false);

		// Reset the drawing cache background color to fully transparent
		// for the duration of this operation //将视图在此操作时置为透明
		int color = v.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色
		v.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色
		if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存
			v.destroyDrawingCache(); // 释放绘图资源所使用的缓存
		}
		v.buildDrawingCache(); // 重新创建绘图缓存,此时的背景色是黑色
		Bitmap cacheBitmap = v.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用
		if (cacheBitmap == null) {
			return null;
		}
		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化
		// Restore the view //恢复视图
		v.destroyDrawingCache();// 释放位图内存
		v.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置
		v.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置
		return bitmap;
	}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics