我目前正在开发一个应用程序,在该应用程序中,我想用箭头指示路径(它的工作方式类似于指南针)。
为了将箭头移动到正确的方向,我使用了RotateAnimation类。它工作得很好,但当初始度数位置远离最终度数位置时,旋转不会选择更快的方式。
在此视频中,您可以看到行为:https://youtu.be/vypZni
下面是用户单击按钮"02“时执行的代码:
RotateAnimation rotateAnimation = new RotateAnimation(355f, 8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
test.startAnimation(rotateAnimation);下面是当用户点击按钮"03“时执行的代码:
RotateAnimation rotateAnimation = new RotateAnimation(8f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
test.startAnimation(rotateAnimation);正如您在视频中看到的,旋转并不选择更快的方式。在第一个动画中,更快的方法是顺时针旋转(但系统选择逆时针旋转),在第二个动画中,更快的方法是逆时针旋转(但系统选择顺时针旋转)。
是否有解决方案或技巧来迫使系统选择更快的方式,以便从点A旋转到点B?
发布于 2016-09-01 17:50:21
如果参数toDegrees大于参数fromDegrees,则RotationAnimation对象将顺时针旋转。实际上,你不想从355f转到8f,而是从355f转到368f ( 360度加8度)。
因此,在这里,您将更改您的两段代码,如下所示:
RotateAnimation rotateAnimation = new RotateAnimation(355f, 368f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
test.startAnimation(rotateAnimation);和
RotateAnimation rotateAnimation = new RotateAnimation(368f, 350f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
test.startAnimation(rotateAnimation);但我建议您,如果您愿意使用14+接口创建一个应用程序来使用View的animate()方法,如下所示:
test.animate()
.rotationBy(13f)
.setDuration(3000)
.setInterpolator(new LinearInterpolator())
.start();rotationBy(offset)方法将使用中心枢轴将视图旋转offset度,如果偏移为正数,则顺时针旋转,否则为逆时针旋转。
发布于 2019-09-23 19:15:20
+1 @MaximeClaude的答案可以大大缩短:
//declarations
private static final int HALF_CIRCLE = 180;
private static final int WHOLE_CIRCLE = 360;
private float currentAzimuth = 0f;
public void rotate(float rotationDegrees, View... views) {
float from = currentAzimuth;
float to = rotationDegrees;
float min = Math.min(from, to);
if (Maths.abs(to - from) > HALF_CIRCLE) {
if (min == from) {
from += WHOLE_CIRCLE;
} else {
to += WHOLE_CIRCLE;
}
}
currentAzimuth = rotationDegrees;
Animation anim = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true); //make the arrow stay at its destination (after rotation)
anim.setDuration(200);
for (View view : views) {
view.startAnimation(anim);
}
}并使用android.view.OrientationEventListener设置视图方向:
mOrientationEventListener = new OrientationEventListener(context) {
@Override
public void onOrientationChanged(int orientation) {
rotate(orientation, arrowView);
}
};发布于 2017-10-05 00:52:44
对于后来询问的人,这是一个普遍的案例,灵感来自@David Fournier的答案:
//declarations
private static final int HALF_CIRCLE = 180;
private static final int WHOLE_CIRCLE = 360;
private float azimuth = 0f;
private float currenttAzimuth = 0f;
/**
* "will set rotation from " + currentAzimuth + " to " + azimuth
*/
public void rotate(Double bearing){
if(bearing != null) {
azimuth = (float)bearing.doubleValue();
float min = Math.min(azimuth,currenttAzimuth);
float tempAzimuth = 0.0f;
float tempCurrentAzimuth = 0.0f;
Animation rotateAnim = null;
if(Math.abs(azimuth - currenttAzimuth) > HALF_CIRCLE) {
if(min == currenttAzimuth) {
tempCurrentAzimuth = currenttAzimuth + WHOLE_CIRCLE;
tempAzimuth = azimuth;
}
else {
tempCurrentAzimuth = currenttAzimuth ;
tempAzimuth = azimuth + WHOLE_CIRCLE;
}
rotateAnim = new RotateAnimation(tempCurrentAzimuth, tempAzimuth,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
}
else {
rotateAnim = new RotateAnimation(currenttAzimuth, azimuth,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
}
currenttAzimuth = azimuth;
rotateAnim.setFillAfter(true); //make the arrow stay at its destination (after rotation)
rotateAnim.setDuration(500);
arrowView.startAnimation(rotateAnim);
}
}https://stackoverflow.com/questions/39259907
复制相似问题