通过在Canvas对象上绘制一个位图对象,然后在相同的Canvas上绘制第二个图像来合成。区别在于绘制第二个图像时,需要再Paint对象上指定一个过渡模式(Xfermode)。
示例代码如下:
package com.example.testphotoedit; import java.io.FileNotFoundException; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.net.Uri; import android.os.Bundle; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class Picturecomposite extends Activity implements OnClickListener { private Button bt1, bt2; private ImageView composite; private static final int PICKED_ONE = 1; private static final int PICKED_TWO = 2; private static boolean onePicked=false; private static boolean twoPicked=false; private Bitmap bmp1,bmp2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.picture_1); bt2 = (Button) findViewById(R.id.picture_2); composite = (ImageView) findViewById(R.id.composite_all); bt1.setOnClickListener(this); bt2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub int which = -1; switch (v.getId()) { case R.id.picture_1: which = PICKED_ONE; break; case R.id.picture_2: which = PICKED_TWO; break; } Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, which); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri imageFileUri = data.getData(); if(requestCode==PICKED_ONE){ bmp1=loadBitmap(imageFileUri); onePicked=true; }else if(requestCode==PICKED_TWO){ bmp2=loadBitmap(imageFileUri); twoPicked=true; } } //如果选择了两幅图像,且两个位图均已完成实例化,继续合成操作 if(onePicked&&twoPicked){ Bitmap alteredBitmap = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());//创建一个空的位图对象,大小和配置与第一个位图对象(bmp1)相同 Canvas canvas=new Canvas(alteredBitmap); Paint paint=new Paint(); canvas.drawBitmap(bmp1, 0, 0, paint); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));//实例化对象 canvas.drawBitmap(bmp2, 0, 0, paint); composite.setImageBitmap(alteredBitmap); } } @SuppressWarnings("deprecation") private Bitmap loadBitmap(Uri imageFileUri) { Display currentDisply = getWindowManager().getDefaultDisplay(); int dw = currentDisply.getWidth() / 2 - 100; int dh = currentDisply.getHeight() / 2 - 100; try { BitmapFactory.Options bmpFactory = new BitmapFactory.Options(); bmpFactory.inJustDecodeBounds = true;// 加载图像的尺寸而非图像本身 Bitmap bmp = BitmapFactory.decodeStream(getContentResolver() .openInputStream(imageFileUri), null, bmpFactory); int heightRatio = (int) Math .ceil(bmpFactory.outHeight / (float) dh); int widthRatio = (int) Math.ceil(bmpFactory.outWidth / (float) dw); if (heightRatio > 1 && widthRatio > 1) { if (heightRatio > widthRatio) { bmpFactory.inSampleSize = heightRatio; } else { bmpFactory.inSampleSize = widthRatio; } } bmpFactory.inJustDecodeBounds = false;// 加载真实的图像 bmp = BitmapFactory.decodeStream(getContentResolver() .openInputStream(imageFileUri), null, bmpFactory); return bmp; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
activity_main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/picture_1" android:layout_width="match_parent" android:layout_height="50dp" android:text="choose Picture1"/> <Button android:id="@+id/picture_2" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/picture_1" android:text="choose Picture2"/> <ImageView android:id="@+id/composite_all" android:layout_below="@+id/picture_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:contentDescription="@null"/> </RelativeLayout>
运行结果如下:
原文:http://blog.csdn.net/woyaochenggong774/article/details/45147735