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

posted @ 2017-04-13 10:14  丛林小阁楼  阅读(145)  评论(0编辑  收藏  举报