zoukankan      html  css  js  c++  java
  • android截屏

    截屏是一个常用的操作,经常会有这种需求。

    截屏的工具类

    package com.fxb.screenshot;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.Matrix;
    import android.graphics.Rect;
    import android.util.DisplayMetrics;
    import android.view.View;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class ScreenShot {
    
        private static Bitmap getScreenBitmap(Activity activity){
            View root = activity.getWindow().getDecorView();
            root.setDrawingCacheEnabled(true);
            root.buildDrawingCache();
            Bitmap bitmapOri = root.getDrawingCache();
    
            Rect rect = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int statusBarHeight = rect.top;
    
    //        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    //        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
    
            Matrix matrix = new Matrix();
            matrix.setScale(0.5f, 0.5f);
            Bitmap bitmapSamll = bitmapOri.createBitmap(bitmapOri,
                                                        0, statusBarHeight, metric.widthPixels, metric.heightPixels - statusBarHeight,
                                                        matrix, true);
            root.destroyDrawingCache();
            return bitmapSamll;
        }
    
        private static void saveBitmap(Bitmap bitmap, String path){
            try {
                FileOutputStream fos = new FileOutputStream(path);
                if(fos != null){
                    bitmap.compress(CompressFormat.PNG, 90, fos);
                    fos.flush();
                    fos.close();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void shoot(Activity activity, String path){
            Bitmap bitmap = getScreenBitmap(activity);
            saveBitmap(bitmap, path);
        }
    
    }
    

    测试类

    package com.fxb.screenshot;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.io.File;
    
    public class MainActivity extends Activity implements View.OnClickListener {
    
        public static final String TAG = "ScreenShot";
    
        private TextView tvShow;
        private Button btnShot;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView(){
            tvShow = (TextView)findViewById(R.id.tvShow);
            btnShot = (Button)findViewById(R.id.btnShot);
    
            btnShot.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            if(v == btnShot){
                String rootPath = Environment.getExternalStorageDirectory() + "/pictest";
                File file = new File(rootPath);
                if(!file.exists()){
                    file.mkdir();
                }
    //            String path = Environment.getExternalStorageDirectory() + "/pictest/shot_" + System.currentTimeMillis() + ".png";
                String path = rootPath + "/shot_" + System.currentTimeMillis() + ".png";
                ScreenShot.shoot(this, path);
                Log.i(TAG, "shot over!");
            }
        }
    }
    

    截屏保存在外置sd卡,需添加写外置存储的权限。  

  • 相关阅读:
    每日一小练——数值自乘递归解
    linux的webserver配置与管理——创建用户个人主页
    微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!
    四月二十五日,bugzilla for CentOS 安装
    【剑指offer】顺时针打印矩阵
    何从之
    Java实现 蓝桥杯VIP 基础练习 时间转换
    Java实现 蓝桥杯VIP 基础练习 时间转换
    Java实现 蓝桥杯VIP 基础练习 字符串对比
    Java实现 蓝桥杯VIP 基础练习 字符串对比
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/6702630.html
Copyright © 2011-2022 走看看