|
|
@@ -0,0 +1,129 @@
|
|
|
+package com.miekir.ym.listener;
|
|
|
+
|
|
|
+import android.animation.AnimatorSet;
|
|
|
+import android.animation.ObjectAnimator;
|
|
|
+import android.app.Activity;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+
|
|
|
+import com.google.android.material.appbar.AppBarLayout;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Miekir
|
|
|
+ * @date 2020/7/21 8:35
|
|
|
+ * Description: AppBar移动时的图标动画监听
|
|
|
+ */
|
|
|
+public class AppbarTranslateListener implements AppBarLayout.OnOffsetChangedListener {
|
|
|
+ private static final int STATUS_CLOSE = 1;
|
|
|
+ private static final int STATUS_OPEN = 2;
|
|
|
+ private static final int STATUS_MOVE = 3;
|
|
|
+ /**
|
|
|
+ * 1. 完全关闭 2. 完全打开 3.运动中
|
|
|
+ */
|
|
|
+ private int mLastStatus = STATUS_CLOSE;
|
|
|
+
|
|
|
+ private double mDefaultHeight;
|
|
|
+ private double mDefaultWidth;
|
|
|
+
|
|
|
+ private ViewGroup mViewGroup;
|
|
|
+ private View rl_search_top;
|
|
|
+ private View rl_search_top_shadow;
|
|
|
+ private View rl_search;
|
|
|
+
|
|
|
+ private long mLastAnimateMillis;
|
|
|
+
|
|
|
+ public AppbarTranslateListener(Activity activity,
|
|
|
+ double defaultWidth, double defaultHeight,
|
|
|
+ View rl_search_top, View rl_search_top_shadow,
|
|
|
+ View rl_search) {
|
|
|
+ mViewGroup = (ViewGroup) activity.getWindow().getDecorView();
|
|
|
+
|
|
|
+ mDefaultWidth = defaultWidth;
|
|
|
+ mDefaultHeight = defaultHeight;
|
|
|
+ this.rl_search = rl_search;
|
|
|
+ this.rl_search_top = rl_search_top;
|
|
|
+ this.rl_search_top_shadow = rl_search_top_shadow;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
|
|
|
+ // 小于10ms的间隔直接忽略
|
|
|
+ if (System.currentTimeMillis() - mLastAnimateMillis < 10) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ mLastAnimateMillis = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // AppBarLayout完全折叠时,fraction为1
|
|
|
+ float fraction = ((float) Math.abs(verticalOffset)) / appBarLayout.getTotalScrollRange();
|
|
|
+
|
|
|
+ float reFraction = 1 - fraction;
|
|
|
+ if (reFraction < 0.007) {
|
|
|
+ // 完全折叠
|
|
|
+ if (mLastStatus != STATUS_CLOSE) {
|
|
|
+ mLastStatus = STATUS_CLOSE;
|
|
|
+ mViewGroup.getOverlay().remove(rl_search_top);
|
|
|
+ rl_search.setVisibility(View.VISIBLE);
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rl_search.setVisibility(View.INVISIBLE);
|
|
|
+ if (reFraction > 0.988) {
|
|
|
+ // 完全展开
|
|
|
+ if (mLastStatus != STATUS_OPEN) {
|
|
|
+ mLastStatus = STATUS_OPEN;
|
|
|
+ rl_search_top_shadow.setVisibility(View.VISIBLE);
|
|
|
+ mViewGroup.getOverlay().add(rl_search_top);
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 移动中
|
|
|
+ if (mLastStatus != STATUS_MOVE) {
|
|
|
+ mLastStatus = STATUS_MOVE;
|
|
|
+ rl_search_top_shadow.setVisibility(View.INVISIBLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ followFinger(fraction);
|
|
|
+
|
|
|
+ /*double searchSize = mDefaultImageSize * fraction;
|
|
|
+ ViewGroup.LayoutParams searchParams = iv_search.getLayoutParams();
|
|
|
+ searchParams.width = (int) searchSize;
|
|
|
+ searchParams.height = (int) searchSize;
|
|
|
+ iv_search.requestLayout();
|
|
|
+
|
|
|
+ double topSearchSize = mDefaultImageSize - searchSize;
|
|
|
+ ViewGroup.LayoutParams topSearchParams = iv_search_top.getLayoutParams();
|
|
|
+ topSearchParams.width = (int) topSearchSize;
|
|
|
+ topSearchParams.height = (int) topSearchSize;
|
|
|
+ iv_search_top.requestLayout();*/
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void followFinger(float fraction) {
|
|
|
+ // 完全展开时fraction为0,所以实时translationX为mDefaultWidth*fraction,
|
|
|
+ // translationY为mDefaultHeight*fraction
|
|
|
+
|
|
|
+ // add之后,要紧接动画
|
|
|
+ mViewGroup.getOverlay().add(rl_search_top);
|
|
|
+ // 横向移动
|
|
|
+ ObjectAnimator tranXAnim = ObjectAnimator.ofFloat(rl_search_top, "translationX", (float) (mDefaultWidth*fraction));
|
|
|
+ tranXAnim.setDuration(0);
|
|
|
+ // 纵向移动
|
|
|
+ ObjectAnimator tranYAnim = ObjectAnimator.ofFloat(rl_search_top, "translationY", (float) (-mDefaultHeight*fraction));
|
|
|
+ tranYAnim.setDuration(0);
|
|
|
+
|
|
|
+ //tranXAnim.start();
|
|
|
+
|
|
|
+ // 一起做运动
|
|
|
+ AnimatorSet set = new AnimatorSet();
|
|
|
+ set.playTogether(tranXAnim, tranYAnim);
|
|
|
+ set.start();
|
|
|
+ }
|
|
|
+}
|