属性动画设置延迟后,如果在未开始前取消,会执行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;
}

不知道为什么这么设计,但是源码里面就是这么写的,用的时候注意一下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值