Leonardo
Leonardo

Reputation: 3191

Override onDraw with RoundedBitmapDrawable

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

Upvotes: 1

Views: 500

Answers (2)

Leonardo
Leonardo

Reputation: 3191

Actually I forgot a simple thing:

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

Thanks for helping !

Upvotes: 1

Developer Paul
Developer Paul

Reputation: 1510

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

Upvotes: 0

Related Questions