android截图 截取ContentView 截取指定的View并且保存

截取DecorView

 1 getWindow().getDecorView().setDrawingCacheEnabled(true);
 2         try {
 3             File myCaptureFile = new File("/mnt/sdcard/"
 4                     + System.currentTimeMillis() + ".jpg");
 5             BufferedOutputStream bos = new BufferedOutputStream(
 6                     new FileOutputStream(myCaptureFile));
 7             getWindow().getDecorView().getDrawingCache()
 8                     .compress(Bitmap.CompressFormat.JPEG, 80, bos);
 9             bos.flush();
10             bos.close();
11         } catch (Exception e) {
12             e.printStackTrace();
13         }
14         getWindow().getDecorView().setDrawingCacheEnabled(false);

截取指定的View

view.setDrawingCacheEnabled(true);
        try {
            File myCaptureFile = new File("/mnt/sdcard/"
                    + System.currentTimeMillis() + ".jpg");
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(myCaptureFile));
            view.getDrawingCache()
                    .compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        view().setDrawingCacheEnabled(false);

 截取不包括状态栏的部分

 1 /**
 2      * 页面返回动画
 3      */
 4     private void gobackAnimation(){
 5         getWindow().getDecorView().setDrawingCacheEnabled(true);
 6         Bitmap bm = getWindow().getDecorView().getDrawingCache();
 7         Rect rect = new Rect();
 8         getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
 9         final Bitmap bitmap = Bitmap.createBitmap(bm, 0, rect.top, Device.getScreenWidth(), Device.getScreenHeight() - rect.top);;
10         bm.recycle();
11         final ImageView imageView = new ImageView(ActivityOnlineAndUser.this);
12         imageView.setScaleType(ScaleType.FIT_XY);
13         imageView.setImageBitmap(bitmap);
14         getWindow().addContentView(imageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
15         Animation anim = AnimationUtils.loadAnimation(ActivityOnlineAndUser.this, R.anim.push_right_out);
16         anim.setAnimationListener(new AnimationListener() {
17             
18             @Override
19             public void onAnimationStart(Animation animation) {
20                 
21             }
22             
23             @Override
24             public void onAnimationRepeat(Animation animation) {
25                 
26             }
27             
28             @Override
29             public void onAnimationEnd(Animation animation) {
30                 // 回收资源
31                 imageView.setImageBitmap(null);
32                 bitmap.recycle();
33                 ((ViewGroup)imageView.getParent()).removeView(imageView);
34                 // 设置成False,否则会很浪费性能
35                 getWindow().getDecorView().setDrawingCacheEnabled(false);
36             }
37         });
38         imageView.startAnimation(anim);
39     }
原文地址:https://www.cnblogs.com/xinye/p/3026803.html