From 5918a9b89e2d4489de254140f795a7cee2e79eab Mon Sep 17 00:00:00 2001 From: Luis G Valle Date: Wed, 23 Sep 2015 17:41:24 +0100 Subject: [PATCH] Add reveal animation for API > 21 --- .../MaterialSearchView.java | 49 ++++++++++++------- .../utils/AnimationUtil.java | 41 ++++++++++++++++ 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java index 68d9383..c87f513 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java @@ -483,25 +483,8 @@ public void showSearch(boolean animate) { mSearchSrcTextView.requestFocus(); if (animate) { - AnimationUtil.fadeInView(mSearchLayout, AnimationUtil.ANIMATION_DURATION_MEDIUM, new AnimationUtil.AnimationListener() { - @Override - public boolean onAnimationStart(View view) { - return false; - } - - @Override - public boolean onAnimationEnd(View view) { - if (mSearchViewListener != null) { - mSearchViewListener.onSearchViewShown(); - } - return false; - } + setVisibleWithAnimation(); - @Override - public boolean onAnimationCancel(View view) { - return false; - } - }); } else { mSearchLayout.setVisibility(VISIBLE); if (mSearchViewListener != null) { @@ -511,6 +494,36 @@ public boolean onAnimationCancel(View view) { mIsSearchOpen = true; } + private void setVisibleWithAnimation() { + AnimationUtil.AnimationListener animationListener = new AnimationUtil.AnimationListener() { + @Override + public boolean onAnimationStart(View view) { + return false; + } + + @Override + public boolean onAnimationEnd(View view) { + if (mSearchViewListener != null) { + mSearchViewListener.onSearchViewShown(); + } + return false; + } + + @Override + public boolean onAnimationCancel(View view) { + return false; + } + }; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mSearchLayout.setVisibility(View.VISIBLE); + AnimationUtil.reveal(mSearchTopBar, AnimationUtil.ANIMATION_DURATION_MEDIUM, animationListener); + + } else { + AnimationUtil.fadeInView(mSearchLayout, AnimationUtil.ANIMATION_DURATION_MEDIUM, animationListener); + } + } + /** * Close search view. */ diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java index d07f0ad..16e8807 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java @@ -1,8 +1,13 @@ package com.miguelcatalan.materialsearchview.utils; +import android.animation.Animator; +import android.annotation.TargetApi; +import android.os.Build; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorListener; import android.view.View; +import android.view.ViewAnimationUtils; +import android.view.animation.DecelerateInterpolator; /** * @author Miguel Catalan BaƱuls @@ -75,6 +80,42 @@ public void onAnimationCancel(View view) { ViewCompat.animate(view).alpha(1f).setDuration(duration).setListener(vpListener); } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public static void reveal(final View view, int animationDuration, final AnimationListener listener) { + View viewRoot = view; + int cx = (viewRoot.getRight()); + int cy = (viewRoot.getTop() + viewRoot.getBottom()) / 2; + int finalRadius = Math.max(viewRoot.getWidth(), viewRoot.getHeight()); + + Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius); + viewRoot.setVisibility(View.VISIBLE); + + anim.setDuration(animationDuration); + anim.setInterpolator(new DecelerateInterpolator()); + anim.addListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animation) { + listener.onAnimationStart(view); + } + + @Override + public void onAnimationEnd(Animator animation) { + listener.onAnimationEnd(view); + } + + @Override + public void onAnimationCancel(Animator animation) { + listener.onAnimationCancel(view); + } + + @Override + public void onAnimationRepeat(Animator animation) { + + } + }); + anim.start(); + } + public static void fadeOutView(View view) { fadeOutView(view, ANIMATION_DURATION_SHORT); }