Droid
Droid

Reputation: 569

Android draw a part of scaled bitmap using canvas

I'm using canvas and have an image 1000x500, phone width 480 and height 800

Scale sample:

        scale = (float) (height / image.getHeight());
        image_width = image.getWidth() * scale;
        image_height = image.getHeight() * scale;

How do I draw this scaled image using canvas, having only center(screen size) of the image visible while left and right sides are outside of screen?

Upvotes: 0

Views: 904

Answers (1)

Jignesh Jain
Jignesh Jain

Reputation: 1568

do it this way

Rect rs = new Rect();
Rect rd = new Rect();
rs.left = rs.top = 0;
rs.right = 480;
rs.bottom = 800;

<calculate destination rectangle from device size>

canvas.drawBitmap(myBitmap, rs, rd, null);

You can also scale and translate (shift) the entire canvas

canvas.scale(float scaleX, float scaleY);
canvas.translate(float dx, float dy);

Upvotes: 2

Related Questions