Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

coordinates - 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 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...