From b30a8f3ff95e2139ccd86a836201e9b67ad67d4b Mon Sep 17 00:00:00 2001 From: tehcneko <7764726+tehcneko@users.noreply.github.com> Date: Fri, 28 May 2021 18:56:35 +0800 Subject: [PATCH] Update insets animation api --- .../ehviewer/WindowInsetsAnimationHelper.java | 84 +++++++++++++++++++ .../ehviewer/ui/scene/FavoritesScene.java | 57 ++----------- .../ui/scene/GalleryCommentsScene.java | 80 ++---------------- .../ehviewer/ui/scene/GalleryListScene.java | 82 ++---------------- 4 files changed, 107 insertions(+), 196 deletions(-) create mode 100644 app/src/main/java/com/hippo/ehviewer/WindowInsetsAnimationHelper.java diff --git a/app/src/main/java/com/hippo/ehviewer/WindowInsetsAnimationHelper.java b/app/src/main/java/com/hippo/ehviewer/WindowInsetsAnimationHelper.java new file mode 100644 index 000000000..3eee26637 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/WindowInsetsAnimationHelper.java @@ -0,0 +1,84 @@ +package com.hippo.ehviewer; + +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.core.view.WindowInsetsAnimationCompat; +import androidx.core.view.WindowInsetsCompat; + +import com.hippo.yorozuya.MathUtils; + +import java.util.HashMap; +import java.util.List; + +public class WindowInsetsAnimationHelper extends WindowInsetsAnimationCompat.Callback { + private final View[] views; + private final HashMap startPaddings = new HashMap<>(); + private final HashMap endPaddings = new HashMap<>(); + + public WindowInsetsAnimationHelper(int dispatchMode, View... views) { + super(dispatchMode); + this.views = views; + } + + WindowInsetsAnimationCompat animation; + + @Override + public void onPrepare(@NonNull WindowInsetsAnimationCompat animation) { + super.onPrepare(animation); + this.animation = animation; + for (View view : views) { + if (view == null) { + continue; + } + startPaddings.put(view, view.getPaddingBottom()); + } + } + + @NonNull + @Override + public WindowInsetsAnimationCompat.BoundsCompat onStart(@NonNull WindowInsetsAnimationCompat animation, @NonNull WindowInsetsAnimationCompat.BoundsCompat bounds) { + this.animation = animation; + for (View view : views) { + if (view == null) { + continue; + } + endPaddings.put(view, view.getPaddingBottom()); + int startPadding = startPaddings.containsKey(view) ? startPaddings.get(view) : 0; + view.setTranslationY(-(startPadding - view.getPaddingBottom())); + } + return bounds; + } + + @NonNull + @Override + public WindowInsetsCompat onProgress(@NonNull WindowInsetsCompat insets, @NonNull List runningAnimations) { + if (animation == null) { + return insets; + } + for (View view : views) { + if (view == null) { + continue; + } + int startPadding = startPaddings.containsKey(view) ? startPaddings.get(view) : 0; + int endPadding = endPaddings.containsKey(view) ? endPaddings.get(view) : 0; + int offset = MathUtils.lerp(-(startPadding - endPadding), 0, animation.getInterpolatedFraction()); + view.setTranslationY(offset); + } + return insets; + } + + @Override + public void onEnd(@NonNull WindowInsetsAnimationCompat animation) { + super.onEnd(animation); + startPaddings.clear(); + endPaddings.clear(); + this.animation = null; + for (View view : views) { + if (view == null) { + continue; + } + view.setTranslationY(0); + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java index 443e963e7..f607b7d67 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java @@ -23,7 +23,6 @@ import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.Point; import android.graphics.Rect; -import android.os.Build; import android.os.Bundle; import android.text.InputType; import android.text.TextUtils; @@ -33,8 +32,6 @@ import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.WindowInsets; -import android.view.WindowInsetsAnimation; import android.widget.TextView; import androidx.annotation.NonNull; @@ -42,6 +39,8 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; import androidx.core.content.ContextCompat; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsAnimationCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -60,6 +59,7 @@ import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.R; import com.hippo.ehviewer.Settings; +import com.hippo.ehviewer.WindowInsetsAnimationHelper; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; @@ -81,7 +81,6 @@ import com.hippo.widget.FabLayout; import com.hippo.widget.SearchBarMover; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; -import com.hippo.yorozuya.MathUtils; import com.hippo.yorozuya.ObjectUtils; import com.hippo.yorozuya.SimpleHandler; import com.hippo.yorozuya.ViewUtils; @@ -259,52 +258,10 @@ public class FavoritesScene extends BaseScene implements FastScroller fastScroller = mContentLayout.getFastScroller(); mSearchBar = (SearchBar) ViewUtils.$$(view, R.id.search_bar); mFabLayout = (FabLayout) ViewUtils.$$(view, R.id.fab_layout); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - view.setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(WindowInsetsAnimation.Callback.DISPATCH_MODE_STOP) { - int startBottomFabLayout = 0; - int endBottomFabLayout = 0; - WindowInsetsAnimation animation; - - @Override - public void onPrepare(@NonNull WindowInsetsAnimation animation) { - this.animation = animation; - if (mFabLayout != null) { - startBottomFabLayout = mFabLayout.getPaddingBottom(); - } - } - - @NonNull - @Override - public WindowInsetsAnimation.Bounds onStart(@NonNull WindowInsetsAnimation animation, @NonNull WindowInsetsAnimation.Bounds bounds) { - this.animation = animation; - if (mFabLayout != null) { - endBottomFabLayout = mFabLayout.getPaddingBottom(); - mFabLayout.setTranslationY(-(startBottomFabLayout - endBottomFabLayout)); - } - return bounds; - } - - @NonNull - @Override - public WindowInsets onProgress(@NonNull WindowInsets insets, @NonNull List runningAnimations) { - if (animation == null) { - return insets; - } - if (mFabLayout != null) { - int offset = MathUtils.lerp(-(startBottomFabLayout - endBottomFabLayout), 0, animation.getInterpolatedFraction()); - mFabLayout.setTranslationY(offset); - } - return insets; - } - - @Override - public void onEnd(@NonNull WindowInsetsAnimation animation) { - startBottomFabLayout = 0; - endBottomFabLayout = 0; - this.animation = null; - } - }); - } + ViewCompat.setWindowInsetsAnimationCallback(view, new WindowInsetsAnimationHelper( + WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_STOP, + mFabLayout + )); Context context = getContext(); AssertUtils.assertNotNull(context); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java index c3f77a835..bec6f289d 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java @@ -22,7 +22,6 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.Typeface; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.os.Looper; import android.text.Spannable; @@ -39,8 +38,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewAnimationUtils; import android.view.ViewGroup; -import android.view.WindowInsets; -import android.view.WindowInsetsAnimation; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; @@ -49,6 +46,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsAnimationCompat; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -60,6 +59,7 @@ import com.hippo.easyrecyclerview.LinearDividerItemDecoration; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.R; import com.hippo.ehviewer.UrlOpener; +import com.hippo.ehviewer.WindowInsetsAnimationHelper; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; @@ -83,7 +83,6 @@ import com.hippo.widget.ObservedTextView; import com.hippo.yorozuya.AnimationUtils; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; -import com.hippo.yorozuya.MathUtils; import com.hippo.yorozuya.ResourcesUtils; import com.hippo.yorozuya.SimpleAnimatorListener; import com.hippo.yorozuya.StringUtils; @@ -184,74 +183,11 @@ public final class GalleryCommentsScene extends ToolbarScene mFabLayout = (FabLayout) ViewUtils.$$(view, R.id.fab_layout); mFab = (FloatingActionButton) ViewUtils.$$(view, R.id.fab); mRefreshLayout = (SwipeRefreshLayout) ViewUtils.$$(view, R.id.refresh_layout); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - view.setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(WindowInsetsAnimation.Callback.DISPATCH_MODE_STOP) { - int startBottomEditPanel = 0; - int startBottomFabLayout = 0; - int endBottomEditPanel = 0; - int endBottomFabLayout = 0; - WindowInsetsAnimation animation; - - @Override - public void onPrepare(@NonNull WindowInsetsAnimation animation) { - this.animation = animation; - if (mEditPanel != null) { - startBottomEditPanel = mEditPanel.getPaddingBottom(); - } - if (mFabLayout != null) { - startBottomFabLayout = mFabLayout.getPaddingBottom(); - } - super.onPrepare(animation); - } - - @NonNull - @Override - public WindowInsetsAnimation.Bounds onStart(@NonNull WindowInsetsAnimation animation, @NonNull WindowInsetsAnimation.Bounds bounds) { - this.animation = animation; - if (mEditPanel != null) { - endBottomEditPanel = mEditPanel.getPaddingBottom(); - mEditPanel.setTranslationY(-(startBottomEditPanel - endBottomEditPanel)); - } - if (mFabLayout != null) { - endBottomFabLayout = mFabLayout.getPaddingBottom(); - mFabLayout.setTranslationY(-(startBottomFabLayout - endBottomFabLayout)); - } - return bounds; - } - - @NonNull - @Override - public WindowInsets onProgress(@NonNull WindowInsets insets, @NonNull List runningAnimations) { - if (animation == null) { - return insets; - } - if (mEditPanel != null) { - int offset = MathUtils.lerp(-(startBottomEditPanel - endBottomEditPanel), 0, animation.getInterpolatedFraction()); - mEditPanel.setTranslationY(offset); - } - if (mFabLayout != null) { - int offset = MathUtils.lerp(-(startBottomFabLayout - endBottomFabLayout), 0, animation.getInterpolatedFraction()); - mFabLayout.setTranslationY(offset); - } - return insets; - } - - @Override - public void onEnd(@NonNull WindowInsetsAnimation animation) { - startBottomEditPanel = 0; - startBottomFabLayout = 0; - endBottomEditPanel = 0; - endBottomFabLayout = 0; - this.animation = null; - if (mEditPanel != null) { - mEditPanel.setTranslationY(0); - } - if (mFabLayout != null) { - mFabLayout.setTranslationY(0); - } - } - }); - } + ViewCompat.setWindowInsetsAnimationCallback(view, new WindowInsetsAnimationHelper( + WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_STOP, + mEditPanel, + mFabLayout + )); mRefreshLayout.setColorSchemeResources( R.color.loading_indicator_red, diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java index 18efa4b1b..e4ef6c5dc 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java @@ -26,7 +26,6 @@ import android.content.res.Resources; import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.text.InputType; import android.text.Spannable; @@ -41,8 +40,6 @@ import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.ViewPropertyAnimator; -import android.view.WindowInsets; -import android.view.WindowInsetsAnimation; import android.widget.ImageView; import android.widget.TextView; @@ -55,6 +52,8 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.Toolbar; import androidx.core.content.ContextCompat; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsAnimationCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -80,6 +79,7 @@ import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.FavouriteStatusRouter; import com.hippo.ehviewer.R; import com.hippo.ehviewer.Settings; +import com.hippo.ehviewer.WindowInsetsAnimationHelper; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; @@ -569,77 +569,11 @@ public final class GalleryListScene extends BaseScene mSearchBar = (SearchBar) ViewUtils.$$(mainLayout, R.id.search_bar); mFabLayout = (FabLayout) ViewUtils.$$(mainLayout, R.id.fab_layout); mSearchFab = ViewUtils.$$(mainLayout, R.id.search_fab); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - view.setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(WindowInsetsAnimation.Callback.DISPATCH_MODE_STOP) { - int startBottomSearchFab = 0; - int startBottomFabLayout = 0; - int endBottomSearchFab = 0; - int endBottomFabLayout = 0; - WindowInsetsAnimation animation; - - @Override - public void onPrepare(@NonNull WindowInsetsAnimation animation) { - this.animation = animation; - if (mSearchFab != null) { - FabLayout fabLayout = (FabLayout) mSearchFab.getParent(); - startBottomSearchFab = fabLayout.getPaddingBottom(); - } - if (mFabLayout != null) { - startBottomFabLayout = mFabLayout.getPaddingBottom(); - } - } - - @NonNull - @Override - public WindowInsetsAnimation.Bounds onStart(@NonNull WindowInsetsAnimation animation, @NonNull WindowInsetsAnimation.Bounds bounds) { - this.animation = animation; - if (mSearchFab != null) { - FabLayout fabLayout = (FabLayout) mSearchFab.getParent(); - endBottomSearchFab = fabLayout.getPaddingBottom(); - fabLayout.setTranslationY(-(startBottomSearchFab - endBottomSearchFab)); - } - if (mFabLayout != null) { - endBottomFabLayout = mFabLayout.getPaddingBottom(); - mFabLayout.setTranslationY(-(startBottomFabLayout - endBottomFabLayout)); - } - return bounds; - } - - @NonNull - @Override - public WindowInsets onProgress(@NonNull WindowInsets insets, @NonNull List runningAnimations) { - if (animation == null) { - return insets; - } - if (mSearchFab != null) { - FabLayout fabLayout = (FabLayout) mSearchFab.getParent(); - int offset = MathUtils.lerp(-(startBottomSearchFab - endBottomSearchFab), 0, animation.getInterpolatedFraction()); - fabLayout.setTranslationY(offset); - } - if (mFabLayout != null) { - int offset = MathUtils.lerp(-(startBottomFabLayout - endBottomFabLayout), 0, animation.getInterpolatedFraction()); - mFabLayout.setTranslationY(offset); - } - return insets; - } - - @Override - public void onEnd(@NonNull WindowInsetsAnimation animation) { - startBottomSearchFab = 0; - startBottomFabLayout = 0; - endBottomSearchFab = 0; - endBottomFabLayout = 0; - this.animation = null; - if (mSearchFab != null) { - FabLayout fabLayout = (FabLayout) mSearchFab.getParent(); - fabLayout.setTranslationY(0); - } - if (mFabLayout != null) { - mFabLayout.setTranslationY(0); - } - } - }); - } + ViewCompat.setWindowInsetsAnimationCallback(view, new WindowInsetsAnimationHelper( + WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_STOP, + mFabLayout, + (View) mSearchFab.getParent() + )); int paddingTopSB = resources.getDimensionPixelOffset(R.dimen.gallery_padding_top_search_bar); int paddingBottomFab = resources.getDimensionPixelOffset(R.dimen.gallery_padding_bottom_fab);