将View转换成Bitmap的方法,可直接调用

来源:互联网 发布:git 安装使用centos 编辑:程序博客网 时间:2024/06/11 00:29
<pre name="code" class="java">private Bitmap getViewBitmap(View v) {    Bitmap bitmap = null;    if (v != null) {        v.clearFocus();        v.setPressed(false);        boolean willNotCache = v.willNotCacheDrawing();        v.setWillNotCacheDrawing(false);        int color = v.getDrawingCacheBackgroundColor();        v.setDrawingCacheBackgroundColor(0);        float alpha = v.getAlpha();        v.setAlpha(1.0f);        if (color != 0) {            v.destroyDrawingCache();        }        v.buildDrawingCache();        Bitmap cacheBitmap = v.getDrawingCache();        if (cacheBitmap == null) {            return null;        }        bitmap = Bitmap.createBitmap(cacheBitmap);        // Restore the view        v.destroyDrawingCache();        v.setAlpha(alpha);        v.setWillNotCacheDrawing(willNotCache);        v.setDrawingCacheBackgroundColor(color);    }    return bitmap;}



0 0
原创粉丝点击