Android: How do I scale a bitmap to fit the screen size using canvas/draw bitmap?

前端 未结 3 1274
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 13:15

I am taking an image and drawing it to the screen using canvas. I want to scale it based on the screen size.

This is what I tried, but it cuts off a large chunk of the

3条回答
  •  隐瞒了意图╮
    2021-01-21 13:38

    dont need creat scale bitmap, instead:

    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,frameBufferHeight, Config.RGB_565);
    

    frameBufferWidth and frameBufferHeight are the sizes that you want to make your app base on those(for example 480*800)

    then simply add this code:

    Rect dstRect = new Rect();
    canvas.getClipBounds(dstRect); // get canvas size base screen size
    canvas.drawBitmap(framebuffer, null, dstRect, null);
    

    it draw your final bitmap according to screen size

提交回复
热议问题