-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomScrollView.java
More file actions
251 lines (209 loc) · 7.75 KB
/
CustomScrollView.java
File metadata and controls
251 lines (209 loc) · 7.75 KB
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
package {{packageName}};
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;
public class {{viewName}} extends View {
public static final String TAG = "{{viewName}}";
public OnScrollListener getScrollListener() {
return mScrollListener;
}
public void setScrollListener(OnScrollListener mScrollListener) {
this.mScrollListener = mScrollListener;
if(mScrollListener != null){
mScrollListener.onChangeSize(mSize.x, mSize.y);
mScrollListener.onChangeContent(mContent.width(), mContent.height());
mScrollListener.onChangeScroll(mScroll.x, mScroll.y);
}
}
public interface OnScrollListener {
void onChangeScroll(final int x, final int y);
void onChangeContent(final int width,final int height);
void onChangeSize(final int maxWidth, final int maxHeight);
}
private OnScrollListener mScrollListener;
private GestureDetectorCompat mGestureDetector;
private OverScroller mScroller;
private ArrayMap<String, Region> mRegions = new ArrayMap<>();
/**
* mContent is visible space
*/
private Rect mContent = new Rect();
/**
* mSize is full size drawing space
* .x - right side
* .y - bottom side
*/
private Point mSize = new Point();
/**
* mScroll current offset scroll
* .x - between 0 and mSize.x
* .y - between 0 and mSize.y
*/
private Point mScroll = new Point();
private Point mPoint = new Point();
protected boolean hasVerticalScroll;
protected boolean hasHorizontalScroll;
private float density;
{{shapesProperties}}
{{paintsProperties}}
public {{viewName}}(Context context) {
super(context);
init(context);
}
public {{viewName}}(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public {{viewName}}(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public {{viewName}}(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
public void init(Context context) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
density = context.getResources().getDisplayMetrics().density;
mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
mScroller = new OverScroller(context);
mSize.set({{sizeView}});
if (mScrollListener != null) {
mScrollListener.onChangeSize(mSize.x, mSize.y);
}
{{paintInit}}
{{shapesInit}}
RectF bounds = new RectF();
{{regionsInit}}
}
@Override
public void onDraw(Canvas canvas) {
canvas.translate(-mScroll.x, -mScroll.y);
{{draw}}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mContent.set(
getPaddingLeft(),
getPaddingTop(),
getWidth() - getPaddingRight(),
getHeight() - getPaddingBottom());
hasHorizontalScroll = mContent.width() < mSize.x;
hasVerticalScroll = mContent.height() < mSize.y;
if(mScrollListener != null){
mScrollListener.onChangeContent(mContent.width(), mContent.height());
}
}
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
// The scroller isn't finished, meaning a fling or programmatic pan operation is
// currently active.
int currX = mScroller.getCurrX();
int currY = mScroller.getCurrY();
float currXRange = currX - mScroll.x;
float currYRange = currY - mScroll.y;
setViewportBottomLeft(currXRange, currYRange);
}
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minSizeWidth = mSize.x;
int minSizeHeight = mSize.y;
setMeasuredDimension(Math.max(getSuggestedMinimumWidth(),
resolveSize(minSizeWidth + getPaddingLeft() + getPaddingRight(), widthMeasureSpec)),
Math.max(getSuggestedMinimumHeight(),
resolveSize(minSizeHeight + getPaddingTop() + getPaddingBottom(), heightMeasureSpec)));
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
fling((int) -velocityX, (int) -velocityY);
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
setViewportBottomLeft((int) distanceX, (int) distanceY);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);
ViewCompat.postInvalidateOnAnimation({{viewName}}.this);
return true;
}
//handle clicks
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
mPoint.x = (int)e.getX() + mScroll.x;
mPoint.y = (int)e.getY() + mScroll.y;
boolean handled = false;
//check in all shapes
for(int i = 0; i< mRegions.size(); i++){
//check i touched shape[i]
if(mRegions.valueAt(i).contains(mPoint.x, mPoint.y)){
Log.d(TAG, "Touch inside " + mRegions.keyAt(i));
handled = true;
}
}
return handled;
}
};
private void setViewportBottomLeft(float dx, float dy) {
float x = 0;
float y = 0;
if (hasHorizontalScroll) {
x = mScroll.x + dx < 0 ? 0 : mScroll.x + dx + mContent.width() < mSize.x ? mScroll.x + dx
: mSize.x - mContent.width();
}
if (hasVerticalScroll) {
y = mScroll.y + dy < 0 ? 0 : mScroll.y + dy + mContent.height() < mSize.y ? mScroll.y + dy
: mSize.y - mContent.height();
}
if(!mScroll.equals((int)x, (int)y)) {
mScroll.set((int)x, (int)y);
ViewCompat.postInvalidateOnAnimation(this);
if(mScrollListener != null){
mScrollListener.onChangeScroll(mScroll.x, mScroll.y);
}
}
}
private void fling(int velocityX, int velocityY) {
mScroller.forceFinished(true);
mScroller.fling(
mScroll.x,
mScroll.y,
velocityX/2,
velocityY/2,
0, mSize.x - mContent.width(),
0, mSize.y - mContent.height(),
mContent.width() / 2,
mContent.height() / 2);
ViewCompat.postInvalidateOnAnimation(this);
}
}