我想采取code在Android的屏幕截图。其实截图是主要RelativeLayout的位图。该截图是采取但内容出现错误,则FILL_PARENT等标记不尊重,图像显示在左上角,并与原来的大小。
I'm trying to take a screenshot in code in Android. Actually the screenshot is the bitmap of the main RelativeLayout. The screenshot is taken but the content appears wrong, the fill_parent, etc tags are not respected and the image appears in the top left and with the original size.
请任何人的帮助。
感谢
这是我的code:
RelativeLayout v = (RelativeLayout)findViewById(R.id.main_layout);
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, v.getMeasuredWidth(), v.getMeasuredHeight());
v.layout(0, 0, v.getWidth(), v.getHeight());
v.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); //
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
File file = new File(path, "screentest.jpg");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
Log.e("ImagePath", "Image Path : "
+ MediaStore.Images.Media.insertImage(
getContentResolver(), file.getAbsolutePath(),
file.getName(), file.getName()));
} catch (Exception e) {
e.printStackTrace();
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/backImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<com.calendarView.CalendarView
android:orientation="vertical"
android:id="@+id/calendar_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
@ ns476
@ns476
在 makeMeasureSpec指定屏幕尺寸
...
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
v.measure(MeasureSpec.makeMeasureSpec(0, size.x),
MeasureSpec.makeMeasureSpec(0, size.y));
...
也可以尝试设置 setDrawingCacheQuality 以高,所以它返回一个更高分辨率的位图。
Also try setting the setDrawingCacheQuality to high, so that it returns a higher resolution bitmap.