<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;
}
将View转换成Bitmap的方法,可直接调用
最新推荐文章于 2024-01-08 15:46:14 发布