MainActivity is as follows:
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. porterduxfermode; import android.net. uri; import android. OS. bundle; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import Droid. widget. button; import android. widget. imageView;/*** Requirement Description: * select two images from the album to synthesize an image ** Summary: * 1. Load the image * mBitmap1 = BitmapFactory Based on the image Uri. decodeStream * (getContentResolver (). openInputStream (photoFileUri); * Why is getContentResolver used? Because the album itself is a content provider * 2. Note the constructor: * Canvas canvas = new Canvas (bitmap); * why do you need to input a bitmap? * We must draw on the canvas. * But here we pass in a bitmap * that is, this bitmap is a carrier, which is equivalent to using bitmap as the canvas. * Of course, it is essentially: painting things on the bitmap in the canvas * You can also think like this: * we didn't end up using mImageView. setImageBitmap (the parameter is a Bitmap) to display * an image? Therefore, when constructing the Canvas, a Bitmap is input. then, draw two images * to the Bitmap. the Bitmap is a synthetic map of the two photos. * Of course, You need to draw two images to a 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 quiet = 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 porterduduxfermode (android. graphics. porterDuff. mode. MULTIPLY); canvas. drawBitmap (mBitmap2, 0, 0, paint); mImageView. setImageBitmap (bitmap );}}}}
Main. xml is as follows:
<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 = "select the first image"/> <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 = "select the second image"/> <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>