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     }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值