我目前正在开发一种应用程序,在该应用程序中,我想用箭头指示路径(它就像指南针一样)。
为了将箭头移向正确的方向,我使用了RotateAnimation
该类。它工作得很好,但是当初始角度位置远离最终角度位置时,旋转不会选择更快的方式。
在此视频中,您可以看到该行为:https : //youtu.be/vypZni_1s3I
这里是用户单击按钮“ 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);
正如您在视频中看到的那样,旋转不会选择更快的方式。在第一个动画中,更快的方法是顺时针旋转(但系统选择ann逆时针旋转),而在第二个动画中,更快的方法是逆时针旋转(但系统选择顺时针旋转)。
是否有解决方案或技巧可以迫使系统选择更快的方法,以便从点A旋转到点B?
如果参数toDegrees
大于参数,则RotationAnimation对象将顺时针旋转fromDegrees
。您必须处理一个事实,即您实际上不想从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);
但是我建议您,如果您愿意使用API 14+创建应用程序以使用如下animate()
方法View
:
test.animate()
.rotationBy(13f)
.setDuration(3000)
.setInterpolator(new LinearInterpolator())
.start();
该rotationBy(offset)
方法将以中心轴为中心旋转offset
角度,如果偏移为正,则顺时针旋转,否则为逆时针旋转。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句