截图的几种方法



Android获取屏幕截图主要有以下三种方法



通过view.getDrawingCache()获取指定View的绘制缓存来实现 截屏 。



这种方式Android 5.0之前也可以 ,且不需要权限。可以截取本应用内任意位置的屏幕截屏,可能会有些webview会截取不到。



注意这种方式只能获取当前应用内的截图(连顶部状态栏中的时间等都信息都获取不到,获取到的状态栏是一片空白)。






通过Linux底层驱动来实现截屏。



linux的图像信息都是通过 FrameBuffer 来写到显示设备上的,所以可以通过读取这个buffer的信息来获取屏幕截图。



DDMS工具就是通过这种方式来获取截图的。 FrameBuffer对应的设备文件目录是/dev/graphics/fb0。



但是这种方法需要root权限,由于是直接从底层显示数据读取的,所以理论上是属于手机屏幕的任何信息都可以截取到。






通过 Android 5.0后的 MediaProjection API 实现截屏。



该接口官方说是用来屏幕录制和音频录制,可以参照系统自带的sample案例ScreenCapture。



关于sample的分析可参照这篇   。



5.0后 系统自带的截图功能也是使用此API,过程分析详见   。



这种方法不用root,api是开放的,不过只针对L版以上。





Activity

public class MainActivity extends ListActivity {
    private TextView tv;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array = { "获取指定Activity整个屏幕,带状态栏", "获取指定Activity整个屏幕,不带状态栏",//
                "获取指定Activity指定View的区域屏幕", "获取指定Activity指定View的区域屏幕2", //
                "获取指定Activity指定区域屏幕", "使用Android 5.0后的MediaProjection API" };
        for (int i = 0; i < array.length; i++) {
            array[i] = i + "、" + array[i];
        }
        tv = new TextView(this);// 将内容显示在TextView中
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(params);
        tv.setTextColor(Color.BLUE);
        tv.setText("我是一个TextView");
        getListView().addFooterView(tv);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Bitmap bitmap = null;
        switch (position) {
        case 0:
            bitmap = SnapUtils.snapShot(this, true);
            SnapUtils.saveBitmap2Pic(bitmap, "bqt0.jpg");
            break;
        case 1:
            bitmap = SnapUtils.snapShot(this, false);
            SnapUtils.saveBitmap2Pic(bitmap, "bqt1.jpg");
            break;
        case 2:
            bitmap = SnapUtils.snapShot(this, v);
            SnapUtils.saveBitmap2Pic(bitmap, "bqt2.jpg");
            break;
        case 3:
            bitmap = SnapUtils.snapShot(this, tv);
            SnapUtils.saveBitmap2Pic(bitmap, "bqt3.jpg");
            break;
        case 4:
            bitmap = SnapUtils.snapShot(this, 300, 300, 100, 200);
            SnapUtils.saveBitmap2Pic(bitmap, "bqt4.jpg");
            break;
        case 5:
            startActivity(new Intent(this, Activity2.class));
            break;
        }
        if (bitmap != null) Toast.makeText(this, bitmap.getWidth() + "--" + bitmap.getHeight(), Toast.LENGTH_SHORT).show();
    }
}



工具类

public class SnapUtils {
    //******************************************************************************************
    //                                                                            屏幕截图
    //******************************************************************************************
    /**获取指定Activity整个屏幕截图,这种方式只能获取当前应用的截图(连顶部状态栏中的时间等都获取不到)
     * @param withStatusBar 是否包含状态栏
     */
    public static Bitmap snapShot(Activity activity, boolean withStatusBar) {
        int statusBarHeight = 0;
        if (!withStatusBar) {
            Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            statusBarHeight = frame.top;
        }
        DisplayMetrics metric = new DisplayMetrics();
        ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metric);
        return snapShot(activity, 0, statusBarHeight, metric.widthPixels, metric.heightPixels - statusBarHeight);
    }
    /**
     * 获取指定Activity指定View的屏幕截图,这种方式只能获取当前应用的截图(连顶部状态栏中的时间等都获取不到)
     * @param activity
     * @param view
     * @return
     */
    public static Bitmap snapShot(Activity activity, View view) {
        return snapShot(activity, (int) view.getX(), (int) view.getY(), (int) view.getWidth(), (int) view.getHeight());
    }
    /**
     * 获取指定Activity指定区域的屏幕截图,这种方式只能获取当前应用的截图(连顶部状态栏中的时间等都获取不到)
     * @param activity        所要截取的Activity
     * @param x        The x coordinate of the first pixel in source
     * @param y        The y coordinate of the first pixel in source
     * @param width        The number of pixels in each row
     * @param height        The number of rows
     * @return        A copy of a subset of the source bitmap or the source bitmap itself.
     */
    public static Bitmap snapShot(Activity activity, int x, int y, int width, int height) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap cache = view.getDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(cache, x, y, width, height);
        view.destroyDrawingCache();
        return bitmap;
    }
    /**
     * 保存bitmap为图片
     * @param bitmap
     * @param fileName    文件名,注意是保存在了SD卡根目录下
     */
    public static void saveBitmap2Pic(Bitmap bitmap, String fileName) {
        try {
            File file = new File(Environment.getExternalStorageDirectory().getPath(), fileName);
            file.createNewFile();//在window中没问题,但是在Android上必须加这一句,否然报异常 java.io.FileNotFoundException: xxx: open failed: EROFS (Read-only file system)
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}