Android add image on another image

In this Android tip, I am going to show you how to do a simple image editing task by adding another image on it. To add an image on another image, first you need to get the Bitmap objects referencing bother images on the disc. Then convert the bottom image to a mutable bitmap object for later editing. There is no method of the Bitmap that allows you to add another image on it. However, you can accomplish the task using Canvas. Canvas allows you draw text, shapes, and even images on the Bitmap object. You create an instance of Canvas passing the mutable image. After that you can use the drawBitmap() method of the Canvas instance to draw the top image on the bottom image. 


add image on another image
Now to have example application on adding image on another image, you create a new Android project. Then in the activity_main.xml file, you add an ImageView to display the result image. Here is the code written in the MainActivity class to add an image called ic_launcher on another image called back_image and display the result image in the ImagView.


		

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.net.wifi.WifiManager;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {      

protected void onCreate(Bundle savedInstanceState) {     

   super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     ImageView imageView = (ImageView) findViewById(R.id.imageView);
     imageView.setImageBitmap(getRoundedCornerImage());
}    // get the back image
     Bitmap backImage=BitmapFactory.decodeResource( getResources(), R.drawable.back_image);
     // Get the front image
     Bitmap originalImg=BitmapFactory.decodeResource( getResources(),R.drawable.ic_launcher);
     // Convert the image to mutable bitmap for later editing     

     Bitmap mutableBitmap = backImage.copy( Bitmap.Config.ARGB_8888, true);
     // Create Canvas object for the mutable image
     Canvas canvas = new Canvas(mutableBitmap);
     // Create paint object
     final Paint paint = new Paint();
     paint.setColor(Color.parseColor("#44aa77")) ;
     // Draw the front image on the back image
     canvas.drawBitmap(originalImg, 100, 50, paint);
     return mutableBitmap;
   }
}


Posted by: Dara | post date: 03-11-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.