为画布在不同设备上的android比例 [英] is canvas in android proportional on different devices

查看:112
本文介绍了为画布在不同设备上的android比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用帆布和surfaceview的应用程序,我担心将来我会有很多问题,因为我不知道,如果在画布成正比,而与每一个设备。目前我只能用我的模拟器,因为我的手机的USB连接线无法正常工作(我知道..我必须得到一个新的..)。

反正,我想知道,如果在画布会转移我的坐标,使一切的比例,我的意思是,如果我有东西在点,可以说,(10,10)的设备屏幕上它是100×100(这只是为了便于计算的例子),那将是点(1,1)一个10×10的设备上。

这是真正困扰我...

谢谢!

解决方案

没有,这不会是这样的。如果有一个坐标(10,10),这将是在所有设备是相同的。我建议你​​比例您的图纸。

扩展您的图纸,你只需定义一个位图(将保持不变),你想提请(当屏幕尺寸发生变化,这将拉伸位图)。

  1. 定义一个恒定的位图:

      

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

  2. 获取规模为x和y

      

    宽度= game.getWindowManager()getDefaultDisplay()的getWidth()。             身高= game.getWindowManager()getDefaultDisplay()的getHeight()。       scaleXFromVirtualToReal =(浮点)宽/ this.gameScreenWidth;               scaleYFromVirtualToreal =(浮点)高度/ this.gameScreenHeight;

  3. 定义基于前面的定义位图中画布对象(允许你画它,例如canvas.drawRect()[...]。):

      

    画布canvasGameScreen =新的Canvas(gameScreen);

  4. 在你的渲染线程,你必须有一个叫做帧缓冲画布,这将使得虚拟帧缓存的:

      

    frameBuffer.drawBitmap(this.gameScreen,空,新的Rect(0,0,宽度,   高),空);

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!

解决方案

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

这篇关于为画布在不同设备上的android比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆