From 6f4e4115d84bae2a893459ecf43e1519e77a039b Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Sat, 6 May 2017 19:58:12 -0400 Subject: [PATCH] Break out rotation animation into animation utils --- .../lightning/animation/AnimationUtils.java | 52 +++++++++++++++++++ .../lightning/fragment/BookmarksFragment.java | 49 ++--------------- 2 files changed, 55 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/animation/AnimationUtils.java diff --git a/app/src/main/java/acr/browser/lightning/animation/AnimationUtils.java b/app/src/main/java/acr/browser/lightning/animation/AnimationUtils.java new file mode 100644 index 0000000..8251448 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/animation/AnimationUtils.java @@ -0,0 +1,52 @@ +package acr.browser.lightning.animation; + +import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; +import android.view.animation.AccelerateDecelerateInterpolator; +import android.view.animation.Animation; +import android.view.animation.Transformation; +import android.widget.ImageView; + +/** + * Animation specific helper code. + */ +public class AnimationUtils { + + /** + * Creates an animation that rotates an {@link ImageView} + * around the Y axis by 180 degrees and changes the image + * resource shown when the view is rotated 90 degrees to the user. + * + * @param imageView the view to rotate. + * @param drawableRes the drawable to set when the view + * is rotated by 90 degrees. + * @return an animation that will change the image shown by the view. + */ + @NonNull + public static Animation createRotationTransitionAnimation(@NonNull final ImageView imageView, + @DrawableRes final int drawableRes) { + Animation animation = new Animation() { + + private boolean mSetFinalDrawable; + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + if (interpolatedTime < 0.5f) { + imageView.setRotationY(90 * interpolatedTime * 2f); + } else { + if (!mSetFinalDrawable) { + mSetFinalDrawable = true; + imageView.setImageResource(drawableRes); + } + imageView.setRotationY((-90) + (90 * (interpolatedTime - 0.5f) * 2f)); + } + } + }; + + animation.setDuration(300); + animation.setInterpolator(new AccelerateDecelerateInterpolator()); + + return animation; + } + +} diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index aaf4a47..c48cffe 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -16,10 +16,7 @@ import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; -import android.view.animation.DecelerateInterpolator; -import android.view.animation.Transformation; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; @@ -40,6 +37,7 @@ import javax.inject.Inject; import acr.browser.lightning.R; import acr.browser.lightning.activity.ReadingActivity; import acr.browser.lightning.activity.TabsManager; +import acr.browser.lightning.animation.AnimationUtils; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.constant.Constants; @@ -240,16 +238,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, } } - @Override - public void onStart() { - super.onStart(); - } - - @Override - public void onStop() { - super.onStop(); - } - public void reinitializePreferences() { Activity activity = getActivity(); if (activity == null) { @@ -293,40 +281,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, resource = R.drawable.ic_action_back; } - final Animation startRotation = new Animation() { - @Override - protected void applyTransformation(float interpolatedTime, Transformation t) { - mBookmarkTitleImage.setRotationY(90 * interpolatedTime); - } - }; - final Animation finishRotation = new Animation() { - @Override - protected void applyTransformation(float interpolatedTime, Transformation t) { - mBookmarkTitleImage.setRotationY((-90) + (90 * interpolatedTime)); - } - }; - startRotation.setAnimationListener(new Animation.AnimationListener() { - @Override - public void onAnimationStart(Animation animation) { - } - - @Override - public void onAnimationEnd(Animation animation) { - mBookmarkTitleImage.setImageResource(resource); - mBookmarkTitleImage.startAnimation(finishRotation); - } - - @Override - public void onAnimationRepeat(Animation animation) { - } - }); - startRotation.setInterpolator(new AccelerateInterpolator()); - finishRotation.setInterpolator(new DecelerateInterpolator()); - startRotation.setDuration(250); - finishRotation.setDuration(250); - if (animate) { - mBookmarkTitleImage.startAnimation(startRotation); + Animation transition = AnimationUtils.createRotationTransitionAnimation(mBookmarkTitleImage, resource); + mBookmarkTitleImage.startAnimation(transition); } else { mBookmarkTitleImage.setImageResource(resource); }