android 视图截屏

来源:互联网 发布:linux telnet 安装包 编辑:程序博客网 时间:2024/05/16 10:15
/**     * 把一个View的对象转换成bitmap     */    static Bitmap getViewBitmap(View v) {             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;}