1

I'm trying to create a CircleImageView on Android using RoundedBitmapDrawable overriding my CustomImageView onDraw method, but I can never get the image shown.

My code:

@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);

        RoundedBitmapDrawable bmp = getCroppedBitmap(bitmap);
        bmp.setAntiAlias(true);
        bmp.setDither(true);
        bmp.setCornerRadius(Math.min(drawable.getMinimumWidth(),drawable.getMinimumHeight()));
        bmp.draw(canvas);
    }

    private RoundedBitmapDrawable getCroppedBitmap(Bitmap bitmapimg) {
        return RoundedBitmapDrawableFactory.create(getResources(),bitmapimg);
    }

What am I doing wrong and how shoud I proceed to make it work ?

Thanks

Leonardo
  • 3,141
  • 3
  • 31
  • 60
  • see [How to make an ImageView with rounded corners](http://stackoverflow.com/questions/2459916/how-to-make-an-imageview-with-rounded-corners) post – ρяσѕρєя K Dec 29 '14 at 04:44
  • Thanks for the reply, but I don't want to use this type of coding since it's not performatic and if we have already written code, why reinvent the wheel? – Leonardo Dec 29 '14 at 04:46

2 Answers2

1

Actually I forgot a simple thing:

bmp.setBounds(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));

Thanks for helping !

Leonardo
  • 3,141
  • 3
  • 31
  • 60
0

It looks like you need to call canvas.drawBitmap(bitmap), no?

Developer Paul
  • 1,502
  • 2
  • 13
  • 18