我在Android应用中使用无限旋转动画。如果我在旋转的cancelAnimation()
上调用ImageView
,图像会立即进入动画的开始位置,而不会将动画最终确定到该位置。有没有办法执行该操作,以便用户在不中断动画的情况下获得更流畅的体验?
答案 0 :(得分:0)
public class MainActivity extends Activity {
ImageView img;
Button btn, btn2;
Animation myAnim;
boolean flag=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
myAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
img.startAnimation(myAnim);
myAnim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
System.out.println("onAnimationStart()");
}
public void onAnimationRepeat(Animation animation) {
if(flag)
System.out.println("onAnimationRepeat()");
else
myAnim.cancel();
}
public void onAnimationEnd(Animation animation) {
System.out.println("onAnimationEnd()");
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flag=false;
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
img.startAnimation(myAnim);
flag=true;
}
});
}
}