Android image editing and processing (iv)

Source: Internet
Author: User

Compositing by drawing a bitmap object on the Canvas object and then drawing a second image on the same canvas. The difference is that when you draw a second image, you need to specify a transition mode (Xfermode) on the Paint object.

The sample code is as follows:

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;pri Vate Static Boolean twopicked=false;private Bitmap bmp1,bmp2; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.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);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubint which = -1;switch (V.getid ()) {case R.ID.PICTU Re_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);} @Overrideprotected 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 two images are selected and two bitmaps have been instantiated, continue the compositing operation if (onepicked&&twopicked) {Bitmap ALteredbitmap = Bitmap.createbitmap (Bmp1.getwidth (), Bmp1.getheight (), Bmp1.getconfig ());//Create an empty bitmap object, Size and configuration same as the first bitmap object (BMP1) Canvas canvas=new canvas (ALTEREDBITMAP); Paint paint=new paint (); Canvas.drawbitmap (bmp1, 0, 0, paint);p Aint.setxfermode (New Porterduffxfermode ( Android.graphics.PorterDuff.Mode.MULTIPLY));//Instantiate Object 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 {Bitma Pfactory.options bmpfactory = new Bitmapfactory.options () Bmpfactory.injustdecodebounds = true;//Load the dimensions of the image rather than the image itself 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/(f loat) DW); if (heightRatio > 1 && widthRatio > 1) {if (HeightRatio > WidthRatio) {bmpfactory.insamplesize = HeightRatio;} else {bmpfactory.insamplesize = WidthRatio;}} Bmpfactory.injustdecodebounds = false;//Loads the real image bmp = Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imagefileuri), NULL, bmpfactory); return bmp;} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}

Activity_main.xml file:

<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>


The results of the operation are as follows:




Android image editing and processing (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.