/
ImageViewTouch.java
250 lines (213 loc) · 7.4 KB
/
ImageViewTouch.java
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
package it.sephiroth.android.library.imagezoom;
import it.sephiroth.android.library.imagezoom.ScaleGestureDetector.OnScaleGestureListener;
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.Constants;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
public class ImageViewTouch extends ImageViewTouchBase implements Constants {
private static final float SCROLL_DELTA_THRESHOLD = 1.0f;
static final float MIN_ZOOM = 0.9f;
protected int mTouchSlop;
protected float mCurrentScaleFactor;
protected float mScaleFactor;
protected int mDoubleTapDirection;
protected GestureDetector mGestureDetector;
protected OnGestureListener mGestureListener;
protected OnScaleGestureListener mScaleListener;
protected ScaleGestureDetector mScaleDetector;
protected boolean mDoubleTapToZoomEnabled = true;
protected boolean mScaleEnabled = true;
protected boolean mScrollEnabled = true;
private OnImageViewTouchDoubleTapListener doubleTapListener;
public ImageViewTouch(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
/**
* Determines whether this ImageViewTouch can be scrolled.
*
* @param direction - positive direction value means scroll from right to
* left, negative value means scroll from left to right
*
* @return true if there is some more place to scroll, false - otherwise.
*/
public boolean canScroll(final int direction) {
final RectF bitmapRect = getBitmapRect();
updateRect(bitmapRect, mScrollRect);
final Rect imageViewRect = new Rect();
getGlobalVisibleRect(imageViewRect);
if (bitmapRect.right >= imageViewRect.right) {
if (direction < 0) return Math.abs(bitmapRect.right - imageViewRect.right) > SCROLL_DELTA_THRESHOLD;
}
final double bitmapScrollRectDelta = Math.abs(bitmapRect.left - mScrollRect.left);
return bitmapScrollRectDelta > SCROLL_DELTA_THRESHOLD;
}
public boolean getDoubleTapEnabled() {
return mDoubleTapToZoomEnabled;
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
if (mScaleDetector != null) {
mScaleDetector.onTouchEvent(event);
}
if (mScaleDetector != null && !mScaleDetector.isInProgress()) {
mGestureDetector.onTouchEvent(event);
}
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
if (getScale() < 1f) {
zoomTo(1f, 50);
}
break;
}
return true;
}
public void setDoubleTapListener(final OnImageViewTouchDoubleTapListener doubleTapListener) {
this.doubleTapListener = doubleTapListener;
}
public void setDoubleTapToZoomEnabled(final boolean value) {
mDoubleTapToZoomEnabled = value;
}
public void setScaleEnabled(final boolean value) {
mScaleEnabled = value;
}
public void setScrollEnabled(final boolean value) {
mScrollEnabled = value;
}
@Override
protected void _setImageDrawable(final Drawable drawable, final boolean reset, final Matrix initial_matrix,
final float maxZoom) {
super._setImageDrawable(drawable, reset, initial_matrix, maxZoom);
mScaleFactor = getMaxZoom() / 3;
}
protected OnGestureListener getGestureListener() {
return new GestureListener();
}
protected OnScaleGestureListener getScaleListener() {
return new ScaleListener();
}
@Override
protected void init() {
super.init();
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mGestureListener = getGestureListener();
mGestureDetector = new GestureDetector(getContext(), mGestureListener);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
mScaleListener = getScaleListener();
mScaleDetector = new ScaleGestureDetector(getContext(), mScaleListener);
}
mCurrentScaleFactor = 1f;
mDoubleTapDirection = 1;
}
@Override
protected void onBitmapChanged(final Drawable drawable) {
super.onBitmapChanged(drawable);
final float v[] = new float[9];
mSuppMatrix.getValues(v);
mCurrentScaleFactor = v[Matrix.MSCALE_X];
}
protected float onDoubleTapPost(final float scale, final float maxZoom) {
if (mDoubleTapDirection == 1) {
if (scale + mScaleFactor * 2 <= maxZoom)
return scale + mScaleFactor;
else {
mDoubleTapDirection = -1;
return maxZoom;
}
} else {
mDoubleTapDirection = 1;
return 1f;
}
}
@Override
protected void onZoom(final float scale) {
super.onZoom(scale);
if (mScaleDetector != null && !mScaleDetector.isInProgress()) {
mCurrentScaleFactor = scale;
}
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(final MotionEvent e) {
if (BuildConfig.DEBUG) {
Log.i(LOG_TAG, "onDoubleTap. double tap enabled? " + mDoubleTapToZoomEnabled);
}
if (mDoubleTapToZoomEnabled) {
final float scale = getScale();
float targetScale = scale;
targetScale = onDoubleTapPost(scale, getMaxZoom());
targetScale = Math.min(getMaxZoom(), Math.max(targetScale, MIN_ZOOM));
mCurrentScaleFactor = targetScale;
zoomTo(targetScale, e.getX(), e.getY(), 200);
invalidate();
}
if (null != doubleTapListener) {
doubleTapListener.onDoubleTap();
}
return super.onDoubleTap(e);
}
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (!mScrollEnabled) return false;
// if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return
// false;
if (mScaleDetector != null && mScaleDetector.isInProgress()) return false;
final float diffX = e2.getX() - e1.getX();
final float diffY = e2.getY() - e1.getY();
if (Math.abs(velocityX) > 800 || Math.abs(velocityY) > 800) {
scrollBy(diffX / 2, diffY / 2, 300);
invalidate();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(final MotionEvent e) {
if (isLongClickable()) {
if (mScaleDetector != null && !mScaleDetector.isInProgress()) {
setPressed(true);
performLongClick();
}
}
}
@Override
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
if (!mScrollEnabled) return false;
if (e1 == null || e2 == null) return false;
// if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return
// false;
if (mScaleDetector != null && mScaleDetector.isInProgress()) return false;
if (getScale() == 1f) return false;
scrollBy(-distanceX, -distanceY);
invalidate();
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
public interface OnImageViewTouchDoubleTapListener {
void onDoubleTap();
}
public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(final ScaleGestureDetector detector) {
float targetScale = mCurrentScaleFactor * detector.getScaleFactor();
if (mScaleEnabled) {
targetScale = Math.min(getMaxZoom(), Math.max(targetScale, MIN_ZOOM));
zoomTo(targetScale, detector.getFocusX(), detector.getFocusY());
mCurrentScaleFactor = Math.min(getMaxZoom(), Math.max(targetScale, MIN_ZOOM));
mDoubleTapDirection = 1;
invalidate();
return true;
}
return false;
}
}
}