Skip to content Skip to sidebar Skip to footer

Is Canvas In Android Proportional On Different Devices

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 proportion

Solution 1:

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);

Solution 2:

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.

Post a Comment for "Is Canvas In Android Proportional On Different Devices"