该项目的的布局文件只有两个控件:ImageView和Button,在这里就不在赘述了,下面我们来看一下activity的实现:
?
public class TwoActivity extends Activity{
? ? private Button button;private ImageView imageView;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? button = (Button)findViewById(R.id.Button);
? ? ? ? imageView = (ImageView)findViewById(R.id.imageView);
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? ObjectAnimator animator = new ObjectAnimator().ofFloat(imageView, "alpha", 0F, 1F);
? ? ? ? ? ? ? ? animator.setDuration(1000);
? ? ? ? ? ? ? ? animator.addListener(new Animator.AnimatorListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onAnimationStart(Animator animator) {
? ? ? ? ? ? ? ? ? ? ? ? //开始动画事件
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(TwoActivity.this, "动画开始", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onAnimationEnd(Animator animator) {
? ? ? ? ? ? ? ? ? ? ? ? //结束动画事件
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(TwoActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onAnimationCancel(Animator animator) {
? ? ? ? ? ? ? ? ? ? ? ? //取消事件
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onAnimationRepeat(Animator animator) {
? ? ? ? ? ? ? ? ? ? ? ? //重复事件
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? animator.start();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
在这里重点为大家介绍一下ObjectAnimator的addListener()方法,可以看到,实现这个方法时需要实现其内部的四个内置方法,我们经常使用onAnimationEnd()方法,用来为用户呈现动画结束后的控制。