整体很简单,直接上代码吧。activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context="com.example.administrator.animator_demo2.MainActivity">
 7 
 8     <Button
 9         android:id="@+id/btn_click"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_centerInParent="true"
13         android:text="Click" />
14 
15 </RelativeLayout>

然后是JAVA代码,MainActivity.java:

 1 package com.example.administrator.animator_demo2;
 2 
 3 import android.animation.Animator;
 4 import android.animation.AnimatorListenerAdapter;
 5 import android.animation.ObjectAnimator;
 6 import android.os.Bundle;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.Toast;
11 
12 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
13 
14     private Button btn_click;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         initView();
21     }
22 
23     private void initView() {
24         btn_click = (Button) findViewById(R.id.btn_click);
25         btn_click.setOnClickListener(this);
26     }
27 
28     @Override
29     public void onClick(View v) {
30         switch (v.getId()) {
31             case R.id.btn_click:
32 //                startAnimator1();
33                 /**
34                  * startAnimator1()方法当中,一下就重写了四个监听方法,在因为有的时候,我们
35                  * 我们并不需要监听那么多,只需要其中的onAnimationEnd()方法。
36                  * 那这时我们会选择用startAnimator2()的方式。(两种执行效果相同)
37                  * */
38                 startAnimator2();
39                 break;
40         }
41     }
42 
43     private void startAnimator1() {
44         //透明动画
45         ObjectAnimator animator = ObjectAnimator.ofFloat(btn_click, "alpha", 0F, 1F);
46         animator.setDuration(1000);
47         //为动画设置监听
48         animator.addListener(new Animator.AnimatorListener() {
49             @Override
50             public void onAnimationStart(Animator animation) {
51 
52             }
53 
54             @Override
55             public void onAnimationEnd(Animator animation) {
56                 /**
57                  * 大部分情况下,我们都要监听的是这个,在动画结束后,执行什么操作
58                  * 这里,我们只弹出一个Toast。
59                  * */
60                 Toast.makeText(MainActivity.this, "动画执行结束", Toast.LENGTH_SHORT).show();
61             }
62 
63             @Override
64             public void onAnimationCancel(Animator animation) {
65 
66             }
67 
68             @Override
69             public void onAnimationRepeat(Animator animation) {
70 
71             }
72         });
73         animator.start();
74     }
75 
76     private void startAnimator2() {
77         //透明动画
78         ObjectAnimator animator = ObjectAnimator.ofFloat(btn_click, "alpha", 0F, 1F);
79         animator.setDuration(1000);
80         //为动画设置监听
81         animator.addListener(new AnimatorListenerAdapter() {
82             @Override
83             public void onAnimationEnd(Animator animation) {
84                 super.onAnimationEnd(animation);
85                 Toast.makeText(MainActivity.this, "动画执行结束", Toast.LENGTH_SHORT).show();
86             }
87         });
88         animator.start();
89     }
90 }