is canvas in android proportional on different dev

2019-01-29 06:58发布

I am trying to make an app using canvas and a surfaceview, and I am worrying that in the future I would have many problems with it because I am not sure if the canvas is proportional with every device. currently I can only use my emulator since my phone's usb cable doesn't work(I know.. I have to get a new one..).

anyways, i would like to know if the canvas would transfer my coordinates and make everything proportional, what I mean by that is that if i have something in point a, lets say (10, 10) on a device that the screen of it is 100 X 100 (this is just an example for easy calculation) it would be on point (1, 1) on a 10 X 10 device.

This is really bothering me...

Thanks!

2条回答
看我几分像从前
2楼-- · 2019-01-29 07:07

No, this wouldn't be the case. If you have a coordinate (10,10), it would be the same on all devices. I'd suggest you scale your drawings.

To scale your drawings you simply define a bitmap (that will stay the same) you'd like to draw to (when screen sizes change, that bitmap will be stretched).

  1. Define a constant bitmap:

    Bitmap gameScreen = Bitmap.createBitmap(getGameScreenWidth(), getGameScreenHeight(), Config.RGB_565);

  2. Get the scale for both x and y

    width = game.getWindowManager().getDefaultDisplay().getWidth(); height = game.getWindowManager().getDefaultDisplay().getHeight(); scaleXFromVirtualToReal = (float) width/this.gameScreenWidth; scaleYFromVirtualToreal = (float) height/this.gameScreenHeight;

  3. Define a canvas object based on the bitmap you defined earlier on (allowing you to draw to it eg. canvas.drawRect() [...]):

    Canvas canvasGameScreen = new Canvas(gameScreen);

  4. In your rendering Thread you'll have to have a Canvas called frameBuffer, which will render the virtual framebuffer:

    frameBuffer.drawBitmap(this.gameScreen, null, new Rect(0, 0, width, height), null);

查看更多
Lonely孤独者°
3楼-- · 2019-01-29 07:30

No, the unit on the screen (whether you are using canvas or OpenGL) is a pixel. You can get the size of your canvas using Canvas.getWidth() and Canvas.getHeight() if you need relative coordinates, but your Canvas drawing methods are also in Pixels, so I guess you will need to convert coordinates in OpenGL only and not while using Canvas.

查看更多
登录 后发表回答