一,代码:
//给按钮增加点击事件:旋转
Button rotateButton = findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
animator.setDuration(2000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
// 动画完成时显示Toast消息
Toast.makeText(AnimActivity.this, "动画开始", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animator animator) {
// 动画完成时显示Toast消息
Toast.makeText(AnimActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animator) {
Toast.makeText(AnimActivity.this, "动画取消", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationRepeat(Animator animator) {
Toast.makeText(AnimActivity.this, "动画重复", Toast.LENGTH_SHORT).show();
}
});
animator.start(); // 启动动画
}
});
二,测试效果:
![]()