25 Feb 2014

Display Image in Round Corner android

Hello Friends,

Today i am going to post Display Image in Shape/Round Corner. here you change corner value as per your requirement.

OutPut Screen:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity 
{
 ImageView imageView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  imageView=(ImageView) findViewById(R.id.img);
  
  Bitmap bitmapFromDrawable = BitmapFactory.decodeResource(getResources(),R.drawable.kukulkan);
  imageView.setImageBitmap(roundCorner(bitmapFromDrawable,20));
 }
 
 
 public static Bitmap roundCorner(Bitmap src, float round) 
 {
     // image size
     int width = src.getWidth();
     int height = src.getHeight();
     // create bitmap output
     Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
     // set canvas for painting
     Canvas canvas = new Canvas(result);
     canvas.drawARGB(0, 0, 0, 0);
  
     // config paint
     final Paint paint = new Paint();
     paint.setAntiAlias(true);
     paint.setColor(Color.BLACK);
  
     // config rectangle for embedding
     final Rect rect = new Rect(0, 0, width, height);
     final RectF rectF = new RectF(rect);
  
     // draw rect to canvas
     canvas.drawRoundRect(rectF, round, round, paint);
  
     // create Xfer mode
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
     // draw source image to canvas
     canvas.drawBitmap(src, rect, rect, paint);
  
     // return final image
     return result;
 }

}



You may also like this post 
More reference link


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment