mikhail
mikhail

Reputation: 137

How scale bitmap in Canvas android

I want to diasplay image(bitmap) in SurfaceView using Canvas. My requirement is that size of the canvas greater than image.I need space(eg:10 dp) in each side of the image in Canvas. How I can scale this?

Thanks Mikahail

Upvotes: 1

Views: 172

Answers (2)

zack
zack

Reputation: 45

Put this method in your SurfaceView Class :D

 public void draw(Canvas canvas) {
    final float scaleFactoryX = getWidth() / (WIDTH * 1.f);
    final float scaleFactoryY = getHeight() / (HEIGHT * 1.f);
    if (canvas != null) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactoryX, scaleFactoryY);

        canvas.restoreToCount(savedState);
    }

}

Upvotes: 0

RexSplode
RexSplode

Reputation: 1505

You can draw rectangle of desired Canvas size, and then draw your image with parameters (11, 11) which will draw it 10px from corner for each dimension.

Upvotes: 1

Related Questions