动画监听有三个方法:
onAnimationStart():动画开始的时候触发
onAnimationRepeat():动画重新执行的时候触发
onAnimationEnd():动画结束的时候触发
通过点击按钮实现按钮从有到无,再从无到有:
xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="淡入淡出" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn"/> </LinearLayout>
活动代码:
public class Three extends AppCompatActivity { private Button mButton; private AlphaAnimation mAlpha1 = new AlphaAnimation(1,0); private AlphaAnimation mAlphaA2 = new AlphaAnimation(0,1); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.three); mButton = (Button) findViewById(R.id.btn); //设置当前动画执行的时间 mAlpha1.setDuration(2000); mAlphaA2.setDuration(2000); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mButton.startAnimation(mAlpha1); } }); //动画监听 mAlpha1.setAnimationListener(new Animation.AnimationListener() { //动画开始的时候触发 @Override public void onAnimationStart(Animation animation) { } //动画结束的时候触发 @Override public void onAnimationEnd(Animation animation) { mButton.startAnimation(mAlphaA2); } //动画重新执行的时候触发 @Override public void onAnimationRepeat(Animation animation) { } }); } }