Android: Merge Bitmap and androidcanvas using Canvas

Source: Internet
Author: User

Android: Merge Bitmap and androidcanvas using Canvas

  • Key Points
canvas.drawBitmap(bitmap, srcRect, dstRect, null);

Draw the srcRect area of 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" />    <Button        android: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" />    <ImageView        android: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 purpose is to merge the images on ivbmp 1 and ivbmp 2 and display them on iv_bmp _merger.

  • MainActivity. java
Package net. mobctrl. mergebitmap; import android. app. activity; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. rect; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. OS. handler; import android. OS. handler. callback; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView;/*** @ author Zheng Haibo * @ web http://www.mobctrl.net ***/public class MainActivity extends Activity {private ImageView ivbmp 1; private ImageView ivbmp 2; private ImageView ivbmp merge; private Button mergeBtn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initUi ();} private void initUi () {ivbmp 1 = (ImageView) findViewById (R. id. iv_bmp 1); ivbmp 2 = (ImageView) findViewById (R. id. iv_bmp 2); ivbmp merge = (ImageView) findViewById (R. id. iv_bmp _merge); mergeBtn = (Button) findViewById (R. id. btn_merge); mergeBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {showMergeBitmap () ;}});} private Handler handler = new Handler (new Callback () {@ Override public boolean handleMessage (Message msg) {Bitmap bitmap = (Bitmap) msg. obj; ivbmp merge. setImageBitmap (bitmap); return false ;}});/*** display the merged graph */protected void showMergeBitmap () {new Thread (new Runnable () {@ Override public void run () {Bitmap bitmap = mergeBitmap (); Message messge = handler. obtainMessage (); messge. obj = bitmap; handler. sendMessage (messge );}}). start ();}/** merge Bitmap Using Canvas */private Bitmap mergeBitmap () {// obtain Bitmap image Bitmap BMP 1 = (BitmapDrawable) ivbmp 1.getdrawable () on ImageView ()). getBitmap (); Bitmap BMP 2 = (BitmapDrawable) ivbmp 2.getdrawable ()). getBitmap (); // create an empty background bitmap // generate the canvas image Bitmap resultBitmap = Bitmap. createBitmap (ivbmp merge. getWidth (), ivbmp merge. getHeight (), Bitmap. config. RGB_565); Canvas canvas = new Canvas (resultBitmap); // use a blank image to generate a canvas. // draw BMP 1 on the Canvas. Rect srcRect = new Rect (0, 0, 0, BMP 1.getwidth (), BMP 1.getheight (); // truncate the Rect dstRect = new Rect (0, 0, ivbmp merge. getWidth ()/2, ivbmp merge. getHeight (); // position of BMP 1 in the target canvas. drawBitmap (BMP 1, srcRect, dstRect, null); // draw BMP 2 on the canvas srcRect = new Rect (0, 0, BMP 2.getwidth (), BMP 2.getheight ()); // intercept dstRect = new Rect (ivbmp merge. getWidth ()/2, 0, ivbmp merge. getWidth (), ivbmp merge. getHeight (); // position of BMP 2 in the target canvas. drawBitmap (BMP 2, srcRect, dstRect, null); // combines BMP 1 and BMP 2 to display return resultBitmap ;}}
  • Running Effect

    Click merge.

Related Article

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.