Android crop image

In this Android tip, I am going to show you how to crop an image. For example, you have a rectangular image. To shape the image as an oval, you need to crop it.
To achieve this goal, first you create a Canvas object with a specified blank bitmap. You will draw your original image into this blank bitmap. Then create a Paint object for drawing. The first thing you have to draw into the blank bitmap is an oval shape so that the original image to be later drawn into the blank bitmap will be oval. You can use the drawRoundRect() method of Canvas to draw the overall shape. When you crop an image, it is import to use PorterDuffXfermode to specify the Xfermode of the paint. There are a lot of Xfermode (e.g. Mode.CLEAR, Mode.DARKEN, Mode.DST, Mode.SRC, Mode.SRC_IN, and so on). However, the Xfermode that helps you to clear area outside the oval shape is Mode.SRC_IN.

Now to have an example application on cropping an image, you create an Android project. Then add an ImageView to the activity_main.xml file. It is used to display the image that is the result of cropping task.

<RelativeLayout xmlns:android=""
   tools:context="com.example.andexample.MainActivity" >



Here is the code to crop the image and display the result image in the ImageView.

package com.example.andexample;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

   protected void onCreate(Bundle savedInstanceState) {
     ImageView imgview=(ImageView)findViewById(;

   public Bitmap getRoundedCornerImage(int imgid) {
   BitmapFactory.Options options = new BitmapFactory.Options();
   // Get the width and height of the image without allocating the memory
   options.inJustDecodeBounds = true;
   BitmapFactory.decodeResource(getResources(), imgid, options);
   int imgHeight = options.outHeight;
   int imgWidth = options.outWidth;
   // Create a blank bitmap object
     Bitmap output = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888);
     // Create Canvas object for the blank image
     Canvas canvas = new Canvas(output);    
     // Create paint object
     final Paint paint = new Paint();
     // Smooth out the edges of drawing
     // Define rectangle for cropping area
     final Rect rect = new Rect(0, 0, imgWidth, imgHeight);
     final RectF rectF = new RectF(rect);
     final float Rx= 200;    
     // Fill canvas' blank bitmap with a transparent color
     canvas.drawARGB(0, 0, 0, 0);
     // Set paint color
     // Draw the specified round-rect using the specified paint
     canvas.drawRoundRect(rectF, Rx, Rx, paint);
     // Apply Xfermode to paint object
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
     // Get original image
     Bitmap originalImg=BitmapFactory.decodeResource(getResources(),imgid);
     // Crop the image
     //canvas.drawBitmap(originalImg, rect, rect, paint);
     canvas.drawBitmap(originalImg, -50, 0, paint);
     //canvas.drawText("Cropped image", 250, 250, paint);
      return output;




Posted by: Dara | post date: 11-12-2014 | 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.