旋转 Android 视图
我想旋转 API 级别 8 的 Android 视图。例如,我想将 EditText 旋转 90 度,以便当用户在左对齐的 EditText 中输入文本时,第一个字符位于底部(旋转 90 度)并且后续字符向上输入。
我首先尝试使用持续时间为 0 的动画,但您仍然看到场旋转。不幸的是,这是不可能的。如果我能找到一种完全隐藏动画的方法,这个方法看起来是最简单的。
然后我尝试在 onDraw 中旋转画布,这对于方形视图非常有用,但对于非方形视图则不太好(而且我不控制 EditText 的尺寸)。我尝试了各种剪切和翻译画布的尝试,但是虽然我可以让光标在文本输入开始时进入视图,但一旦有人开始输入更多内容,它就会做奇怪的事情(通常内容会从视图中消失) 。
我还尝试在 onMeasure 中制作视图正方形,然后在 onDraw 中旋转画布,然后将视图尺寸放回到后续的 onMeasure 中。前两个步骤效果很好。但第三步产生了与上述类似的结果:一切看起来都很好,直到用户开始输入更多文本,此时字段文本做了奇怪的事情(通常消失)。
有没有人能够在没有动画且 API 级别为 8 或更低的情况下成功旋转非方形 Android 视图(例如 EditText)?
I want to rotate an Android View with API level 8. For example, I want to rotate an EditText by 90 degrees so that when the user enters text into a left justified EditText, the first character is at the bottom (rotated 90 degrees) and subsequent characters are entered upwards.
I first tried using an animation with duration of 0, but you still see the field rotate. Unfortunately, this is a non-starter. If I could find a way to hide the animation completely, this method looks to be the simplest.
I then tried rotating the canvas in onDraw which works great for square Views but not so great for ones that aren't square (and I don't control the dimensions of the EditText). I tried various attempts at clipping and translating the canvas, but while I could get the cursor to come into view at the start of text input, it would do weird things once somebody started entering more content (usually the content would disappear out of view).
I also tried making the View square in onMeasure, then rotating the canvas in onDraw, then putting the View dimensions back in a subsequent onMeasure. The first two steps worked great. But the third step produced similar results as described above: things looked ok until the user started entering more text at which point the field text did strange things (usually disappearing).
Has anybody been able to successfully rotate a non-square Android View (an EditText for example) without animation, and with API level 8 or lower?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了相当多的时间尝试在 API 级别 8 上精确地旋转视图,但我认为不可能让它正常工作。仅供参考,尽管 View 类中有一个
setRotation(int angle)
方法,但它在 android 3.2 上也无法正常工作。例如,对于 VideoView 来说,这根本无法正常工作。PS 当时我似乎已经成功地使旋转功能适用于除 VideoView 之外的任何内容。您是否重写了
onTouchEvent
方法?如果没有,您的视图将不会接收触摸事件,因为旋转画布只会使视图绘制旋转,但它仍然会在其旧区域接收触摸事件。您必须手动将旋转矩阵应用于触摸事件坐标,以便将它们偏移到正确的位置。I've spent a pretty good amount of time trying to rotate a view exactly on API level 8 and I think it's impossible to make it work properly. FYI, it doesn't work correctly on android 3.2 either although there's a
setRotation(int angle)
method in the View class. This won't work correctly at all for VideoView for example.P.S back then it seemed that I've managed to make the rotation thing work for anything other than VideoView though. Have you overriden the
onTouchEvent
method? If you have not your view won't be receiving the touch event since rotating the canvas will just make the view draw rotated but it will still receive the touch events in its old area. You have to manually apply rotation matrix to the touch event coordinates in order to offset them to the proper location.