List of usage examples for android.animation AnimatorSet addListener
public void addListener(AnimatorListener listener)
From source file:com.android.deskclock.alarms.AlarmActivity.java
private Animator getAlertAnimator(final View source, final int titleResId, final String infoText, final String accessibilityText, final int revealColor, final int backgroundColor) { final ViewGroup containerView = (ViewGroup) findViewById(android.R.id.content); final Rect sourceBounds = new Rect(0, 0, source.getHeight(), source.getWidth()); containerView.offsetDescendantRectToMyCoords(source, sourceBounds); final int centerX = sourceBounds.centerX(); final int centerY = sourceBounds.centerY(); final int xMax = Math.max(centerX, containerView.getWidth() - centerX); final int yMax = Math.max(centerY, containerView.getHeight() - centerY); final float startRadius = Math.max(sourceBounds.width(), sourceBounds.height()) / 2.0f; final float endRadius = (float) Math.sqrt(xMax * xMax + yMax * yMax); final CircleView revealView = new CircleView(this).setCenterX(centerX).setCenterY(centerY) .setFillColor(revealColor);/*from ww w . j a va 2 s . c om*/ containerView.addView(revealView); // TODO: Fade out source icon over the reveal (like LOLLIPOP version). final Animator revealAnimator = ObjectAnimator.ofFloat(revealView, CircleView.RADIUS, startRadius, endRadius); revealAnimator.setDuration(ALERT_REVEAL_DURATION_MILLIS); revealAnimator.setInterpolator(REVEAL_INTERPOLATOR); revealAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animator) { mAlertView.setVisibility(View.VISIBLE); mAlertTitleView.setText(titleResId); if (infoText != null) { mAlertInfoView.setText(infoText); mAlertInfoView.setVisibility(View.VISIBLE); } mContentView.setVisibility(View.GONE); getWindow().setBackgroundDrawable(new ColorDrawable(backgroundColor)); } }); final ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); fadeAnimator.setDuration(ALERT_FADE_DURATION_MILLIS); fadeAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { containerView.removeView(revealView); } }); final AnimatorSet alertAnimator = new AnimatorSet(); alertAnimator.play(revealAnimator).before(fadeAnimator); alertAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animator) { mAlertView.announceForAccessibility(accessibilityText); mHandler.postDelayed(new Runnable() { @Override public void run() { finish(); } }, ALERT_DISMISS_DELAY_MILLIS); } }); return alertAnimator; }
From source file:Main.java
public static void animatePhotoLike(final ImageView likeBg, final ImageView likeIcon) { likeBg.setVisibility(View.VISIBLE); likeIcon.setVisibility(View.VISIBLE); likeBg.setScaleY(0.1f);/*w w w . j a va 2 s .c o m*/ likeBg.setScaleX(0.1f); likeBg.setAlpha(1f); likeIcon.setScaleY(0.1f); likeIcon.setScaleX(0.1f); AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator bgScaleYAnim = ObjectAnimator.ofFloat(likeBg, "scaleY", 0.1f, 1f); bgScaleYAnim.setDuration(200); bgScaleYAnim.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator bgScaleXAnim = ObjectAnimator.ofFloat(likeBg, "scaleX", 0.1f, 1f); bgScaleXAnim.setDuration(200); bgScaleXAnim.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator bgAlphaAnim = ObjectAnimator.ofFloat(likeBg, "alpha", 1f, 0f); bgAlphaAnim.setDuration(200); bgAlphaAnim.setStartDelay(150); bgAlphaAnim.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator imgScaleUpYAnim = ObjectAnimator.ofFloat(likeIcon, "scaleY", 0.1f, 1f); imgScaleUpYAnim.setDuration(300); imgScaleUpYAnim.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator imgScaleUpXAnim = ObjectAnimator.ofFloat(likeIcon, "scaleX", 0.1f, 1f); imgScaleUpXAnim.setDuration(300); imgScaleUpXAnim.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator imgScaleDownYAnim = ObjectAnimator.ofFloat(likeIcon, "scaleY", 1f, 0f); imgScaleDownYAnim.setDuration(300); imgScaleDownYAnim.setInterpolator(ACCELERATE_INTERPOLATOR); ObjectAnimator imgScaleDownXAnim = ObjectAnimator.ofFloat(likeIcon, "scaleX", 1f, 0f); imgScaleDownXAnim.setDuration(300); imgScaleDownXAnim.setInterpolator(ACCELERATE_INTERPOLATOR); animatorSet.playTogether(bgScaleYAnim, bgScaleXAnim, bgAlphaAnim, imgScaleUpYAnim, imgScaleUpXAnim); animatorSet.play(imgScaleDownYAnim).with(imgScaleDownXAnim).after(imgScaleUpYAnim); animatorSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { resetLikeAnimationState(likeBg, likeIcon); } }); animatorSet.start(); }
From source file:com.example.mike.birdalarm.ExpandingListView.java
/** * This method collapses the view that was clicked and animates all the * views around it to close around the collapsing view. There are several * steps required to do this which are outlined below. * <p/>/*w w w . jav a 2s .c o m*/ * 1. Update the layout parameters of the view clicked so as to minimize its * height to the original collapsed (default) state. 2. After invoking a * layout, the listview will shift all the cells so as to display them most * efficiently. Therefore, during the first predraw pass, the listview must * be offset by some amount such that given the custom bound change upon * collapse, all the cells that need to be on the screen after the layout * are rendered by the listview. 3. On the second predraw pass, all the * items are first returned to their original location (before the first * layout). 4. The collapsing view's bounds are animated to what the final * values should be. 5. The bounds above the collapsing view are animated * downwards while the bounds below the collapsing view are animated * upwards. 6. The extra text is faded out as its contents become visible * throughout the animation process. */ public void collapseView(final View view) { final Alarm viewObject = (Alarm) getItemAtPosition(getPositionForView(view)); /* Store the original top and bottom bounds of all the cells. */ final int oldTop = view.getTop(); final int oldBottom = view.getBottom(); final HashMap<View, int[]> oldCoordinates = new HashMap<>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); ViewCompat.setHasTransientState(v, true); oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() }); } /* Update the layout so the extra content becomes invisible. */ view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, viewObject.getCollapsedHeight())); /* Add an onPreDraw listener. */ final ViewTreeObserver observer = getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (!mShouldRemoveObserver) { /* * Same as for expandingView, the parameters for * setSelectionFromTop must be determined such that the * necessary cells of the ListView are rendered and added to * it. */ mShouldRemoveObserver = true; int newTop = view.getTop(); int newBottom = view.getBottom(); int newHeight = newBottom - newTop; int oldHeight = oldBottom - oldTop; int deltaHeight = oldHeight - newHeight; mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false); int currentTop = view.getTop(); int futureTop = oldTop + mTranslate[0]; int firstChildStartTop = getChildAt(0).getTop(); int firstVisiblePosition = getFirstVisiblePosition(); int deltaTop = currentTop - futureTop; int i; int childCount = getChildCount(); for (i = 0; i < childCount; i++) { View v = getChildAt(i); int height = v.getBottom() - Math.max(0, v.getTop()); if (deltaTop - height > 0) { firstVisiblePosition++; deltaTop -= height; } else { break; } } if (i > 0) { firstChildStartTop = 0; } setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop); requestLayout(); return false; } mShouldRemoveObserver = false; observer.removeOnPreDrawListener(this); int yTranslateTop = mTranslate[0]; int yTranslateBottom = mTranslate[1]; int index = indexOfChild(view); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); int[] old = oldCoordinates.get(v); if (old != null) { /* * If the cell was present in the ListView before the * collapse and after the collapse then the bounds are * reset to their old values. */ v.setTop(old[0]); v.setBottom(old[1]); ViewCompat.setHasTransientState(v, false); } else { /* * If the cell is present in the ListView after the * collapse but not before the collapse then the bounds * are calculated using the bottom and top translation * of the collapsing cell. */ int delta = i > index ? yTranslateBottom : -yTranslateTop; v.setTop(v.getTop() + delta); v.setBottom(v.getBottom() + delta); } } final View expandingLayout = view.findViewById(R.id.options_layout); /* * Animates all the cells present on the screen after the * collapse. */ ArrayList<Animator> animations = new ArrayList<>(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); if (v != view) { float diff = i > index ? -yTranslateBottom : yTranslateTop; animations.add(getAnimation(v, diff, diff)); } } /* Adds animation for collapsing the cell that was clicked. */ animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom)); /* Adds an animation for fading out the extra content. */ animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0)); /* Disabled the ListView for the duration of the animation. */ setEnabled(false); setClickable(false); /* * Play all the animations created above together at the same * time. */ AnimatorSet s = new AnimatorSet(); s.playTogether(animations); s.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { expandingLayout.setVisibility(View.GONE); view.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); viewObject.setExpandedState(false); setEnabled(true); setClickable(true); /* * Note that alpha must be set back to 1 in case this * view is reused by a cell that was expanded, but not * yet collapsed, so its state should persist in an * expanded state with the extra content visible. */ expandingLayout.setAlpha(1); } }); s.start(); return true; } }); }
From source file:com.example.mike.birdalarm.ExpandingListView.java
/** * This method expands the view that was clicked and animates all the views * around it to make room for the expanding view. There are several steps * required to do this which are outlined below. * <p/>/*from w w w . j a va 2 s . c om*/ * 1. Store the current top and bottom bounds of each visible item in the * listview. 2. Update the layout parameters of the selected view. In the * context of this method, the view should be originally collapsed and set * to some custom height. The layout parameters are updated so as to wrap * the content of the additional text that is to be displayed. * <p/> * After invoking a layout to take place, the listview will order all the * items such that there is space for each view. This layout will be * independent of what the bounds of the items were prior to the layout so * two pre-draw passes will be made. This is necessary because after the * layout takes place, some views that were visible before the layout may * now be off bounds but a reference to these views is required so the * animation completes as intended. * <p/> * 3. The first predraw pass will set the bounds of all the visible items to * their original location before the layout took place and then force * another layout. Since the bounds of the cells cannot be set directly, the * method setSelectionFromTop can be used to achieve a very similar effect. * 4. The expanding view's bounds are animated to what the final values * should be from the original bounds. 5. The bounds above the expanding * view are animated upwards while the bounds below the expanding view are * animated downwards. 6. The extra text is faded in as its contents become * visible throughout the animation process. * <p/> * It is important to note that the listview is disabled during the * animation because the scrolling behaviour is unpredictable if the bounds * of the items within the listview are not constant during the scroll. */ public void expandView(final View view) { final Alarm viewObject = (Alarm) getItemAtPosition(getPositionForView(view)); // final CardListItem viewObject = (CardListItem) getItemAtPosition(getPositionForView(view)); /* Store the original top and bottom bounds of all the cells. */ final int oldTop = view.getTop(); final int oldBottom = view.getBottom(); final HashMap<View, int[]> oldCoordinates = new HashMap<>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); ViewCompat.setHasTransientState(v, true); oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() }); } /* Update the layout so the extra content becomes visible. */ final View expandingLayout = view.findViewById(R.id.options_layout); expandingLayout.setVisibility(View.VISIBLE); /* * Add an onPreDraw Listener to the listview. onPreDraw will get invoked * after onLayout and onMeasure have run but before anything has been * drawn. This means that the final post layout properties for all the * items have already been determined, but still have not been rendered * onto the screen. */ final ViewTreeObserver observer = getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { /* Determine if this is the first or second pass. */ if (!mShouldRemoveObserver) { mShouldRemoveObserver = true; /* * Calculate what the parameters should be for * setSelectionFromTop. The ListView must be offset in a * way, such that after the animation takes place, all the * cells that remain visible are rendered completely by the * ListView. */ int newTop = view.getTop(); int newBottom = view.getBottom(); int newHeight = newBottom - newTop; int oldHeight = oldBottom - oldTop; int delta = newHeight - oldHeight; mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true); int currentTop = view.getTop(); int futureTop = oldTop - mTranslate[0]; int firstChildStartTop = getChildAt(0).getTop(); int firstVisiblePosition = getFirstVisiblePosition(); int deltaTop = currentTop - futureTop; int i; int childCount = getChildCount(); for (i = 0; i < childCount; i++) { View v = getChildAt(i); int height = v.getBottom() - Math.max(0, v.getTop()); if (deltaTop - height > 0) { firstVisiblePosition++; deltaTop -= height; } else { break; } } if (i > 0) { firstChildStartTop = 0; } setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop); /* * Request another layout to update the layout parameters of * the cells. */ requestLayout(); /* * Return false such that the ListView does not redraw its * contents on this layout but only updates all the * parameters associated with its children. */ return false; } /* * Remove the predraw listener so this method does not keep * getting called. */ mShouldRemoveObserver = false; observer.removeOnPreDrawListener(this); int yTranslateTop = mTranslate[0]; int yTranslateBottom = mTranslate[1]; ArrayList<Animator> animations = new ArrayList<>(); int index = indexOfChild(view); /* * Loop through all the views that were on the screen before the * cell was expanded. Some cells will still be children of the * ListView while others will not. The cells that remain * children of the ListView simply have their bounds animated * appropriately. The cells that are no longer children of the * ListView also have their bounds animated, but must also be * added to a list of views which will be drawn in dispatchDraw. */ for (View v : oldCoordinates.keySet()) { int[] old = oldCoordinates.get(v); v.setTop(old[0]); v.setBottom(old[1]); if (v.getParent() == null) { mViewsToDraw.add(v); int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom; animations.add(getAnimation(v, delta, delta)); } else { int i = indexOfChild(v); if (v != view) { int delta = i > index ? yTranslateBottom : -yTranslateTop; animations.add(getAnimation(v, delta, delta)); } ViewCompat.setHasTransientState(v, false); } } /* Adds animation for expanding the cell that was clicked. */ animations.add(getAnimation(view, -yTranslateTop, yTranslateBottom)); /* Adds an animation for fading in the extra content. */ animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.options_layout), View.ALPHA, 0, 1)); /* Disabled the ListView for the duration of the animation. */ setEnabled(false); setClickable(false); /* * Play all the animations created above together at the same * time. */ AnimatorSet s = new AnimatorSet(); s.playTogether(animations); s.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { viewObject.setExpandedState(true); setEnabled(true); setClickable(true); if (mViewsToDraw.size() > 0) { for (View v : mViewsToDraw) { ViewCompat.setHasTransientState(v, false); } } mViewsToDraw.clear(); } }); s.start(); return true; } }); }
From source file:com.onyx.deskclock.deskclock.alarms.AlarmActivity.java
private Animator getAlertAnimator(final View source, final int titleResId, final String infoText, final String accessibilityText, final int revealColor, final int backgroundColor) { final ViewGroup containerView = (ViewGroup) findViewById(android.R.id.content); final Rect sourceBounds = new Rect(0, 0, source.getHeight(), source.getWidth()); containerView.offsetDescendantRectToMyCoords(source, sourceBounds); final int centerX = sourceBounds.centerX(); final int centerY = sourceBounds.centerY(); final int xMax = Math.max(centerX, containerView.getWidth() - centerX); final int yMax = Math.max(centerY, containerView.getHeight() - centerY); final float startRadius = Math.max(sourceBounds.width(), sourceBounds.height()) / 2.0f; final float endRadius = (float) Math.sqrt(xMax * xMax + yMax * yMax); final CircleView revealView = new CircleView(this).setCenterX(centerX).setCenterY(centerY) .setFillColor(revealColor);//from w w w. j av a 2 s. c o m containerView.addView(revealView); // TODO: Fade out source icon over the reveal (like LOLLIPOP version). final Animator revealAnimator = ObjectAnimator.ofFloat(revealView, CircleView.RADIUS, startRadius, endRadius); revealAnimator.setDuration(ALERT_REVEAL_DURATION_MILLIS); revealAnimator.setInterpolator(REVEAL_INTERPOLATOR); revealAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animator) { mAlertView.setVisibility(View.VISIBLE); mAlertTitleView.setText(titleResId); if (infoText != null) { mAlertInfoView.setText(infoText); mAlertInfoView.setVisibility(View.VISIBLE); } mContentView.setVisibility(View.GONE); getWindow().setBackgroundDrawable(new ColorDrawable(backgroundColor)); } }); final ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); fadeAnimator.setDuration(ALERT_FADE_DURATION_MILLIS); fadeAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { containerView.removeView(revealView); } }); final AnimatorSet alertAnimator = new AnimatorSet(); alertAnimator.play(revealAnimator).before(fadeAnimator); alertAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animator) { if (Build.VERSION.SDK_INT >= 16) { mAlertView.announceForAccessibility(accessibilityText); } mHandler.postDelayed(new Runnable() { @Override public void run() { finish(); } }, ALERT_DISMISS_DELAY_MILLIS); } }); return alertAnimator; }
From source file:me.zchang.onchart.ui.MainActivity.java
private void popupThisWeek(int weekNum) { popupWeekText.setText(String.format(getString(R.string.weekday_week), weekNum)); popupWeekText.setVisibility(View.VISIBLE); popupWeekText.setScaleX(.3f);/*from w w w .j av a 2s . co m*/ popupWeekText.setScaleY(.3f); popupWeekText.setAlpha(.7f); Animator scaleAnimator = ObjectAnimator.ofFloat(popupWeekText, "scaleX", 1f); Animator scaleAnimator2 = ObjectAnimator.ofFloat(popupWeekText, "scaleY", 1f); scaleAnimator.setDuration(150); scaleAnimator2.setDuration(150); Animator alphaAnimator = ObjectAnimator.ofFloat(popupWeekText, "alpha", 0f); alphaAnimator.setStartDelay(120); alphaAnimator.setDuration(500); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(scaleAnimator).with(scaleAnimator2).with(alphaAnimator); animatorSet.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { popupWeekText.setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); animatorSet.start(); }
From source file:arun.com.chromer.webheads.ui.views.BaseWebHead.java
/** * Opposite of {@link #getRevealAnimator(int)}. Reveal goes from max scale to 0 appearing to be * revealing in./*from w ww. j a v a2s . co m*/ * * @param newWebHeadColor New themeColor of reveal * @param start Runnable to run on start * @param end Runnable to run on end */ void revealInAnimation(@ColorInt final int newWebHeadColor, @NonNull final Runnable start, @NonNull final Runnable end) { if (revealView == null || circleBg == null) { start.run(); end.run(); } revealView.clearAnimation(); revealView.setColor(circleBg.getColor()); revealView.setScaleX(1f); revealView.setScaleY(1f); revealView.setAlpha(1f); circleBg.setColor(newWebHeadColor); final AnimatorSet animator = new AnimatorSet(); animator.playTogether(ObjectAnimator.ofFloat(revealView, "scaleX", 0f), ObjectAnimator.ofFloat(revealView, "scaleY", 0f)); revealView.setLayerType(LAYER_TYPE_HARDWARE, null); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { start.run(); } @Override public void onAnimationEnd(Animator animation) { webHeadColor = newWebHeadColor; indicator.setTextColor(getForegroundWhiteOrBlack(newWebHeadColor)); revealView.setLayerType(LAYER_TYPE_NONE, null); revealView.setScaleX(0f); revealView.setScaleY(0f); end.run(); } }); animator.setInterpolator(new LinearOutSlowInInterpolator()); animator.setDuration(400); animator.setStartDelay(100); animator.start(); }
From source file:android.support.transition.ChangeBounds.java
@Override @Nullable/*from w ww . ja va 2 s. c om*/ public Animator createAnimator(@NonNull final ViewGroup sceneRoot, @Nullable TransitionValues startValues, @Nullable TransitionValues endValues) { if (startValues == null || endValues == null) { return null; } Map<String, Object> startParentVals = startValues.values; Map<String, Object> endParentVals = endValues.values; ViewGroup startParent = (ViewGroup) startParentVals.get(PROPNAME_PARENT); ViewGroup endParent = (ViewGroup) endParentVals.get(PROPNAME_PARENT); if (startParent == null || endParent == null) { return null; } final View view = endValues.view; if (parentMatches(startParent, endParent)) { Rect startBounds = (Rect) startValues.values.get(PROPNAME_BOUNDS); Rect endBounds = (Rect) endValues.values.get(PROPNAME_BOUNDS); final int startLeft = startBounds.left; final int endLeft = endBounds.left; final int startTop = startBounds.top; final int endTop = endBounds.top; final int startRight = startBounds.right; final int endRight = endBounds.right; final int startBottom = startBounds.bottom; final int endBottom = endBounds.bottom; final int startWidth = startRight - startLeft; final int startHeight = startBottom - startTop; final int endWidth = endRight - endLeft; final int endHeight = endBottom - endTop; Rect startClip = (Rect) startValues.values.get(PROPNAME_CLIP); Rect endClip = (Rect) endValues.values.get(PROPNAME_CLIP); int numChanges = 0; if ((startWidth != 0 && startHeight != 0) || (endWidth != 0 && endHeight != 0)) { if (startLeft != endLeft || startTop != endTop) ++numChanges; if (startRight != endRight || startBottom != endBottom) ++numChanges; } if ((startClip != null && !startClip.equals(endClip)) || (startClip == null && endClip != null)) { ++numChanges; } if (numChanges > 0) { Animator anim; if (!mResizeClip) { ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startRight, startBottom); if (numChanges == 2) { if (startWidth == endWidth && startHeight == endHeight) { Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop); anim = ObjectAnimatorUtils.ofPointF(view, POSITION_PROPERTY, topLeftPath); } else { final ViewBounds viewBounds = new ViewBounds(view); Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop); ObjectAnimator topLeftAnimator = ObjectAnimatorUtils.ofPointF(viewBounds, TOP_LEFT_PROPERTY, topLeftPath); Path bottomRightPath = getPathMotion().getPath(startRight, startBottom, endRight, endBottom); ObjectAnimator bottomRightAnimator = ObjectAnimatorUtils.ofPointF(viewBounds, BOTTOM_RIGHT_PROPERTY, bottomRightPath); AnimatorSet set = new AnimatorSet(); set.playTogether(topLeftAnimator, bottomRightAnimator); anim = set; set.addListener(new AnimatorListenerAdapter() { // We need a strong reference to viewBounds until the // animator ends (The ObjectAnimator holds only a weak reference). @SuppressWarnings("unused") private ViewBounds mViewBounds = viewBounds; }); } } else if (startLeft != endLeft || startTop != endTop) { Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop); anim = ObjectAnimatorUtils.ofPointF(view, TOP_LEFT_ONLY_PROPERTY, topLeftPath); } else { Path bottomRight = getPathMotion().getPath(startRight, startBottom, endRight, endBottom); anim = ObjectAnimatorUtils.ofPointF(view, BOTTOM_RIGHT_ONLY_PROPERTY, bottomRight); } } else { int maxWidth = Math.max(startWidth, endWidth); int maxHeight = Math.max(startHeight, endHeight); ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startLeft + maxWidth, startTop + maxHeight); ObjectAnimator positionAnimator = null; if (startLeft != endLeft || startTop != endTop) { Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop); positionAnimator = ObjectAnimatorUtils.ofPointF(view, POSITION_PROPERTY, topLeftPath); } final Rect finalClip = endClip; if (startClip == null) { startClip = new Rect(0, 0, startWidth, startHeight); } if (endClip == null) { endClip = new Rect(0, 0, endWidth, endHeight); } ObjectAnimator clipAnimator = null; if (!startClip.equals(endClip)) { ViewCompat.setClipBounds(view, startClip); clipAnimator = ObjectAnimator.ofObject(view, "clipBounds", sRectEvaluator, startClip, endClip); clipAnimator.addListener(new AnimatorListenerAdapter() { private boolean mIsCanceled; @Override public void onAnimationCancel(Animator animation) { mIsCanceled = true; } @Override public void onAnimationEnd(Animator animation) { if (!mIsCanceled) { ViewCompat.setClipBounds(view, finalClip); ViewUtils.setLeftTopRightBottom(view, endLeft, endTop, endRight, endBottom); } } }); } anim = TransitionUtils.mergeAnimators(positionAnimator, clipAnimator); } if (view.getParent() instanceof ViewGroup) { final ViewGroup parent = (ViewGroup) view.getParent(); ViewGroupUtils.suppressLayout(parent, true); TransitionListener transitionListener = new TransitionListenerAdapter() { boolean mCanceled = false; @Override public void onTransitionCancel(@NonNull Transition transition) { ViewGroupUtils.suppressLayout(parent, false); mCanceled = true; } @Override public void onTransitionEnd(@NonNull Transition transition) { if (!mCanceled) { ViewGroupUtils.suppressLayout(parent, false); } transition.removeListener(this); } @Override public void onTransitionPause(@NonNull Transition transition) { ViewGroupUtils.suppressLayout(parent, false); } @Override public void onTransitionResume(@NonNull Transition transition) { ViewGroupUtils.suppressLayout(parent, true); } }; addListener(transitionListener); } return anim; } } else { int startX = (Integer) startValues.values.get(PROPNAME_WINDOW_X); int startY = (Integer) startValues.values.get(PROPNAME_WINDOW_Y); int endX = (Integer) endValues.values.get(PROPNAME_WINDOW_X); int endY = (Integer) endValues.values.get(PROPNAME_WINDOW_Y); // TODO: also handle size changes: check bounds and animate size changes if (startX != endX || startY != endY) { sceneRoot.getLocationInWindow(mTempLocation); Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); @SuppressWarnings("deprecation") final BitmapDrawable drawable = new BitmapDrawable(bitmap); final float transitionAlpha = ViewUtils.getTransitionAlpha(view); ViewUtils.setTransitionAlpha(view, 0); ViewUtils.getOverlay(sceneRoot).add(drawable); Path topLeftPath = getPathMotion().getPath(startX - mTempLocation[0], startY - mTempLocation[1], endX - mTempLocation[0], endY - mTempLocation[1]); PropertyValuesHolder origin = PropertyValuesHolderUtils.ofPointF(DRAWABLE_ORIGIN_PROPERTY, topLeftPath); ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(drawable, origin); anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { ViewUtils.getOverlay(sceneRoot).remove(drawable); ViewUtils.setTransitionAlpha(view, transitionAlpha); } }); return anim; } } return null; }
From source file:com.arlib.floatingsearchview.util.view.MenuView.java
/** * Shows all the menu items that were hidden by hideIfRoomItems(boolean withAnim) * * @param withAnim// w w w. j a v a 2 s . c o m */ public void showIfRoomItems(boolean withAnim) { if (mMenu == -1) return; cancelChildAnimListAndClear(); if (mMenuItems.isEmpty()) return; anims = new ArrayList<>(); for (int i = 0; i < getChildCount(); i++) { final View currentView = getChildAt(i); if (i < mActionItems.size()) { ImageView action = (ImageView) currentView; final MenuItem actionItem = mActionItems.get(i); action.setImageDrawable(Util.setIconColor(actionItem.getIcon(), mActionIconColor)); action.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mMenuCallback != null) mMenuCallback.onMenuItemSelected(mMenuBuilder, actionItem); } }); } //todo go over logic int animDuration = withAnim ? SHOW_IF_ROOM_ITEMS_ANIM_DURATION : 0; Interpolator interpolator = new DecelerateInterpolator(); //todo check logic if (i > mActionShowAlwaysItems.size() - 1) interpolator = new LinearInterpolator(); currentView.setClickable(true); anims.add(ViewPropertyObjectAnimator.animate(currentView).addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { currentView.setTranslationX(0); } }).setInterpolator(interpolator).setDuration(animDuration).translationX(0).get()); anims.add(ViewPropertyObjectAnimator.animate(currentView).addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { currentView.setScaleX(1.0f); } }).setInterpolator(interpolator).setDuration(animDuration).scaleX(1.0f).get()); anims.add(ViewPropertyObjectAnimator.animate(currentView).addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { currentView.setScaleY(1.0f); } }).setInterpolator(interpolator).setDuration(animDuration).scaleY(1.0f).get()); anims.add(ViewPropertyObjectAnimator.animate(currentView).addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { currentView.setAlpha(1.0f); } }).setInterpolator(interpolator).setDuration(animDuration).alpha(1.0f).get()); } AnimatorSet animSet = new AnimatorSet(); //temporary, from laziness if (!withAnim) animSet.setDuration(0); animSet.playTogether(anims.toArray(new ObjectAnimator[anims.size()])); animSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { if (mOnVisibleWidthChanged != null) mOnVisibleWidthChanged.onVisibleWidthChanged( (getChildCount() * (int) ACTION_DIMENSION_PX) - (mHasOverflow ? Util.dpToPx(8) : 0)); } }); animSet.start(); }
From source file:com.jforce.chapelhillnextbus.ExpandingListView.java
/** * This method collapses the view that was clicked and animates all the * views around it to close around the collapsing view. There are several * steps required to do this which are outlined below. * <p/>//from w w w.j a v a 2 s.co m * 1. Update the layout parameters of the view clicked so as to minimize its * height to the original collapsed (default) state. 2. After invoking a * layout, the listview will shift all the cells so as to display them most * efficiently. Therefore, during the first predraw pass, the listview must * be offset by some amount such that given the custom bound change upon * collapse, all the cells that need to be on the screen after the layout * are rendered by the listview. 3. On the second predraw pass, all the * items are first returned to their original location (before the first * layout). 4. The collapsing view's bounds are animated to what the final * values should be. 5. The bounds above the collapsing view are animated * downwards while the bounds below the collapsing view are animated * upwards. 6. The extra text is faded out as its contents become visible * throughout the animation process. */ private void collapseView(final View view) { final ExpandableListItem viewObject = (ExpandableListItem) getItemAtPosition(getPositionForView(view)); /* Store the original top and bottom bounds of all the cells. */ final int oldTop = view.getTop(); final int oldBottom = view.getBottom(); final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); ViewCompat.setHasTransientState(v, true); oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() }); } /* Update the layout so the extra content becomes invisible. */ view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, viewObject.getCollapsedHeight())); /* Add an onPreDraw listener. */ final ViewTreeObserver observer = getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (!mShouldRemoveObserver) { /* * Same as for expandingView, the parameters for * setSelectionFromTop must be determined such that the * necessary cells of the ListView are rendered and added to * it. */ mShouldRemoveObserver = true; int newTop = view.getTop(); int newBottom = view.getBottom(); int newHeight = newBottom - newTop; int oldHeight = oldBottom - oldTop; int deltaHeight = oldHeight - newHeight; mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false); int currentTop = view.getTop(); int futureTop = oldTop + mTranslate[0]; int firstChildStartTop = getChildAt(0).getTop(); int firstVisiblePosition = getFirstVisiblePosition(); int deltaTop = currentTop - futureTop; int i; int childCount = getChildCount(); for (i = 0; i < childCount; i++) { View v = getChildAt(i); int height = v.getBottom() - Math.max(0, v.getTop()); if (deltaTop - height > 0) { firstVisiblePosition++; deltaTop -= height; } else { break; } } if (i > 0) { firstChildStartTop = 0; } setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop); requestLayout(); return false; } mShouldRemoveObserver = false; observer.removeOnPreDrawListener(this); int yTranslateTop = mTranslate[0]; int yTranslateBottom = mTranslate[1]; int index = indexOfChild(view); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); int[] old = oldCoordinates.get(v); if (old != null) { /* * If the cell was present in the ListView before the * collapse and after the collapse then the bounds are * reset to their old values. */ v.setTop(old[0]); v.setBottom(old[1]); ViewCompat.setHasTransientState(v, false); } else { /* * If the cell is present in the ListView after the * collapse but not before the collapse then the bounds * are calculated using the bottom and top translation * of the collapsing cell. */ int delta = i > index ? yTranslateBottom : -yTranslateTop; v.setTop(v.getTop() + delta); v.setBottom(v.getBottom() + delta); } } final View expandingLayout = view.findViewById(R.id.expanding_layout); /* * Animates all the cells present on the screen after the * collapse. */ ArrayList<Animator> animations = new ArrayList<Animator>(); for (int i = 0; i < childCount; i++) { View v = getChildAt(i); if (v != view) { float diff = i > index ? -yTranslateBottom : yTranslateTop; animations.add(getAnimation(v, diff, diff)); } } /* Adds animation for collapsing the cell that was clicked. */ animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom)); /* Adds an animation for fading out the extra content. */ animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0)); /* Disabled the ListView for the duration of the animation. */ setEnabled(false); setClickable(false); /* * Play all the animations created above together at the same * time. */ AnimatorSet s = new AnimatorSet(); s.playTogether(animations); s.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { expandingLayout.setVisibility(View.GONE); view.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); viewObject.setExpanded(false); setEnabled(true); setClickable(true); /* * Note that alpha must be set back to 1 in case this * view is reused by a cell that was expanded, but not * yet collapsed, so its state should persist in an * expanded state with the extra content visible. */ expandingLayout.setAlpha(1); } }); s.start(); return true; } }); }