Android: Merge bitmap with canvas

Source: Internet
Author: User

    • Key points
null);

Draws the srcrect area of the bitmap to the Dstrect area of the canvas

    • Demo

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:id="@+id/rl_root"android:layout_width="Match_parent"android: Layout_height="Match_parent"android:background="@android: Color/background_dark"  android:orientation="Horizontal" >                        <ImageView        Android:id="@+id/iv_bmp1"        Android:layout_width="80DP"        Android:layout_height="100DP"        Android:layout_alignparentleft="true"        Android:layout_alignparentstart="true"        Android:layout_alignparenttop="true"        Android:layout_marginleft="20DP"        Android:layout_marginstart="20DP"        Android:layout_margintop="20DP"        android:contentdescription="@null"        android:src="@drawable/baby"/>    <ImageView        Android:id="@+id/iv_bmp2"        Android:layout_width="80DP"        Android:layout_height="100DP"        Android:layout_alignparentend="true"        Android:layout_alignparentright="true"        Android:layout_alignparenttop="true"        Android:layout_marginend="20DP"        Android:layout_marginright="20DP"        Android:layout_margintop="20DP"        android:contentdescription="@null"        android:src="@drawable/baby2"/>    <buttonandroid:id="@+id/btn_merge"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_below=" @id/iv_ Bmp1 "android:layout_centerhorizontal=" true "android:layout_margintop= "20DP" Android:text="Merge" />                                                            <ImageViewandroid:id= "@+id/iv_bmp_merge"android:layout_width=" 160DP "android:layout_height=" 100DP "android:layout_below=" @id/btn_merge "  android:layout_centerhorizontal="true"android:layout_margintop=" 20DP "android:contentdescription=" @null " />                                                        </relativelayout>

The goal is to merge the pictures on IvBmp1 and IVBMP2, and display them on the Iv_bmp_merger.

    • Mainactivity.java
 PackageNet.mobctrl.mergebitmap;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Handler.Callback;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;/** * * @author Zheng haibo * @web http://www.mobctrl.net * */ Public  class mainactivity extends Activity {    PrivateImageView IVBMP1;PrivateImageView IVBMP2;PrivateImageView Ivbmpmerge;PrivateButton mergebtn;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main);    Initui (); }Private void Initui() {ivBmp1 = (ImageView) Findviewbyid (R.ID.IV_BMP1);        IVBMP2 = (ImageView) Findviewbyid (R.ID.IV_BMP2);        Ivbmpmerge = (ImageView) Findviewbyid (R.id.iv_bmp_merge);        MERGEBTN = (Button) Findviewbyid (R.id.btn_merge); Mergebtn.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v)            {Showmergebitmap ();    }        }); }PrivateHandler Handler =NewHandler (NewCallback () {@Override         Public Boolean Handlemessage(Message msg)            {Bitmap Bitmap = (Bitmap) msg.obj; Ivbmpmerge.setimagebitmap (bitmap);return false; }    });/** * Show merged graphs */    protected void Showmergebitmap() {NewThread (NewRunnable () {@Override             Public void Run() {Bitmap Bitmap = Mergebitmap ();                Message Messge = Handler.obtainmessage ();                Messge.obj = bitmap;            Handler.sendmessage (MESSGE);    }}). Start (); }/ * * Use canvas to merge bitmap */    PrivateBitmapMergebitmap() {//Get bitmap pictures on ImageViewBitmap bmp1 = ((bitmapdrawable) ivbmp1.getdrawable ()). Getbitmap (); Bitmap BMP2 = ((bitmapdrawable) ivbmp2.getdrawable ()). Getbitmap ();//Create an empty background bitmap        //Generate canvas imageBitmap Resultbitmap = Bitmap.createbitmap (Ivbmpmerge.getwidth (), Ivbmpmerge.getheight (), Bitmap.Config.RGB_        565); Canvas Canvas =NewCanvas (RESULTBITMAP);//Use a blank image to create a canvas        //Draw the BMP1 on the canvasRect srcrect =NewRect (0,0, Bmp1.getwidth (), Bmp1.getheight ());//intercept the rectangular area in the BMP1Rect dstrect =NewRect (0,0, Ivbmpmerge.getwidth ()/2, Ivbmpmerge.getheight ());//BMP1 position in the target canvasCanvas.drawbitmap (BMP1, Srcrect, Dstrect,NULL);//Draw the BMP2 on the canvasSrcrect =NewRect (0,0, Bmp2.getwidth (), Bmp2.getheight ());//intercept the rectangular area in the BMP1Dstrect =NewRect (Ivbmpmerge.getwidth ()/2,0, Ivbmpmerge.getwidth (), Ivbmpmerge.getheight ());//BMP2 position in the target canvasCanvas.drawbitmap (BMP2, Srcrect, Dstrect,NULL);//BMP1,BMP2 Merge display        returnResultbitmap; }}
    • Run effect

      After clicking Merge

Android: Merge bitmap with canvas

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.