/** * 开始动画 * * @param anim * @param index */ protected void startAnim(Animator anim, int index) { anim.setDuration(mDuration).start(); anim.setInterpolator(mInterpolator); }
private void toggle() { if (animator != null) { animator.cancel(); } animator = ObjectAnimator.ofFloat(this, PROGRESS, isPlay ? 1.0F : 0.0F, isPlay ? 0.0F : 1.0F); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { isPlay = !isPlay; } }); animator.setInterpolator(new DecelerateInterpolator()); animator.setDuration(200); animator.start(); }
@Override public void end() { mAnimator.end(); }
private void addLeaf(float progress, RectF leafFlyRect) { if (progress < mNextLeafCreateThreshold) { return; } mNextLeafCreateThreshold += LEAF_CREATE_DURATION_INTERVAL; LeafHolder leafHolder = new LeafHolder(); mLeafHolders.add(leafHolder); Animator leafAnimator = getAnimator(leafHolder, leafFlyRect, progress); leafAnimator.addListener(new AnimEndListener(leafHolder)); leafAnimator.start(); }
@Test public void doubleCanceled() { Animator animator = ViewAnimationUtils.createCircularReveal(view, 10, 10, 10f, 100f); animator.addListener(listener); Robolectric.getForegroundThreadScheduler().pause(); animator.start(); animator.cancel(); animator.cancel(); assertThat(listener.startCount).isEqualTo(1); assertThat(listener.cancelCount).isEqualTo(1); assertThat(listener.endCount).isEqualTo(1); }
protected void addIndicator(int orientation, @DrawableRes int backgroundDrawableId, Animator animator) { if (animator.isRunning()) { animator.end(); animator.cancel(); } View indicator = new View(getContext()); indicator.setBackgroundResource(backgroundDrawableId); addView(indicator, mIndicatorWidth, mIndicatorHeight); LayoutParams lp = (LayoutParams) indicator.getLayoutParams(); if (orientation == HORIZONTAL) { lp.leftMargin = mIndicatorMargin; lp.rightMargin = mIndicatorMargin; } else { lp.topMargin = mIndicatorMargin; lp.bottomMargin = mIndicatorMargin; } indicator.setLayoutParams(lp); animator.setTarget(indicator); animator.start(); }
@Override public void start() { mAnimator.start(); } }
private void circularRevealActivity() { int cx = rootLayout.getWidth() / 2; int cy = rootLayout.getHeight() / 2; float finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight()); // create the animator for this view (the start radius is zero) Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, cx, cy, 0, finalRadius); circularReveal.setDuration(1000); // make the view visible and start the animation rootLayout.setVisibility(View.VISIBLE); circularReveal.start(); }
@Test public void doubleEnded() { Animator animator = ViewAnimationUtils.createCircularReveal(view, 10, 10, 10f, 100f); animator.addListener(listener); Robolectric.getForegroundThreadScheduler().pause(); animator.start(); animator.end(); animator.end(); assertThat(listener.startCount).isEqualTo(1); assertThat(listener.endCount).isEqualTo(1); }
@Test public void delayed() { Animator animator = ViewAnimationUtils.createCircularReveal(view, 10, 10, 10f, 100f); animator.setStartDelay(1000); animator.addListener(listener); animator.start(); assertThat(listener.startCount).isEqualTo(1); assertThat(listener.endCount).isEqualTo(1); }
private Animator createAnimator( final View view, float startTranslationY, float endTranslationY) { view.setTranslationY(startTranslationY); final List<Boolean> ancestralClipping = TransitionUtils.setAncestralClipping(view, false); Animator transition = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, endTranslationY); transition.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { TransitionUtils.restoreAncestralClipping(view, ancestralClipping); } }); return transition; } }
@Override public Animator setDuration(long durationMS) { mAnimator.setDuration(durationMS); return this; }
@Override public void setInterpolator(TimeInterpolator timeInterpolator) { mAnimator.setInterpolator(timeInterpolator); }
@Override public void cancel() { mAnimator.cancel(); }
protected Animator createContentCenteringVerticalAnimation(View newContentText, View newContentIcon) { newContentText.measure(View.MeasureSpec.makeMeasureSpec(mContentCenteredContainer.getWidth(), View.MeasureSpec.AT_MOST), -2); int measuredContentTextHeight = newContentText.getMeasuredHeight(); newContentIcon.measure(-2, -2); int measuredContentIconHeight = newContentIcon.getMeasuredHeight(); int newHeightOfContent = measuredContentIconHeight + measuredContentTextHeight + ((ViewGroup.MarginLayoutParams) mContentTextContainer.getLayoutParams()).topMargin; Animator centerContentAnimation = ObjectAnimator.ofFloat(mContentCenteredContainer, "y", mContentCenteredContainer.getY(), (mContentRootLayout.getHeight() - newHeightOfContent) / 2); centerContentAnimation.setDuration(ANIM_CONTENT_CENTERING_TIME); centerContentAnimation.setInterpolator(new DecelerateInterpolator()); return centerContentAnimation; }
@Test public void neverStartedCanceled() { Animator animator = ViewAnimationUtils.createCircularReveal(view, 10, 10, 10f, 100f); animator.addListener(listener); animator.cancel(); assertThat(listener.startCount).isEqualTo(0); // This behavior changed between L and L MR1. In older versions, onAnimationCancel and // onAnimationEnd would always be called regardless of whether the animation was started. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { assertThat(listener.cancelCount).isEqualTo(0); assertThat(listener.endCount).isEqualTo(0); } else { assertThat(listener.cancelCount).isEqualTo(1); assertThat(listener.endCount).isEqualTo(1); } }