如何根据手机方向(横向、纵向)绘制画布?
纵向模式没有问题,但当我在横向模式下尝试我的应用程序时,它看起来不正常。解决此问题的一般方法是什么。我的第一个想法是替换 X 和 Y 位置。如果我在右边跟踪我应该怎么做?我是否应该在 onDraw() 函数的开头使用 if 语句并编写两次完全相同的代码(一个用于 x,y,另一个用于 y,x)?
There is no problem with portrait mode but when i try my app on landscape mode it doesn't look how it should.What is general aproach to solve this problem.My first idea is replacing X and Y places.If i am on the right track how should i do this ? Should i use an if statement at the beginning of the onDraw() function and write entire same code twice (one for x,y and other is for y,x) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在为绘图调用指定绝对宽度和高度。例如,如果您提前保存了 myWidth = 480 和 myHeight = 800,并以这种方式进行所有绘图,那么当设备旋转时,这些宽度和高度将不再适用。
我认为您的解决方案是监听大小更改的回调并相应地重置您对宽度和高度的想法。如果您直接在视图中绘图,则需要覆盖 View.onSizeChanged()。
It sounds like you are specifying absolute widths and heights for your drawing calls. For instance, if you saved myWidth = 480 and myHeight = 800 early on, and do all your drawing this way, then when the device is rotated, those widths and height no longer apply.
I think the solution for you here is to listen to size changed callbacks and reset your idea of width and height accordingly. If you are drawing directly in a view, you want to override View.onSizeChanged().
您无需交换绘图中的 X 和 Y 坐标。默认情况下,当应用程序旋转时,坐标系也会旋转。
There should be no need for your to swap the X and Y coordinates in your drawing. By default, when apps are rotated, so are the coordinate systems.