来自RelativeLayout View的Android低质量位图包含MapView

时间:2012-04-20 21:53:47

标签: android view bitmap screenshot relativelayout

我从RelativeLayout生成一个Bitmap,它包含来自MapView设置为背景的另一个Bitmap以及右上角的另外两个视图(icon& text)。我得到了我想要的图像,但质量很低。我搜索高低,发现很多页面将视图转换为位图,但没有关于质量的内容。我猜它可能与缩放有关。

输出图像:

output image low quality

代码:

public Bitmap getBitmapFromMapView() {
    // Get Bitmap from MapView
    Bitmap mapBitmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas mapCanvas = new Canvas(mapBitmap);
    mMapView.draw(mapCanvas);

    // Inflate RelativeLayout view containing ImageView (FlagIt icon) and TextView (URL) in bottom-right corner
    // and merge it with the MapView Bitmap to produce an Overall Bitmap containing everything
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout overallRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.screenshot, null);
    overallRelativeLayout.setBackgroundDrawable(new BitmapDrawable(mapBitmap));

    overallRelativeLayout.measure(
            MeasureSpec.makeMeasureSpec(mMapView.getWidth(), MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(mMapView.getHeight(), MeasureSpec.EXACTLY));
    overallRelativeLayout.layout(0, 0, overallRelativeLayout.getMeasuredWidth(), overallRelativeLayout.getMeasuredHeight());

    Bitmap overallBitmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas overallCanvas = new Canvas(overallBitmap);
    overallRelativeLayout.draw(overallCanvas);


    return overallBitmap;
}

和膨胀的screenshot.xml XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/screenshot_corner_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="4dip"
        android:layout_marginRight="4dip"
        android:shadowColor="#000000"
        android:shadowDx="0.0"
        android:shadowDy="0.0"
        android:shadowRadius="4"
        android:text="http://www.aroha.ws"
        android:textColor="#ff0000"
        android:textSize="18sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/screenshot_corner_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/screenshot_corner_text"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="4dip"
        android:layout_marginRight="4dip"
        android:src="@drawable/corner_title_icon" />

</RelativeLayout>

在我的屏幕上显示的RelativeLayout看起来质量更好:

output image desired quality http://5312208804164438257-a-1802744773732722657-s-sites.googlegroups.com/site/arohacorp/pilcrowpipeblog/device-2012-04-20-224931.png

0 个答案:

没有答案