如何利用Android的表面观截图? [英] How to take a screenshot of Android's Surface View?

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

问题描述

我希望通过编程采取截图我的比赛,就像你会得到在Eclipse DDMS。

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

截图通过该解决方案所采取这里提出:<一href="http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android">How以编程方式采取截图在Android?,并在大多数其他等问题,只能查看内容可见,但不是SurfaceView。

Screenshot taken through the solution proposed here: How to programatically take a screenshot on 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();
}

任何帮助在这方面将是非常美联社preciated。谢谢你。

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

推荐答案

希望这个片段可以帮助你

Hope This Snippet can help you

    View v= getRootView();
v.setDrawingCacheEnabled(true);
// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());

v.buildDrawingCache(true);
Bitmap bitmap =v.getDrawingCache();
path=Environment.getExternalStorageDirectory() + "/test.png";
File file = new File(path);
try
{
    file.createNewFile();
    FileOutputStream ostream = new                     FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 100, ostream);
    ostream.close();
}
catch (Exception e)
{
    e.printStackTrace();
}

这篇关于如何利用Android的表面观截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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