您的位置:首页 > 其它

属性动画设置延迟后,如果在未开始前取消,会执行onAnimationStart方法,特此记录

2016-06-30 14:31 429 查看
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;
}


不知道为什么这么设计,但是源码里面就是这么写的,用的时候注意一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: