-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathMotionView.java
More file actions
384 lines (325 loc) · 11.7 KB
/
MotionView.java
File metadata and controls
384 lines (325 loc) · 11.7 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package team.uptech.motionviews.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.FrameLayout;
import com.almeros.android.multitouch.MoveGestureDetector;
import com.almeros.android.multitouch.RotateGestureDetector;
import java.util.ArrayList;
import java.util.List;
import team.uptech.motionviews.R;
import team.uptech.motionviews.widget.entity.MotionEntity;
/**
* Created on 9/29/16.
*/
public class MotionView extends FrameLayout {
private static final String TAG = MotionView.class.getSimpleName();
public interface Constants {
float SELECTED_LAYER_ALPHA = 0.15F;
}
public interface MotionViewCallback {
void onEntitySelected(@Nullable MotionEntity entity);
void onEntityDoubleTap(@NonNull MotionEntity entity);
}
// layers
private final List<MotionEntity> entities = new ArrayList<>();
@Nullable
private MotionEntity selectedEntity;
private Paint selectedLayerPaint;
// callback
@Nullable
private MotionViewCallback motionViewCallback;
// gesture detection
private ScaleGestureDetector scaleGestureDetector;
private RotateGestureDetector rotateGestureDetector;
private MoveGestureDetector moveGestureDetector;
private GestureDetectorCompat gestureDetectorCompat;
// constructors
public MotionView(Context context) {
super(context);
init(context);
}
public MotionView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MotionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@SuppressWarnings("unused")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MotionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(@NonNull Context context) {
// I fucking love Android
setWillNotDraw(false);
selectedLayerPaint = new Paint();
selectedLayerPaint.setAlpha((int) (255 * Constants.SELECTED_LAYER_ALPHA));
selectedLayerPaint.setAntiAlias(true);
// init listeners
this.scaleGestureDetector = new ScaleGestureDetector(context, new ScaleListener());
this.rotateGestureDetector = new RotateGestureDetector(context, new RotateListener());
this.moveGestureDetector = new MoveGestureDetector(context, new MoveListener());
this.gestureDetectorCompat = new GestureDetectorCompat(context, new TapsListener());
setOnTouchListener(onTouchListener);
updateUI();
}
public MotionEntity getSelectedEntity() {
return selectedEntity;
}
public List<MotionEntity> getEntities() {
return entities;
}
public void setMotionViewCallback(@Nullable MotionViewCallback callback) {
this.motionViewCallback = callback;
}
public void addEntity(@Nullable MotionEntity entity) {
if (entity != null) {
entities.add(entity);
selectEntity(entity, false);
}
}
public void addEntityAndPosition(@Nullable MotionEntity entity) {
if (entity != null) {
initEntityBorder(entity);
initialTranslateAndScale(entity);
entities.add(entity);
selectEntity(entity, true);
}
}
private void initEntityBorder(@NonNull MotionEntity entity ) {
// init stroke
int strokeSize = getResources().getDimensionPixelSize(R.dimen.stroke_size);
Paint borderPaint = new Paint();
borderPaint.setStrokeWidth(strokeSize);
borderPaint.setAntiAlias(true);
borderPaint.setColor(ContextCompat.getColor(getContext(), R.color.stroke_color));
entity.setBorderPaint(borderPaint);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// dispatch draw is called after child views is drawn.
// the idea that is we draw background stickers, than child views (if any), and than selected item
// to draw on top of child views - do it in dispatchDraw(Canvas)
// to draw below that - do it in onDraw(Canvas)
if (selectedEntity != null) {
selectedEntity.draw(canvas, selectedLayerPaint);
}
}
@Override
protected void onDraw(Canvas canvas) {
drawAllEntities(canvas);
super.onDraw(canvas);
}
/**
* draws all entities on the canvas
* @param canvas Canvas where to draw all entities
*/
private void drawAllEntities(Canvas canvas) {
for (int i = 0; i < entities.size(); i++) {
entities.get(i).draw(canvas, null);
}
}
/**
* as a side effect - the method deselects Entity (if any selected)
* @return bitmap with all the Entities at their current positions
*/
public Bitmap getThumbnailImage() {
selectEntity(null, false);
Bitmap bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
// IMPORTANT: always create white background, cos if the image is saved in JPEG format,
// which doesn't have transparent pixels, the background will be black
bmp.eraseColor(Color.WHITE);
Canvas canvas = new Canvas(bmp);
drawAllEntities(canvas);
return bmp;
}
private void updateUI() {
invalidate();
}
private void handleTranslate(PointF delta) {
if (selectedEntity != null) {
float newCenterX = selectedEntity.absoluteCenterX() + delta.x;
float newCenterY = selectedEntity.absoluteCenterY() + delta.y;
// limit entity center to screen bounds
boolean needUpdateUI = false;
if (newCenterX >= 0 && newCenterX <= getWidth()) {
selectedEntity.getLayer().postTranslate(delta.x / getWidth(), 0.0F);
needUpdateUI = true;
}
if (newCenterY >= 0 && newCenterY <= getHeight()) {
selectedEntity.getLayer().postTranslate(0.0F, delta.y / getHeight());
needUpdateUI = true;
}
if (needUpdateUI) {
updateUI();
}
}
}
private void initialTranslateAndScale(@NonNull MotionEntity entity) {
entity.moveToCanvasCenter();
entity.getLayer().setScale(entity.getLayer().initialScale());
}
private void selectEntity(@Nullable MotionEntity entity, boolean updateCallback) {
if (selectedEntity != null) {
selectedEntity.setIsSelected(false);
}
if (entity != null) {
entity.setIsSelected(true);
}
selectedEntity = entity;
invalidate();
if (updateCallback && motionViewCallback != null) {
motionViewCallback.onEntitySelected(entity);
}
}
public void unselectEntity() {
if (selectedEntity != null) {
selectEntity(null, true);
}
}
@Nullable
private MotionEntity findEntityAtPoint(float x, float y) {
MotionEntity selected = null;
PointF p = new PointF(x, y);
for (int i = entities.size() - 1; i >= 0; i--) {
if (entities.get(i).pointInLayerRect(p)) {
selected = entities.get(i);
break;
}
}
return selected;
}
private void updateSelectionOnTap(MotionEvent e) {
MotionEntity entity = findEntityAtPoint(e.getX(), e.getY());
selectEntity(entity, true);
}
private void updateOnLongPress(MotionEvent e) {
// if layer is currently selected and point inside layer - move it to front
if (selectedEntity != null) {
PointF p = new PointF(e.getX(), e.getY());
if (selectedEntity.pointInLayerRect(p)) {
bringLayerToFront(selectedEntity);
}
}
}
private void bringLayerToFront(@NonNull MotionEntity entity) {
// removing and adding brings layer to front
if (entities.remove(entity)) {
entities.add(entity);
invalidate();
}
}
private void moveEntityToBack(@Nullable MotionEntity entity) {
if (entity == null) {
return;
}
if (entities.remove(entity)) {
entities.add(0, entity);
invalidate();
}
}
public void flipSelectedEntity() {
if (selectedEntity == null) {
return;
}
selectedEntity.getLayer().flip();
invalidate();
}
public void moveSelectedBack() {
moveEntityToBack(selectedEntity);
}
public void deletedSelectedEntity() {
if (selectedEntity == null) {
return;
}
if (entities.remove(selectedEntity)) {
selectedEntity.release();
selectedEntity = null;
invalidate();
}
}
// memory
public void release() {
for (MotionEntity entity : entities) {
entity.release();
}
}
// gesture detectors
private final View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (scaleGestureDetector != null) {
scaleGestureDetector.onTouchEvent(event);
rotateGestureDetector.onTouchEvent(event);
moveGestureDetector.onTouchEvent(event);
gestureDetectorCompat.onTouchEvent(event);
}
return true;
}
};
private class TapsListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
if (motionViewCallback != null && selectedEntity != null) {
motionViewCallback.onEntityDoubleTap(selectedEntity);
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
updateOnLongPress(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
updateSelectionOnTap(e);
return true;
}
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
if (selectedEntity != null) {
float scaleFactorDiff = detector.getScaleFactor();
selectedEntity.getLayer().postScale(scaleFactorDiff);
updateUI();
}
return true;
}
}
private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
@Override
public boolean onRotate(RotateGestureDetector detector) {
if (selectedEntity != null) {
selectedEntity.getLayer().postRotate(-detector.getRotationDegreesDelta());
updateUI();
}
return true;
}
}
private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
@Override
public boolean onMove(MoveGestureDetector detector) {
handleTranslate(detector.getFocusDelta());
return true;
}
}
}