zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    分形与数据结构第一篇(神奇的色子)
    画图小工具第二篇
    画图小工具第一篇
    图形界面第一篇
    回合制对战游戏第二篇
    回合对战制游戏第一篇(初识java)
    技术+态度+人品
    排序的一些方法(稳定性,内外排序,时间空间复杂度)
    暂时性死区
    vue传值(父子传值,非父子传值)
  • 原文地址:https://www.cnblogs.com/xinye/p/3026803.html
Copyright © 2011-2022 走看看