屬性動畫設置延遲後,如果在未開始前取消,會執行onAnimationStart方法,特此記錄

public class AnimatorDelayTestActivity extends AppCompatActivity {



    private View mTextView;
    private ObjectAnimator translationY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animator_delay);
        mTextView = findViewById(R.id.animator_test);
    }

    public void start(View view) {
        translationY = moveView(mTextView, "translationY", 0, mTextView.getHeight(), 5000);
        translationY.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                Log.i("onAnimationUpdate", "start方法");
            }

        });
        translationY.start();
    }

    public void cancel(View view) {
        translationY.cancel();
    }

    private ObjectAnimator moveView(View view, final String attr, float start, float end, int delay) {
        ObjectAnimator move = ObjectAnimator.ofFloat(view, attr, start, end);
        move.setDuration(1000);
        move.setStartDelay(delay);
        return move;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lizheng_ds3.myapplication.MainActivity">

    <TextView
        android:id="@+id/animator_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="動畫測試"
        android:textSize="20dp" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/animator_test"
        android:layout_marginTop="50dp"
        android:onClick="start"
        android:text="開始" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/start"
        android:layout_marginTop="50dp"
        android:onClick="cancel"
        android:text="取消" />
</RelativeLayout>

@Override
public void cancel() {
    // Only cancel if the animation is actually running or has been started and is about
    // to run
    AnimationHandler handler = getOrCreateAnimationHandler();
    if (mPlayingState != STOPPED
            || handler.mPendingAnimations.contains(this)
            || handler.mDelayedAnims.contains(this)) {
        // Only notify listeners if the animator has actually started
        if ((mStarted || mRunning) && mListeners != null) {
            if (!mRunning) {
                // If it's not yet running, then start listeners weren't called. Call them now.
                notifyStartListeners();
            }
            ArrayList<AnimatorListener> tmpListeners =
                    (ArrayList<AnimatorListener>) mListeners.clone();
            for (AnimatorListener listener : tmpListeners) {
                listener.onAnimationCancel(this);
            }
        }
        endAnimation(handler);
    }
}
private void notifyStartListeners() {
    if (mListeners != null && !mStartListenersCalled) {
        ArrayList<AnimatorListener> tmpListeners =
                (ArrayList<AnimatorListener>) mListeners.clone();
        int numListeners = tmpListeners.size();
        for (int i = 0; i < numListeners; ++i) {
            tmpListeners.get(i).onAnimationStart(this);
        }
    }
    mStartListenersCalled = true;
}

不知道爲什麼這麼設計,但是源碼裏面就是這麼寫的,用的時候注意一下


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章