(4.4.3.1)将布局保存成图像和对activity截屏

来源:互联网 发布:淘宝买家信用积分规则 编辑:程序博客网 时间:2024/06/06 07:32

(1)view保存为png



[html] view plaincopy
  1. //将布局转换为View类型对象  
  2.         View view = getLayoutInflater().inflate(R.layout.main, null);  
  3.         //打开图像缓存  
  4.         view.setDrawingCacheEnabled(true);  
  5.         //必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件  
  6.         //测量View大小  
  7.         view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  8.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  9.         //发送位置和尺寸到View及其所有的子View  
  10.         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
  11.         try{  
  12.             //获得可视组件的截图  
  13.             Bitmap bitmap = view.getDrawingCache();  
  14.             //将截图保存在SD卡根目录的test.png图像文件中  
  15.             FileOutputStream fos = new FileOutputStream("/sdcard/test.png");  
  16.             //将Bitmap对象中的图像数据压缩成png格式的图像数据,并将这些数据保存在test.png文件中  
  17.             bitmap.compress(CompressFormat.PNG, 100, fos);  
  18.             //关闭文件输出流  
  19.             fos.close();  
  20.         }catch (Exception e) {  
  21.               
  22.         }  

(2)对acitvity截屏

  1. import java.io.FileNotFoundException;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.Rect;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10.   
  11. public class ScreenShot {  
  12.     // 获取指定Activity的截屏,保存到png文件  
  13.     private static Bitmap takeScreenShot(Activity activity) {  
  14.         // View是你需要截图的View  
  15.         View view = activity.getWindow().getDecorView();  
  16.         view.setDrawingCacheEnabled(true);  
  17.         view.buildDrawingCache();  
  18.         Bitmap b1 = view.getDrawingCache();  
  19.   
  20.         // 获取状态栏高度  
  21.         Rect frame = new Rect();  
  22.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  23.         int statusBarHeight = frame.top;  
  24.         Log.i("TAG""" + statusBarHeight);  
  25.   
  26.         // 获取屏幕长和高  
  27.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();  
  28.         int height = activity.getWindowManager().getDefaultDisplay()  
  29.                 .getHeight();  
  30.         // 去掉标题栏  
  31.         // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);  
  32.         Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  
  33.                 - statusBarHeight);  
  34.         view.destroyDrawingCache();  
  35.         return b;  
  36.     }  
  37.   
  38.     // 保存到sdcard  
  39.     private static void savePic(Bitmap b, String strFileName) {  
  40.         FileOutputStream fos = null;  
  41.         try {  
  42.             fos = new FileOutputStream(strFileName);  
  43.             if (null != fos) {  
  44.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);  
  45.                 fos.flush();  
  46.                 fos.close();  
  47.             }  
  48.         } catch (FileNotFoundException e) {  
  49.             e.printStackTrace();  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54.   
  55.     // 程序入口  
  56.     public static void shoot(Activity a) {  
  57.         ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");  
  58.     }  
  59. }  
  60.   
  61.    
  62.   
  63.    
  64.   
  65. 需要注意的是,shoot方法只能在view已经被加载后方可调用。  
  66.   
  67. 或者在    @Override  
  68.     public void onWindowFocusChanged(boolean hasFocus) {  
  69.         // TODO Auto-generated method stub  
  70.         super.onWindowFocusChanged(hasFocus);  
  71.         ScreenShot.shoot(this);  
  72.     }中调用  

[html] view plaincopy
  1. //将布局转换为View类型对象  
  2.         View view = getLayoutInflater().inflate(R.layout.main, null);  
  3.         //打开图像缓存  
  4.         view.setDrawingCacheEnabled(true);  
  5.         //必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件  
  6.         //测量View大小  
  7.         view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  8.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  9.         //发送位置和尺寸到View及其所有的子View  
  10.         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
  11.         try{  
  12.             //获得可视组件的截图  
  13.             Bitmap bitmap = view.getDrawingCache();  
  14.             //将截图保存在SD卡根目录的test.png图像文件中  
  15.             FileOutputStream fos = new FileOutputStream("/sdcard/test.png");  
  16.             //将Bitmap对象中的图像数据压缩成png格式的图像数据,并将这些数据保存在test.png文件中  
  17.             bitmap.compress(CompressFormat.PNG, 100, fos);  
  18.             //关闭文件输出流  
  19.             fos.close();  
  20.         }catch (Exception e) {  
  21.               
  22.         }  
0 0