管灌系统巡查员智能手机App
zuoxiao
2025-02-11 dde9027478b772dd60371937413ac2838c4f3bbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cc.shinichi.library.view.helper;
 
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
 
import cc.shinichi.library.ImagePreview;
import cc.shinichi.library.view.nine.ViewHelper;
import cc.shinichi.library.view.photoview.PhotoView;
import cc.shinichi.library.view.subsampling.SubsamplingScaleImageView;
 
/**
 * @author 工藤
 * @email qinglingou@gmail.com
 * create at 2018/10/19  11:22
 * description:辅助下拉关闭图片
 */
public class DragCloseView extends RelativeLayout {
 
    private static final String TAG = DragCloseView.class.getSimpleName();
 
    private final static int MAX_EXIT_Y = 500;
    private final static long DURATION = 200;
 
    private SubsamplingScaleImageView imageStatic;
    private PhotoView imageAnime;
    private View videoView;
 
    private float mDownX;
    private float mDownY;
    private float mTranslationY;
 
    private onAlphaChangedListener mOnAlphaChangedListener;
 
    public DragCloseView(Context context) {
        this(context, null);
    }
 
    public DragCloseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public DragCloseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        imageStatic = (SubsamplingScaleImageView) getChildAt(0);
        imageAnime = (PhotoView) getChildAt(1);
        videoView = getChildAt(2);
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ImagePreview.Companion.getInstance().isEnableDragClose()) {
            int action = ev.getActionMasked();
            if (action == MotionEvent.ACTION_DOWN) {
                mDownX = ev.getRawX();
                mDownY = ev.getRawY();
            } else if (action == MotionEvent.ACTION_MOVE) {
                return canInterceptDrag(ev);
            }
        }
        return false;
    }
 
    private boolean canInterceptDrag(MotionEvent ev) {
        float diffX = Math.abs(ev.getX() - mDownX);
        float diffY = Math.abs(ev.getY() - mDownY);
        if (diffY <= diffX) {
            // 下拉手势,Y 轴位移小于 X 轴位移,是横向滑动,不拦截
            return false;
        }
        getParent().requestDisallowInterceptTouchEvent(true);
        if (imageStatic != null && imageStatic.getVisibility() == View.VISIBLE) {
            // 静图
            boolean isAtEdge = ImagePreview.Companion.getInstance().isEnableDragCloseIgnoreScale()
                    ? imageStatic.getScale() <= (imageStatic.getMinScale() + 0.001F) || imageStatic.isAtYEdge()
                    : imageStatic.getScale() <= (imageStatic.getMinScale() + 0.001F) && imageStatic.isAtYEdge();
            return isAtEdge && (imageStatic.getMaxTouchCount() == 0 || imageStatic.getMaxTouchCount() == 1);
        } else if (imageAnime != null && imageAnime.getVisibility() == View.VISIBLE) {
            // 动图
            return imageAnime.getScale() <= (imageAnime.getMinimumScale() + 0.001F) && (imageAnime.getMaxTouchCount() == 0 || imageAnime.getMaxTouchCount() == 1);
        } else if (videoView != null && videoView.getVisibility() == View.VISIBLE) {
            // 视频
            return true;
        }
        return false;
    }
 
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & event.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownX = event.getRawX();
                mDownY = event.getRawY();
            case MotionEvent.ACTION_MOVE:
                if (ImagePreview.Companion.getInstance().isEnableDragClose()) {
                    if ((imageAnime != null && imageAnime.getVisibility() == View.VISIBLE)
                            || (imageStatic != null && imageStatic.getVisibility() == View.VISIBLE)
                            || (videoView != null && videoView.getVisibility() == View.VISIBLE)
                    ) {
                        onOneFingerPanActionMove(event);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                onActionUp();
                break;
            default:
                break;
        }
        return true;
    }
 
    /**
     * 处理拖拽事件
     *
     * @param event
     */
    private void onOneFingerPanActionMove(MotionEvent event) {
        float moveY = event.getRawY();
        mTranslationY = moveY - mDownY;
        if (mOnAlphaChangedListener != null) {
            mOnAlphaChangedListener.onTranslationYChanged(event, mTranslationY);
        }
        setTranslationY(mTranslationY);
    }
 
    private void onActionUp() {
        // 是否启用上拉关闭
        boolean enableUpDragClose = ImagePreview.Companion.getInstance().isEnableUpDragClose();
        if (enableUpDragClose) {
            if (Math.abs(mTranslationY) > MAX_EXIT_Y) {
                if (null != mOnAlphaChangedListener) {
                    mOnAlphaChangedListener.onExit();
                }
                exit();
            } else {
                resetCallBackAnimation();
            }
        } else {
            if (mTranslationY > MAX_EXIT_Y) {
                if (null != mOnAlphaChangedListener) {
                    mOnAlphaChangedListener.onExit();
                }
                exit();
            } else {
                resetCallBackAnimation();
            }
        }
    }
 
    private void resetCallBackAnimation() {
        ValueAnimator animatorY = ValueAnimator.ofFloat(mTranslationY, 0);
        animatorY.setDuration(DURATION);
        animatorY.addUpdateListener(animation -> {
            mTranslationY = (float) animation.getAnimatedValue();
            setTranslationY(mTranslationY);
        });
        animatorY.addListener(new SimpleAnimatorListener() {
            @Override
            public void onAnimationEnd(@NonNull Animator animation) {
                mTranslationY = 0;
                reset();
            }
        });
        animatorY.start();
    }
 
    private void exit() {
        ValueAnimator animatorExit;
        if (mTranslationY > 0) {
            animatorExit = ValueAnimator.ofFloat(mTranslationY, getHeight());
        } else {
            animatorExit = ValueAnimator.ofFloat(mTranslationY, -getHeight());
        }
        animatorExit.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float fraction = (float) animation.getAnimatedValue();
                ViewHelper.setScrollY(DragCloseView.this, -(int) fraction);
            }
        });
        animatorExit.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(@NonNull Animator animation) {
            }
 
            @Override
            public void onAnimationEnd(@NonNull Animator animation) {
                ((Activity) getContext()).finish();
            }
 
            @Override
            public void onAnimationCancel(@NonNull Animator animation) {
            }
 
            @Override
            public void onAnimationRepeat(@NonNull Animator animation) {
            }
        });
        animatorExit.setDuration(DURATION);
        animatorExit.setInterpolator(new LinearInterpolator());
        animatorExit.start();
    }
 
    /**
     * 暴露的回调方法(可根据位移距离或者alpha来改变主UI控件的透明度等
     */
    public void setOnAlphaChangeListener(onAlphaChangedListener alphaChangeListener) {
        mOnAlphaChangedListener = alphaChangeListener;
    }
 
    private void reset() {
        if (null != mOnAlphaChangedListener) {
            mOnAlphaChangedListener.onTranslationYChanged(null, mTranslationY);
        }
    }
 
    public abstract static class SimpleAnimatorListener implements Animator.AnimatorListener {
        @Override
        public void onAnimationStart(@NonNull Animator animation) {
        }
 
        @Override
        public void onAnimationCancel(@NonNull Animator animation) {
        }
 
        @Override
        public void onAnimationRepeat(@NonNull Animator animation) {
        }
    }
 
    public interface onAlphaChangedListener {
 
        void onTranslationYChanged(MotionEvent event, float translationY);
 
        void onExit();
    }
}