1

I am using following code for converting ImageView into rounded iage, but gettin error at "Mode.SRC_IN" . Need help. Thank you in advance.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        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 = pixels;

        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;
    } 

4 Answers4

0

Use this class:

public class RoundedImageView extends ImageView {
private Paint objPaint = new Paint();

public RoundedImageView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    Log.i("TAG", "Bitmap Width:" + w);

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    objPaint.setAntiAlias(true);
    objPaint.setDither(true);
    canvas.drawBitmap(roundBitmap, 0, 0, objPaint);

}

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;
}

}

now in xml file:

       <path to your class
        android:id="@+id/iv_leaderboard_profile_icon"
        android:layout_width="@dimen/lederboard_image_size"
        android:layout_height="@dimen/lederboard_image_size"
        android:src="@drawable/ic_launcher" />

here

  path= com.example.view.RoundedImageView 

something like that.

hope you understand, thank you.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
0

Romain guy has an excellent blog post about achieving rounded corners on ImageViews here

Sunny
  • 286
  • 1
  • 4
  • 14
0

For PorterDuffXfermode, you have to write import android.graphics.PorterDuffXfermode;

For Config.ARGB_8888, you have to write import android.graphics.Bitmap.Config;

Otherwise Direct press CTRL + SHIFT + O to organize imports.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
0

A new class is introduced in android.support.v4. Which provides you the circled image which any customization.

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(pixels);
imageView.setImageDrawable(roundedBitmapDrawable);

I would recommend to make use of it.

Gowtham Kumar
  • 534
  • 8
  • 22