• 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卡,需添加写外置存储的权限。  

  • 相关阅读:
    MyEclipse快捷键大全
    The type 'Microsoft.Office.Interop.Excel.ApplicationClass' has no constructors defined
    ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’
    使用游标循环表的简单DEMO
    URL Records
    一步一步教你实现CTreeCtrl 自绘
    自绘按钮
    UDP文件传输的实现
    美化VC界面(用户登录界面)
    如何截取QQ密码和聊天内容、去掉QQ广告栏、添加QQ尾巴
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/6702630.html
Copyright © 2020-2023  润新知