我正在从一个相对布局生成一个位图,其中包含来自 MapView 设置为背景的另一个位图以及右上角的两个其他视图(图标和文本)。我得到了我想要的图像,但它的质量很低。我搜索了高低,发现很多关于将视图转换为位图的页面,但没有关于质量的内容。我猜这可能与缩放有关。
输出图像:
编码:
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 看起来质量更好: