android view create snapshot and savebitmap

  1. private  Bitmap createScreenshot(int width, int height) {  
  2.     // We render to a bitmap 2x the desired size so that we can then  
  3.     // re-scale it with filtering since canvas.scale doesn't filter  
  4.     // This helps reduce aliasing at the cost of being slightly blurry  
  5.     final int filter_scale = 2;  
  6.     Picture thumbnail = capturePicture();  
  7.     if (thumbnail == null) {  
  8.         return null;  
  9.     }  
  10.     //width = getWidth();  
  11.     //height = getHeight();  
  12.     width *= filter_scale;  
  13.     height *= filter_scale;  
  14.     Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  15.     Canvas canvas = new Canvas(bm);  
  16.     // May need to tweak these values to determine what is the  
  17.     // best scale factor  
  18.     int thumbnailWidth = thumbnail.getWidth();  
  19.     int thumbnailHeight = thumbnail.getHeight();  
  20.     float scaleFactor = 1.0f;  
  21.     if (thumbnailWidth > 0 && thumbnailHeight > 0) {  
  22.         scaleFactor = (float) width / (float)thumbnailWidth;  
  23.     } else {  
  24.         return null;  
  25.     }  
  26.   
  27.     float scaleFactorY = (float) height / (float)thumbnailHeight;  
  28.     if (scaleFactorY > scaleFactor) {  
  29.         // The picture is narrower than the requested AR  
  30.         // Center the thumnail and crop the sides  
  31.         scaleFactor = scaleFactorY;  
  32.         float wx = (thumbnailWidth * scaleFactor) - width;  
  33.         canvas.translate((int) -(wx / 2), 0);  
  34.     }  
  35.   
  36.     canvas.scale(scaleFactor, scaleFactor);  
  37.   
  38.     thumbnail.draw(canvas);  
  39.     Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,  
  40.             height / filter_scale, true);  
  41.     bm.recycle();  
  42.     return ret;  
  43. }  

 

 

  1.     public void saveBitmap(Bitmap bm, String filename){  
  2.         //String filename = "/mnt/sdcard/a.png";  
  3.         if (null == bm || bm.getWidth() == 0 || bm.getHeight() == 0){  
  4.             return;  
  5.         }      
  6.         File temp = new File(filename);  
  7.         FileOutputStream fout = null;  
  8.         try{  
  9.             temp.createNewFile();  
  10.             fout = new FileOutputStream(temp);  
  11.         }catch (IOException e){  
  12.             e.printStackTrace();  
  13.         }  
  14.         bm.compress(Bitmap.CompressFormat.PNG, 100, fout);  
  15.         try {  
  16.             fout.flush();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.         try {  
  21.             fout.close();  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         if (!bm.isRecycled()){  
  26.             bm.recycle();  
  27.         }  
  28.     }  

 

 

 

  1.      Bitmap getViewBitmap(View v){  
  2.         Bitmap drawingCache = Bitmap.createBitmap(v.getWidth(),  
  3.             v.getHeight(), Bitmap.Config.RGB_565);  
  4.         Canvas cvs = new Canvas();  
  5.         cvs.setBitmap(drawingCache);  
  6.         v.draw(cvs);  
  7.         return drawingCache;  
  8.     } 
posted @ 2012-04-05 21:59  cascais  阅读(675)  评论(0编辑  收藏  举报