Android:如何选择RotateAnimation的旋转方向?

rol*_*ndl 0 animation android rotation android-animation

我目前正在开发一个应用程序,其中我想用箭头指示一个路径(它像指南针一样工作).

为了将箭头移动到好的方向,我使用了这个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);
Run Code Online (Sandbox Code Playgroud)

这里是用户单击按钮"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);
Run Code Online (Sandbox Code Playgroud)

正如您在视频中看到的那样,旋转不会选择更快的方式.在第一个动画中,更快的方法是顺时针旋转(但系统选择逆时针旋转),在第二个动画中,更快的方法是逆时针旋转(但系统选择顺时针旋转).

是否有解决方案或技巧迫使系统选择更快的方式从A点旋转到B点?

Dav*_*ier 6

如果参数toDegrees大于参数,RotationAnimation对象将顺时针旋转fromDegrees.你必须自己处理,实际上,哟不想从去的事实355f8f,但355f368f(这是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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

但我建议你,如果你愿意使用API​​ 14+创建一个应用程序来使用这样的animate()方法View:

test.animate()
  .rotationBy(13f)
  .setDuration(3000)
  .setInterpolator(new LinearInterpolator())
  .start();
Run Code Online (Sandbox Code Playgroud)

rotationBy(offset)方法将旋转您的视图 - 使用中心枢轴 - 按offset度,如果偏移为正,则顺时针旋转,否则逆时针旋转.