動畫監聽有三個方法:
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) { } }); } }