在我的应用程序中,一个相对布局完全三个ImageView()。我想通过单击按钮将所有三个图像视图保存为单个.png文件到设备存储。
我的代码:
布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/make" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/am0" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Save Meme"
android:textStyle="bold"
android:textColor="@color/wt" />
<ImageView
android:id="@+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="64dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="74dp"
android:src="@drawable/ic_launcher" />
活动代码:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sm);
main1 = (ImageView) findViewById(R.id.imageView1);
top = (ImageView) findViewById(R.id.top);
down = (ImageView) findViewById(R.id.down);
Bundle rec = getIntent().getExtras();
int rec1 = getIntent().getExtras().getInt("ams0");
Bitmap bmp = (Bitmap) rec.getParcelable("bm0");
Bitmap bmp1 = (Bitmap) rec.getParcelable("bm1");
main1.setImageResource(rec1);
top.setImageBitmap(bmp);
down.setImageBitmap(bmp1);
}
如何将图像视图另存为文件?
答案 0 :(得分:1)
尝试使用Сanvas
Bitmap mainBitmap = ((BitmapDrawable)main1.getDrawable()).getBitmap().copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mainBitmap);
canvas.drawBitmap(bmp.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
canvas.drawBitmap(bmp1.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/myImages.png");
mainBitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
} catch (IOException e) {
e.printStackTrace();
}