-2

i trying to do bitmap circle it work but high resolution image not work properly.It is working like small circle in image view.

i have tried this code and i tried many other code,but didn't work properly

public static Bitmap circleShape(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());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                bitmap.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
        //return _bmp;
        return output;
}
Android Team
  • 63
  • 1
  • 8
  • 1
    Check my answer http://stackoverflow.com/questions/18378741/how-to-make-an-imageview-in-circular-shape/18378873#18378873 – Piyush Apr 30 '15 at 08:28
  • its very blurred, i have tested – Android Team Apr 30 '15 at 08:29
  • Post your output. The code from the previous comment down-samples the image to 50*50, the `targetWidth` and `targetHeight` specifies how the original image is sampled. – tom91136 Apr 30 '15 at 08:37

1 Answers1

2

I have tried so many code and that was not working perfectly. I have searched and found this code. It work awesome, amazing.. hope anybody helpfull this code.. thanks

public static Bitmap circleShape(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap,
            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;

}
Android Team
  • 63
  • 1
  • 8