-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanvasView.java
901 lines (780 loc) · 23.2 KB
/
CanvasView.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* This class defines fields and methods for drawing.
*/
public class CanvasView extends View {
// Enumeration for Mode
public enum Mode {
DRAW,
TEXT,
ERASER;
}
// Enumeration for Drawer
public enum Drawer {
PEN,
LINE,
RECTANGLE,
CIRCLE,
ELLIPSE,
QUADRATIC_BEZIER,
QUBIC_BEZIER;
}
private Canvas canvas = null;
private Bitmap bitmap = null;
private List<Path> pathLists = new ArrayList<Path>();
private List<Paint> paintLists = new ArrayList<Paint>();
private final Paint emptyPaint = new Paint();
// for Eraser
private int baseColor = Color.WHITE;
// for Undo, Redo
private int historyPointer = 0;
// Flags
private Mode mode = Mode.DRAW;
private Drawer drawer = Drawer.PEN;
private boolean isDown = false;
// for Paint
private Paint.Style paintStyle = Paint.Style.STROKE;
private int paintStrokeColor = Color.BLACK;
private int paintFillColor = Color.BLACK;
private float paintStrokeWidth = 3F;
private int opacity = 255;
private float blur = 0F;
private Paint.Cap lineCap = Paint.Cap.ROUND;
private PathEffect drawPathEffect = null;
// for Text
private String text = "";
private Typeface fontFamily = Typeface.DEFAULT;
private float fontSize = 32F;
private Paint.Align textAlign = Paint.Align.RIGHT; // fixed
private Paint textPaint = new Paint();
private float textX = 0F;
private float textY = 0F;
// for Drawer
private float startX = 0F;
private float startY = 0F;
private float controlX = 0F;
private float controlY = 0F;
/**
* Copy Constructor
*
* @param context
* @param attrs
* @param defStyle
*/
public CanvasView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setup();
}
/**
* Copy Constructor
*
* @param context
* @param attrs
*/
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setup();
}
/**
* Copy Constructor
*
* @param context
*/
public CanvasView(Context context) {
super(context);
this.setup();
}
/**
* Common initialization.
*
*/
private void setup() {
this.pathLists.add(new Path());
this.paintLists.add(this.createPaint());
this.historyPointer++;
this.textPaint.setARGB(0, 255, 255, 255);
}
/**
* This method creates the instance of Paint.
* In addition, this method sets styles for Paint.
*
* @return paint This is returned as the instance of Paint
*/
private Paint createPaint() {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(this.paintStyle);
paint.setStrokeWidth(this.paintStrokeWidth);
paint.setStrokeCap(this.lineCap);
paint.setStrokeJoin(Paint.Join.MITER); // fixed
// for Text
if (this.mode == Mode.TEXT) {
paint.setTypeface(this.fontFamily);
paint.setTextSize(this.fontSize);
paint.setTextAlign(this.textAlign);
paint.setStrokeWidth(0F);
}
if (this.mode == Mode.ERASER) {
// Eraser
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
paint.setARGB(0, 0, 0, 0);
// paint.setColor(this.baseColor);
// paint.setShadowLayer(this.blur, 0F, 0F, this.baseColor);
} else {
// Otherwise
paint.setColor(this.paintStrokeColor);
paint.setShadowLayer(this.blur, 0F, 0F, this.paintStrokeColor);
paint.setAlpha(this.opacity);
paint.setPathEffect(this.drawPathEffect);
}
return paint;
}
/**
* This method initialize Path.
* Namely, this method creates the instance of Path,
* and moves current position.
*
* @param event This is argument of onTouchEvent method
* @return path This is returned as the instance of Path
*/
private Path createPath(MotionEvent event) {
Path path = new Path();
// Save for ACTION_MOVE
this.startX = event.getX();
this.startY = event.getY();
path.moveTo(this.startX, this.startY);
return path;
}
/**
* This method updates the lists for the instance of Path and Paint.
* "Undo" and "Redo" are enabled by this method.
*
* @param path the instance of Path
*/
private void updateHistory(Path path) {
if (this.historyPointer == this.pathLists.size()) {
this.pathLists.add(path);
this.paintLists.add(this.createPaint());
this.historyPointer++;
} else {
// On the way of Undo or Redo
this.pathLists.set(this.historyPointer, path);
this.paintLists.set(this.historyPointer, this.createPaint());
this.historyPointer++;
for (int i = this.historyPointer, size = this.paintLists.size(); i < size; i++) {
this.pathLists.remove(this.historyPointer);
this.paintLists.remove(this.historyPointer);
}
}
}
/**
* This method gets the instance of Path that pointer indicates.
*
* @return the instance of Path
*/
private Path getCurrentPath() {
return this.pathLists.get(this.historyPointer - 1);
}
/**
* This method draws text.
*
* @param canvas the instance of Canvas
*/
private void drawText(Canvas canvas) {
if (this.text.length() <= 0) {
return;
}
if (this.mode == Mode.TEXT) {
this.textX = this.startX;
this.textY = this.startY;
this.textPaint = this.createPaint();
}
float textX = this.textX;
float textY = this.textY;
Paint paintForMeasureText = new Paint();
// Line break automatically
float textLength = paintForMeasureText.measureText(this.text);
float lengthOfChar = textLength / (float)this.text.length();
float restWidth = this.canvas.getWidth() - textX; // text-align : right
int numChars = (lengthOfChar <= 0) ? 1 : (int)Math.floor((double)(restWidth / lengthOfChar)); // The number of characters at 1 line
int modNumChars = (numChars < 1) ? 1 : numChars;
float y = textY;
for (int i = 0, len = this.text.length(); i < len; i += modNumChars) {
String substring = "";
if ((i + modNumChars) < len) {
substring = this.text.substring(i, (i + modNumChars));
} else {
substring = this.text.substring(i, len);
}
y += this.fontSize;
canvas.drawText(substring, textX, y, this.textPaint);
}
}
/**
* This method defines processes on MotionEvent.ACTION_DOWN
*
* @param event This is argument of onTouchEvent method
*/
private void onActionDown(MotionEvent event) {
switch (this.mode) {
case DRAW :
case ERASER :
if ((this.drawer != Drawer.QUADRATIC_BEZIER) && (this.drawer != Drawer.QUBIC_BEZIER)) {
// Oherwise
this.updateHistory(this.createPath(event));
this.isDown = true;
} else {
// Bezier
if ((this.startX == 0F) && (this.startY == 0F)) {
// The 1st tap
this.updateHistory(this.createPath(event));
} else {
// The 2nd tap
this.controlX = event.getX();
this.controlY = event.getY();
this.isDown = true;
}
}
break;
case TEXT :
this.startX = event.getX();
this.startY = event.getY();
break;
default :
break;
}
}
/**
* This method defines processes on MotionEvent.ACTION_MOVE
*
* @param event This is argument of onTouchEvent method
*/
private void onActionMove(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (this.mode) {
case DRAW :
case ERASER :
if ((this.drawer != Drawer.QUADRATIC_BEZIER) && (this.drawer != Drawer.QUBIC_BEZIER)) {
if (!isDown) {
return;
}
Path path = this.getCurrentPath();
switch (this.drawer) {
case PEN :
path.lineTo(x, y);
break;
case LINE :
path.reset();
path.moveTo(this.startX, this.startY);
path.lineTo(x, y);
break;
case RECTANGLE :
path.reset();
float left = Math.min(this.startX, x);
float right = Math.max(this.startX, x);
float top = Math.min(this.startY, y);
float bottom = Math.max(this.startY, y);
path.addRect(left, top, right, bottom, Path.Direction.CCW);
break;
case CIRCLE :
double distanceX = Math.abs((double)(this.startX - x));
double distanceY = Math.abs((double)(this.startX - y));
double radius = Math.sqrt(Math.pow(distanceX, 2.0) + Math.pow(distanceY, 2.0));
path.reset();
path.addCircle(this.startX, this.startY, (float)radius, Path.Direction.CCW);
break;
case ELLIPSE :
RectF rect = new RectF(this.startX, this.startY, x, y);
path.reset();
path.addOval(rect, Path.Direction.CCW);
break;
default :
break;
}
} else {
if (!isDown) {
return;
}
Path path = this.getCurrentPath();
path.reset();
path.moveTo(this.startX, this.startY);
path.quadTo(this.controlX, this.controlY, x, y);
}
break;
case TEXT :
this.startX = x;
this.startY = y;
break;
default :
break;
}
}
/**
* This method defines processes on MotionEvent.ACTION_DOWN
*
* @param event This is argument of onTouchEvent method
*/
private void onActionUp(MotionEvent event) {
if (isDown) {
this.startX = 0F;
this.startY = 0F;
this.isDown = false;
}
}
/**
* This method updates the instance of Canvas (View)
*
* @param canvas the new instance of Canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Before "drawPath"
canvas.drawColor(this.baseColor);
if (this.bitmap != null) {
canvas.drawBitmap(this.bitmap, 0F, 0F, emptyPaint);
}
for (int i = 0; i < this.historyPointer; i++) {
Path path = this.pathLists.get(i);
Paint paint = this.paintLists.get(i);
canvas.drawPath(path, paint);
}
this.drawText(canvas);
this.canvas = canvas;
}
/**
* This method set event listener for drawing.
*
* @param event the instance of MotionEvent
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.onActionDown(event);
break;
case MotionEvent.ACTION_MOVE :
this.onActionMove(event);
break;
case MotionEvent.ACTION_UP :
this.onActionUp(event);
break;
default :
break;
}
// Re draw
this.invalidate();
return true;
}
/**
* This method is getter for mode.
*
* @return
*/
public Mode getMode() {
return this.mode;
}
/**
* This method is setter for mode.
*
* @param mode
*/
public void setMode(Mode mode) {
this.mode = mode;
}
/**
* This method is getter for drawer.
*
* @return
*/
public Drawer getDrawer() {
return this.drawer;
}
/**
* This method is setter for drawer.
*
* @param drawer
*/
public void setDrawer(Drawer drawer) {
this.drawer = drawer;
}
/**
* This method checks if Undo is available
*
* @return If Undo is available, this is returned as true. Otherwise, this is returned as false.
*/
public boolean canUndo() {
return this.historyPointer > 1;
}
/**
* This method checks if Redo is available
*
* @return If Redo is available, this is returned as true. Otherwise, this is returned as false.
*/
public boolean canRedo() {
return this.historyPointer < this.pathLists.size();
}
/**
* This method draws canvas again for Undo.
*
* @return If Undo is enabled, this is returned as true. Otherwise, this is returned as false.
*/
public boolean undo() {
if (canUndo()) {
this.historyPointer--;
this.invalidate();
return true;
} else {
return false;
}
}
/**
* This method draws canvas again for Redo.
*
* @return If Redo is enabled, this is returned as true. Otherwise, this is returned as false.
*/
public boolean redo() {
if (canRedo()) {
this.historyPointer++;
this.invalidate();
return true;
} else {
return false;
}
}
/**
* This method initializes canvas.
*
* @return
*/
public void clear() {
Path path = new Path();
path.moveTo(0F, 0F);
path.addRect(0F, 0F, 1000F, 1000F, Path.Direction.CCW);
path.close();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
if (this.historyPointer == this.pathLists.size()) {
this.pathLists.add(path);
this.paintLists.add(paint);
this.historyPointer++;
} else {
// On the way of Undo or Redo
this.pathLists.set(this.historyPointer, path);
this.paintLists.set(this.historyPointer, paint);
this.historyPointer++;
for (int i = this.historyPointer, size = this.paintLists.size(); i < size; i++) {
this.pathLists.remove(this.historyPointer);
this.paintLists.remove(this.historyPointer);
}
}
this.text = "";
// Clear
this.invalidate();
}
/**
* This method is getter for canvas background color
*
* @return
*/
public int getBaseColor() {
return this.baseColor;
}
/**
* This method is setter for canvas background color
*
* @param color
*/
public void setBaseColor(int color) {
this.baseColor = color;
}
/**
* This method is getter for drawn text.
*
* @return
*/
public String getText() {
return this.text;
}
/**
* This method is setter for drawn text.
*
* @param text
*/
public void setText(String text) {
this.text = text;
}
/**
* This method is getter for stroke or fill.
*
* @return
*/
public Paint.Style getPaintStyle() {
return this.paintStyle;
}
/**
* This method is setter for stroke or fill.
*
* @param style
*/
public void setPaintStyle(Paint.Style style) {
this.paintStyle = style;
}
/**
* This method is getter for stroke color.
*
* @return
*/
public int getPaintStrokeColor() {
return this.paintStrokeColor;
}
/**
* This method is setter for stroke color.
*
* @param color
*/
public void setPaintStrokeColor(int color) {
this.paintStrokeColor = color;
}
/**
* This method is getter for fill color.
* But, current Android API cannot set fill color (?).
*
* @return
*/
public int getPaintFillColor() {
return this.paintFillColor;
};
/**
* This method is setter for fill color.
* But, current Android API cannot set fill color (?).
*
* @param color
*/
public void setPaintFillColor(int color) {
this.paintFillColor = color;
}
/**
* This method is getter for stroke width.
*
* @return
*/
public float getPaintStrokeWidth() {
return this.paintStrokeWidth;
}
/**
* This method is setter for stroke width.
*
* @param width
*/
public void setPaintStrokeWidth(float width) {
if (width >= 0) {
this.paintStrokeWidth = width;
} else {
this.paintStrokeWidth = 3F;
}
}
/**
* This method is getter for alpha.
*
* @return
*/
public int getOpacity() {
return this.opacity;
}
/**
* This method is setter for alpha.
* The 1st argument must be between 0 and 255.
*
* @param opacity
*/
public void setOpacity(int opacity) {
if ((opacity >= 0) && (opacity <= 255)) {
this.opacity = opacity;
} else {
this.opacity= 255;
}
}
/**
* This method is getter for amount of blur.
*
* @return
*/
public float getBlur() {
return this.blur;
}
/**
* This method is setter for amount of blur.
* The 1st argument is greater than or equal to 0.0.
*
* @param blur
*/
public void setBlur(float blur) {
if (blur >= 0) {
this.blur = blur;
} else {
this.blur = 0F;
}
}
/**
* This method is getter for line cap.
*
* @return
*/
public Paint.Cap getLineCap() {
return this.lineCap;
}
/**
* This method is setter for line cap.
*
* @param cap
*/
public void setLineCap(Paint.Cap cap) {
this.lineCap = cap;
}
/**
* This method is getter for path effect of drawing.
*
* @return drawPathEffect
*/
public PathEffect getDrawPathEffect() {
return drawPathEffect;
}
/**
* This method is setter for path effect of drawing.
*
* @param drawPathEffect
*/
public void setDrawPathEffect(PathEffect drawPathEffect) {
this.drawPathEffect = drawPathEffect;
}
/**
* This method is getter for font size,
*
* @return
*/
public float getFontSize() {
return this.fontSize;
}
/**
* This method is setter for font size.
* The 1st argument is greater than or equal to 0.0.
*
* @param size
*/
public void setFontSize(float size) {
if (size >= 0F) {
this.fontSize = size;
} else {
this.fontSize = 32F;
}
}
/**
* This method is getter for font-family.
*
* @return
*/
public Typeface getFontFamily() {
return this.fontFamily;
}
/**
* This method is setter for font-family.
*
* @param face
*/
public void setFontFamily(Typeface face) {
this.fontFamily = face;
}
/**
* This method gets current canvas as bitmap.
*
* @return This is returned as bitmap.
*/
public Bitmap getBitmap() {
this.setDrawingCacheEnabled(false);
this.setDrawingCacheEnabled(true);
return Bitmap.createBitmap(this.getDrawingCache());
}
/**
* This method gets current canvas as scaled bitmap.
*
* @return This is returned as scaled bitmap.
*/
public Bitmap getScaleBitmap(int w, int h) {
this.setDrawingCacheEnabled(false);
this.setDrawingCacheEnabled(true);
return Bitmap.createScaledBitmap(this.getDrawingCache(), w, h, true);
}
/**
* This method draws the designated bitmap to canvas.
*
* @param bitmap
*/
public void drawBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
this.invalidate();
}
/**
* This method draws the designated byte array of bitmap to canvas.
*
* @param byteArray This is returned as byte array of bitmap.
*/
public void drawBitmap(byte[] byteArray) {
this.drawBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length));
}
/**
* This static method gets the designated bitmap as byte array.
*
* @param bitmap
* @param format
* @param quality
* @return This is returned as byte array of bitmap.
*/
public static byte[] getBitmapAsByteArray(Bitmap bitmap, CompressFormat format, int quality) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(format, quality, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
/**
* This method gets the bitmap as byte array.
*
* @param format
* @param quality
* @return This is returned as byte array of bitmap.
*/
public byte[] getBitmapAsByteArray(CompressFormat format, int quality) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
this.getBitmap().compress(format, quality, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
/**
* This method gets the bitmap as byte array.
* Bitmap format is PNG, and quality is 100.
*
* @return This is returned as byte array of bitmap.
*/
public byte[] getBitmapAsByteArray() {
return this.getBitmapAsByteArray(CompressFormat.PNG, 100);
}
}