简体   繁体   English

像Android API一样的AnglePicker

[英]AnglePicker Like Android API19 TimePicker

I want to create an "Angle Picker" like the Android TimerPicker in Alarm Clock since API Level 19 KitKat. 自API Level 19 KitKat之后,我想在闹钟中创建一个类似于Android TimerPicker的“角度选择器”。

I only want to pick an angle between 0 and 360 degrees. 我只想选择0到360度之间的角度。 Anybody know how to create something like this? 有人知道如何创建这样的东西吗?

Android TimePicker in Clock App 时钟应用中的Android TimePicker

Create your own component: 创建自己的组件:

public class AnglePicker extends View { ... }

Override the onDraw(Canvas canvas) method. 重写onDraw(Canvas canvas)方法。 You should draw a circle, a few texts (0º, 90º, º180 and 270º for example)... 您应该画一个圆,一些文字(例如0º,90º,º180和270º)...
You only need a few calls to canvas.drawCircle() and canvas.drawText() . 您只需要几个调用canvas.drawCircle()canvas.drawText() The most "difficult" part would probably be calculating the text positions. 最“困难”的部分可能是计算文本位置。 But it's not a big problem, just a little bit of sin and cos . 但这不是一个大问题,只有一点sincos

Then override onTouchEvent(MotionEvent event) too, so you can listen to touch events and repaint the component depending on your needs (drawing a line between center and the selected number, highlighting the closest number with an additional drawCircle ...). 然后也重写onTouchEvent(MotionEvent event) ,以便您可以听触摸事件并根据需要重新绘制组件(在中心和所选数字之间画一条线,并用附加的drawCircle突出显示最接近的数字...)。
Basically when the user touches your view, calculate the (x,y) position: 基本上,当用户触摸您的视图时,计算(x,y)位置:

Point pt = new Point( (int)event.getX(), (int)event.getY() );

And get the angle with double theta = Math.atan2(pt.x-center.x, center.y-pt.y); 并使用double theta = Math.atan2(pt.x-center.x, center.y-pt.y);获得角度double theta = Math.atan2(pt.x-center.x, center.y-pt.y); . Translate it into degrees or whatever you need, cause theta is expressed in radians. 将其转换为度数或任何您需要的度数,因为theta以弧度表示。

Finally, add a public double getAngle() so other components can read the selected angle. 最后,添加一个public double getAngle()以便其他组件可以读取所选角度。
Seems complicated, but it's not. 看起来很复杂,但事实并非如此。 You can search for "android custom view" if you want some code to start with. 如果您想以某些代码开头,则可以搜索“ android自定义视图”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM