Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android clip canvas.drawBitmap

I am new to Android and I have two images one is an empty image and the other is a full image to represent a progress bar.

How can I only draw a percentage of the full image using canvas.drawBitmap?

I would prefer not to resize the bitmap image each time.

like image 362
Jonathan Ogle-Barrington Avatar asked Dec 06 '25 21:12

Jonathan Ogle-Barrington


1 Answers

you can use 2 Rects, the source Rect and the destination Rect to only draw a piece

//example
bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.backbutton);

Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Rect bitmapRect = new Rect(0, 0, canvasWidth -200,50);
canvas.drawBitmap(bitmap, source, bitmapRect, null);

a Rect is a rectangle like this

new Rect(topleftx,toplefty,bottomrightx,bottomrighty);
like image 151
JRowan Avatar answered Dec 12 '25 04:12

JRowan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!