zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    【刷题】洛谷 P4319 变化的道路
    【刷题】BZOJ 4573 [Zjoi2016]大森林
    CSS3_天猫商品墙
    CSS3_3D 变换
    CSS3_扇形导航_transitionend 事件
    CSS3_过渡_2D 变换_瓶体旋转_动态时钟
    CSS3_多列布局
    CSS3_线性渐变_径向渐变----背景
    CSS3_盒子背景
    CSS3_盒阴影_倒影_盒子大小可调
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910122.html
Copyright © 2011-2022 走看看