• 4月30日学习日志


    今天学习了Bitmap(位图)全解析。

    主要实现代码为:

    public class MainActivity extends AppCompatActivity {
        static ByteArrayOutputStream byteOut = null;
        private Bitmap bitmap = null;
        private Button btn_cut;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn_cut = (Button) findViewById(R.id.btn_cut);
            btn_cut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    captureScreen();
                }
            });
        }
    
        public void captureScreen() {
            Runnable action = new Runnable() {
                @Override
                public void run() {
                    final View contentView = getWindow().getDecorView();
                    try{
                        Log.e("HEHE",contentView.getHeight()+":"+contentView.getWidth());
                        bitmap = Bitmap.createBitmap(contentView.getWidth(),
                                contentView.getHeight(), Bitmap.Config.ARGB_4444);
                        contentView.draw(new Canvas(bitmap));
                        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOut);
                        savePic(bitmap, "sdcard/short.png");
                    }catch (Exception e){e.printStackTrace();}
                    finally {
                        try{
                            if (null != byteOut)
                                byteOut.close();
                            if (null != bitmap && !bitmap.isRecycled()) {
    //                            bitmap.recycle();
                                bitmap = null;
                            }
                        }catch (IOException e){e.printStackTrace();}
    
                    }
                }
            };
            try {
                action.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void savePic(Bitmap b, String strFileName) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(strFileName);
                if (null != fos) {
                    boolean success= b.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                    if(success)
                        Toast.makeText(MainActivity.this, "截屏成功", Toast.LENGTH_SHORT).show();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Eclipse 远程调试
    大数据处理方法bloom filter
    sicily 1259 Sum of Consecutive Primes
    sicily 1240. Faulty Odometer
    sicily 1152 简单马周游 深度优先搜索及回溯算法
    sicily 1050 深度优先搜索解题
    sicily 1024 邻接矩阵与深度优先搜索解题
    sicily 1156 二叉树的遍历 前序遍历,递归,集合操作
    sicily 1443 队列基本操作
    sicily 1006 team rankings 枚举解题
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910122.html
Copyright © 2020-2023  润新知