3

I found lot of sample codes for circular corners.What i need is circular image.I found this one okay but the output is not perfectly circular.

ImageView im = (ImageView) findViewById(R.id.imag);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.white);
Bitmap circleBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bmp.getWidth() / 2, bmp.getHeight() / 2, bmp.getWidth() / 2, paint);
im.setImageBitmap(circleBitmap);
Seraphim's
  • 12,559
  • 20
  • 88
  • 129

4 Answers4

5

Try this...

public static Bitmap getCircularBitmapFrom(Bitmap bitmap) {
    if (bitmap == null || bitmap.isRecycled()) {
        return null;
    }
    float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap
            .getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f;
    Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,
            TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    Canvas canvas = new Canvas(canvasBitmap);

    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            radius, paint);

    return canvasBitmap;
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
1

Look at

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

Or try this

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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 CircleImage extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout);
        ImageView img1 = (ImageView) findViewById(R.id.imageView1);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.hair_four);
        Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
        Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
        img1.setImageBitmap(conv_bm);
        // TODO Auto-generated method stub
    }

    public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            int color = 0xff424242;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 200, 200);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(50, 50, 50, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }

}
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
1

hi have a look at the given code snippet

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class CornerededImageActivity extends Activity {
    /** Called when the activity is first created. */
    ImageView imag;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imag=(ImageView)findViewById(R.id.image);

        //ImageView img1=(ImageView)findViewById(R.id.imageView1);
        BitmapFactory.Options bitopt=new BitmapFactory.Options();
        bitopt.inSampleSize=1;

        String filepath ="/mnt/sdcard/LOST.DIR";
        File imagefile = new File(filepath + "/logo.jpg");
        FileInputStream fis = null;
        try 
        {
        fis = new FileInputStream(imagefile);
        }  
        catch (FileNotFoundException e1)
        {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }
        Bitmap bi = BitmapFactory.decodeStream(fis);
        if(bi!=null){
            imag.setImageBitmap(getRoundedCornerBitmap(bi));
        }

    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
         bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
    }
}

please also visit

http://manishkpr.webheavens.com/android-rounded-corner-image-bitmap-example/

How should I give images rounded corners in Android?

https://codereview.stackexchange.com/questions/29324/android-image-with-rounded-corners

Hope it will help you

beside this you can visit https://github.com/vinc3m1/RoundedImageView

There are many ways to create rounded corners in android, but this is the fastest and best one that I know of because it:

does not create a copy of the original bitmap does not use a clipPath which is not hardware accelerated and not anti-aliased. does not use setXfermode to clip the bitmap and draw twice to the canvas.

enter image description here

Community
  • 1
  • 1
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
0

You can use try this method to get roundcornor bitmap

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, Context context) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getWidth(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
    final RectF rectF = new RectF(rect);
    final float roundPx = context.getResources().getDimension(
            R.dimen.rect_round_px);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
Santhosh
  • 1,867
  • 2
  • 16
  • 23