Android截屏方法汇总

来源:互联网 发布:ubuntu修改ssh端口 编辑:程序博客网 时间:2024/06/04 18:45

1.  >=  ICE_CREAM_SANDWICH (14)

直接使用 "/system/bin/screencap -p "   需要root吗?


2.   >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH (14)            < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2  (18)

android.view.Surface  -> screenshot()


3.    >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 (18)

android.view.SurfaceControl   -> screenshot()


4. JNI 调用 surfaceflinger/ISurfaceComposer.h captureScreen() 获取 framebuffer

参考 http://blog.csdn.net/zmyde2010/article/details/6925498



-------------------------------------------------------------------------------------------------------------------------------------

截取当前页面的屏:

public class ScreenShot {
    // 获取指定Activity的截屏,保存到png文件
    private static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Log.i("TAG", "" + statusBarHeight);

        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

    // 保存到sdcard
    private static void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }





0 0
原创粉丝点击