如何拍摄Android的Surface View的屏幕截图? [英] How to take a screenshot of Android's Surface View?

查看:69
本文介绍了如何拍摄Android的Surface View的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式拍摄我的游戏的屏幕快照,就像在Eclipse DDMS中一样.

I want to programatically take screen shot of my game, just as you'd get in Eclipse DDMS.

通过此处提出的解决方案拍摄的屏幕截图:如何以编程方式获取和Android中的大多数其他问题中,只有可见的View元素可见,而SurfaceView则不可见.

Screenshot taken through the solution proposed here: How to programmatically take a screenshot in Android? and in most other SO questions only have View elements visible, but not the SurfaceView.

SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
// Get root view
View view = activity.getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);

// Get current theme to know which background to use
final Theme theme = activity.getTheme();
final TypedArray ta = theme
    .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = activity.getResources().getDrawable(res);

// Draw background
background.draw(canvas);

// Draw views
view.draw(canvas);

// Save the screenshot to the file system
FileOutputStream fos = null;
try {
    final File sddir = new File(SCREENSHOTS_LOCATIONS);
    if (!sddir.exists()) {
        sddir.mkdirs();
    }
    fos = new FileOutputStream(SCREENSHOTS_LOCATIONS
            + System.currentTimeMillis() + ".jpg");
    if (fos != null) {
        if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
            Log.d("ScreenShot", "Compress/Write failed");
        }
        fos.flush();
        fos.close();
    }

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

在这方面的任何帮助将不胜感激.谢谢.

Any help in this regard would be highly appreciated. Thanks.

推荐答案

在大多数情况下,Usman Kurd答案中的代码将不起作用.

The code from the Usman Kurd answer will not work is most cases.

除非您使用Canvas将软件中的H.264视频帧渲染到View上,否则绘图缓存方法将不起作用(请参见此答案).

Unless you're rendering H.264 video frames in software with Canvas onto a View, the drawing-cache approach won't work (see e.g. this answer).

您无法从SurfaceView的Surface部分读取像素.基本问题是,Surface是带有生产者-消费者接口的缓冲区队列,而您的应用程序位于生产者端.使用者(通常是系统合成器(SurfaceFlinger))能够捕获屏幕快照,因为它在管道的另一端.

You cannot read pixels from the Surface part of the SurfaceView. The basic problem is that a Surface is a queue of buffers with a producer-consumer interface, and your app is on the producer side. The consumer, usually the system compositor (SurfaceFlinger), is able to capture a screen shot because it's on the other end of the pipe.

拍摄SurfaceView的屏幕截图

这篇关于如何拍摄Android的Surface View的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆