这在仿真器上工作正常,但在动画结束之前的真实设备上,图像移动到另一个位置并返回了几毫秒。
函数changeAngle带有值,该值用于旋转png imageView。 OnAnimationEnd在动画结束时触发,用于保存imageView的结束状态。
private void changeAngle(float newAngle)
{
if((int)newAngle == (int)compassImageView.getRotation())
return;
newAngle = newAngle - compassImageView.getRotation();
rotationValue = newAngle;
rotateAnimation = new RotateAnimation(0, rotationValue,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator( new LinearInterpolator() );
int duration = (2000 / 360) * (int)rotationValue;
if(duration < 0)
duration *= -1;
rotateAnimation.setDuration(2000);
compassImageView.startAnimation(rotateAnimation);
rotateAnimation.setAnimationListener(this);
}
// Listener
@Override
public void onAnimationEnd(Animation animation)
{
float x = compassImageView.getRotation()+rotationValue;
compassImageView.setRotation(x);
animation.cancel();
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/compassImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/r_2"/>
</LinearLayout>