我正在尝试在 Android 中的代码中截取屏幕截图。实际上,屏幕截图是主RelativeLayout 的位图。屏幕截图已拍摄,但内容显示错误,不遵守 fill_parent 等标签,并且图像显示在左上角并具有原始大小。
请任何人帮忙。
Thanks
这是我的代码:
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