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;
}
}
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">
android:id="@+id/animator_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画测试"
android:textSize="20dp" />
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="开始" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/start"
android:layout_marginTop="50dp"
android:onClick="cancel"
android:text="取消" />
@Overridepublic voidcancel() {
// Only cancel if the animation is actually running or has been started and is about// to runAnimationHandler handler = getOrCreateAnimationHandler();if(mPlayingState!= STOPPED|| handler.mPendingAnimations.contains(this)
|| handler.mDelayedAnims.contains(this)) {
// Only notify listeners if the animator has actually startedif((mStarted|| mRunning) && mListeners != null) {
if(!mRunning) {
// If it's not yet running, then start listeners weren't called. Call them now.notifyStartListeners();}
ArrayList tmpListeners =
(ArrayList) mListeners.clone();for(AnimatorListener listener : tmpListeners) {
listener.onAnimationCancel(this);}
}
endAnimation(handler);}
}
private voidnotifyStartListeners() {
if(mListeners != null&& !mStartListenersCalled) {
ArrayList tmpListeners =
(ArrayList) mListeners.clone();intnumListeners = tmpListeners.size();for(inti = 0;i < numListeners;++i) {
tmpListeners.get(i).onAnimationStart(this);}
}
mStartListenersCalled= true;}
不知道为什么这么设计,但是源码里面就是这么写的,用的时候注意一下