View转换成Bitmap

来源:互联网 发布:淘宝服务.com 编辑:程序博客网 时间:2024/05/19 19:41
 public static Bitmap getViewBitmap(View v) {        if (v == null) {            return null;        }        try {            v.clearFocus(); //            v.setPressed(false); //            // 能画缓存就返回false            boolean willNotCache = v.willNotCacheDrawing();            v.setWillNotCacheDrawing(false);            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 = null;            try {                bitmap = cacheBitmap.copy(Config.RGB_565, true);            } catch (Exception e) {            }            // Restore the view            v.destroyDrawingCache();            v.setWillNotCacheDrawing(willNotCache);            v.setDrawingCacheBackgroundColor(color);            return bitmap;        } catch (OutOfMemoryError ex) {        }        return null;    }

1 0
原创粉丝点击