android 把view转成bitmap~~~

来源:互联网 发布:web工程师和java哪个好 编辑:程序博客网 时间:2024/05/16 10:01
Java代码  收藏代码
  1. private Bitmap getViewBitmap(View v) {  
  2.         v.clearFocus();  
  3.         v.setPressed(false);  
  4.   
  5.         boolean willNotCache = v.willNotCacheDrawing();  
  6.         v.setWillNotCacheDrawing(false);  
  7.   
  8.         // Reset the drawing cache background color to fully transparent  
  9.         // for the duration of this operation  
  10.         int color = v.getDrawingCacheBackgroundColor();  
  11.         v.setDrawingCacheBackgroundColor(0);  
  12.   
  13.         if (color != 0) {  
  14.             v.destroyDrawingCache();  
  15.         }  
  16.         v.buildDrawingCache();  
  17.         Bitmap cacheBitmap = v.getDrawingCache();  
  18.         if (cacheBitmap == null) {  
  19.             Log.e("TTTTTTTTActivity""failed getViewBitmap(" + v + ")"new RuntimeException());  
  20.             return null;  
  21.         }  
  22.   
  23.         Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  
  24.   
  25.         // Restore the view  
  26.         v.destroyDrawingCache();  
  27.         v.setWillNotCacheDrawing(willNotCache);  
  28.         v.setDrawingCacheBackgroundColor(color);  
  29.   
  30.         return bitmap;  
  31.     } 

原创粉丝点击