4

I have created an android application, in that I want to display Image in Image-View in round shape.

But image doesn't display properly in Image-View.

My code is-

int rounded_value = 168;
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(rounded_value)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(options).build(); 
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(photourl, imageView, options);

For example-

Original Image is- It's size is 168*168

168*168

and I get output like-ImageView size is 85*85

85*85

I wants output like-

85*85

Anjali Pandya
  • 83
  • 2
  • 9

3 Answers3

0

Try this , its working for me

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius)
{
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
0

User masked image view: http://goo.gl/w2LrM0

Provide mask, in image view xml declaration, as a drawable xml with shape oval.

No need to apply image cropping to round shape.

Image view widget in layout:

<com.android.BezelImageView
// required properties, to getexact circle height should be equat to width
 app:maskDrawable="@drawable/round_mask" />

round_mask.xml :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
           <solid android:color="#000000" />
        </shape>
    </item>
</layer-list>
Ammar
  • 1,811
  • 5
  • 26
  • 60
0

This function returns a rounded cropped bitmap

public Bitmap getCroppedBitmap(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(`n`ew PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        // Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
        // return _bmp;
        return output;
}
Nazik
  • 8,696
  • 27
  • 77
  • 123
Aritra
  • 436
  • 2
  • 6