来自代码的Android截图。得到它但不完美

时间:2011-05-19 08:44:46

标签: android android-layout screenshot relativelayout

我正试图在Android中的代码中截取屏幕截图。实际上,屏幕截图是主RelativeLayout的位图。屏幕截图已被拍摄,但内容显示错误,fill_parent等标签未得到遵守,图片显示在左上方并带有原始尺寸。

请任何人,帮助。

谢谢

这是我的代码:

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

5 个答案:

答案 0 :(得分:1)

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设置为高,以便返回更高分辨率的位图。

答案 1 :(得分:0)

我认为你需要更换

RelativeLayout v = (RelativeLayout)findViewById(R.id.main_layout);

v.setDrawingCacheEnabled(true);

v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

v.layout(0, 0, v.getWidth(), v.getHeight());

v.buildDrawingCache(true);

Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());

v.setDrawingCacheEnabled(false);

Bitmap bitmap;

View root = findViewById(android.R.id.content);

View v1 = root.getRootView();

v1.setDrawingCacheEnabled(true);

root.setDrawingCacheEnabled(true);

Bitmap bm = Bitmap.createBitmap(root.getDrawingCache());

root.setDrawingCacheEnabled(false);

答案 2 :(得分:0)

我假设你在创作过程中这样做。不要!

在绘制视图后使用postDelayed(Runnable,int)执行Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());

同样,删除v.measure()和v.layout() 这些是ViewGroup调用的方法。

答案 3 :(得分:0)

删除行:

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());

答案 4 :(得分:0)

LinearLayout ll = (LinearLayout )findViewById(R.id.layout); ll .setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache();

试试这个