由网友(此QQ已被封号)分享简介:我希望通过编程采取截图我的比赛,就像你会得到在Eclipse DDMS。I want to programatically take screen shot of my game, just as you'd get in Eclipse DDMS.截图通过该解决方案所采取这里提出:How以编程方式采取截图在And...
我希望通过编程采取截图我的比赛,就像你会得到在Eclipse DDMS。
I want to programatically take screen shot of my game, just as you'd get in Eclipse DDMS.
截图通过该解决方案所采取这里提出: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();
}
相关推荐
最新文章