Android图片的合成

mainActivity如下:

package c.c;

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.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/**
 * 需求描述:
 * 从相册中选取两张图片合成一张图片
 * 
 * 小结:
 * 1 依据图片的Uri加载图片
 * mBitmap1=BitmapFactory.decodeStream
 * (getContentResolver().openInputStream(photoFileUri));
 * 为什么使用getContentResolver呢?因为在相册本身是一个内容提供者
 * 2 注意构造方法:
 * Canvas canvas=new Canvas(bitmap);
 * 为什么要传入一个bitmap呢?
 * 我们画东西肯定是在画布上画.
 * 但是在这里传入一个bitmap
 * 即该bitmap是一个载体,相当于把bitmap作为了画布.
 * 当然本质上是:把东西画在了canvas里的bitmap上
 * 也可以这么想:
 * 我们最后不是要用mImageView.setImageBitmap(参数是一个Bitmap)来显示
 * 一张图片么?所以在构造Canvas的时候传入了一个Bitmap.然后把两张图片
 * 画到了该Bitmap上.最后该Bitmap就是两张照片的合成图.
 * 所以当然要把两张图片画到一个Bitmap上
 */
public class MainActivity extends Activity {
	private static int REQUESTCODE=0;
	private final static int PICK_FIRST_PHOTO=1;
	private final static int PICK_SECOND_PHOTO=2;
	private boolean isPickedFirstPhoto=false;
	private boolean isPickedSecondPhoto=false;
    private Button mButton1;
    private Button mButton2;
    private Bitmap mBitmap1;
    private Bitmap mBitmap2;
    private ImageView mImageView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private void init(){
    	mButton1=(Button) findViewById(R.id.button1);
    	mButton1.setOnClickListener(new ButtonOnClickListenerImpl());
    	mButton2=(Button) findViewById(R.id.button2);
    	mButton2.setOnClickListener(new ButtonOnClickListenerImpl());
    	mImageView=(ImageView) findViewById(R.id.imageView);
    }
    private class ButtonOnClickListenerImpl implements OnClickListener{
		public void onClick(View v) {
			if (v.getId()==R.id.button1) {
				REQUESTCODE=PICK_FIRST_PHOTO;
			} else {
				REQUESTCODE=PICK_SECOND_PHOTO;
			}
			Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, REQUESTCODE);
		}
    	
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode==RESULT_OK) {
    		Uri photoFileUri=data.getData();
			if (requestCode==PICK_FIRST_PHOTO) {
				try {
					mBitmap1=BitmapFactory.decodeStream
					(getContentResolver().openInputStream(photoFileUri));
					isPickedFirstPhoto=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (requestCode==PICK_SECOND_PHOTO) {
				try {
					mBitmap2=BitmapFactory.decodeStream
					(getContentResolver().openInputStream(photoFileUri));
					isPickedSecondPhoto=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (isPickedFirstPhoto&&isPickedSecondPhoto) {
				Bitmap bitmap=
				Bitmap.createBitmap(mBitmap1.getWidth(), mBitmap1.getHeight(), mBitmap1.getConfig());
				Canvas canvas=new Canvas(bitmap);
				Paint paint=new Paint();
				canvas.drawBitmap(mBitmap1, 0, 0, paint);
				paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
				canvas.drawBitmap(mBitmap2, 0, 0,paint);
				mImageView.setImageBitmap(bitmap);
				
			}

		}
    }
}


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/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="选择第一张图片"
         />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button1"
        android:text="选择第二张图片"
     />
    <ImageView 
      android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/button2"
     />

</RelativeLayout>


 

你可能感兴趣的:(Android图片的合成)