• 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();
            }
        }
    }
  • 相关阅读:
    中国历史年代史的一些总结-缕清上下五千年
    struts2 自己定义表单
    "What's New" WebPart in SharePoint
    Tomcat+Servlet登录页面实例
    JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦
    Android开发 -- Bootloader
    NorFlash、NandFlash、eMMC比较区别【转】
    strspn() 和 strcspn() 函数【转】
    Device Tree(一):背景介绍【转】
    设备树使用手册【转】
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910122.html
Copyright © 2020-2023  润新知