-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas.java
More file actions
2109 lines (1943 loc) · 87.9 KB
/
Copy pathCanvas.java
File metadata and controls
2109 lines (1943 loc) · 87.9 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
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.text.GraphicsOperations;
import android.text.SpannableString;
import android.text.SpannedString;
import android.text.TextUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.microedition.khronos.opengles.GL;
/**
* The Canvas class holds the "draw" calls. To draw something, you need
* 4 basic components: A Bitmap to hold the pixels, a Canvas to host
* the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
* Path, text, Bitmap), and a paint (to describe the colors and styles for the
* drawing).
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For more information about how to use Canvas, read the
* <a href="{@docRoot}guide/topics/graphics/2d-graphics.html">
* Canvas and Drawables</a> developer guide.</p></div>
*/
public class Canvas {
// assigned in constructors or setBitmap, freed in finalizer
private long mNativeCanvasWrapper;
/** @hide */
public long getNativeCanvasWrapper() {
return mNativeCanvasWrapper;
}
/** @hide */
public boolean isRecordingFor(Object o) { return false; }
// may be null
private Bitmap mBitmap;
// optional field set by the caller
private DrawFilter mDrawFilter;
/**
* @hide
*/
protected int mDensity = Bitmap.DENSITY_NONE;
/**
* Used to determine when compatibility scaling is in effect.
*
* @hide
*/
protected int mScreenDensity = Bitmap.DENSITY_NONE;
// Used by native code
@SuppressWarnings("UnusedDeclaration")
private int mSurfaceFormat;
/**
* Flag for drawTextRun indicating left-to-right run direction.
* @hide
*/
public static final int DIRECTION_LTR = 0;
/**
* Flag for drawTextRun indicating right-to-left run direction.
* @hide
*/
public static final int DIRECTION_RTL = 1;
// Maximum bitmap size as defined in Skia's native code
// (see SkCanvas.cpp, SkDraw.cpp)
private static final int MAXMIMUM_BITMAP_SIZE = 32766;
// This field is used to finalize the native Canvas properly
private final CanvasFinalizer mFinalizer;
private static final class CanvasFinalizer {
private long mNativeCanvasWrapper;
public CanvasFinalizer(long nativeCanvas) {
mNativeCanvasWrapper = nativeCanvas;
}
@Override
protected void finalize() throws Throwable {
try {
dispose();
} finally {
super.finalize();
}
}
public void dispose() {
if (mNativeCanvasWrapper != 0) {
finalizer(mNativeCanvasWrapper);
mNativeCanvasWrapper = 0;
}
}
}
/**
* Construct an empty raster canvas. Use setBitmap() to specify a bitmap to
* draw into. The initial target density is {@link Bitmap#DENSITY_NONE};
* this will typically be replaced when a target bitmap is set for the
* canvas.
*/
public Canvas() {
if (!isHardwareAccelerated()) {
// 0 means no native bitmap
mNativeCanvasWrapper = initRaster(0);
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
} else {
mFinalizer = null;
}
}
/**
* Construct a canvas with the specified bitmap to draw into. The bitmap
* must be mutable.
*
* <p>The initial target density of the canvas is the same as the given
* bitmap's density.
*
* @param bitmap Specifies a mutable bitmap for the canvas to draw into.
*/
public Canvas(@NonNull Bitmap bitmap) {
if (!bitmap.isMutable()) {
throw new IllegalStateException("Immutable bitmap passed to Canvas constructor");
}
throwIfCannotDraw(bitmap);
mNativeCanvasWrapper = initRaster(bitmap.ni());
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
mBitmap = bitmap;
mDensity = bitmap.mDensity;
}
/** @hide */
public Canvas(long nativeCanvas) {
if (nativeCanvas == 0) {
throw new IllegalStateException();
}
mNativeCanvasWrapper = nativeCanvas;
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
mDensity = Bitmap.getDefaultDensity();
}
/**
* Returns null.
*
* @deprecated This method is not supported and should not be invoked.
*
* @hide
*/
@Deprecated
protected GL getGL() {
return null;
}
/**
* Indicates whether this Canvas uses hardware acceleration.
*
* Note that this method does not define what type of hardware acceleration
* may or may not be used.
*
* @return True if drawing operations are hardware accelerated,
* false otherwise.
*/
public boolean isHardwareAccelerated() {
return false;
}
/**
* Specify a bitmap for the canvas to draw into. All canvas state such as
* layers, filters, and the save/restore stack are reset with the exception
* of the current matrix and clip stack. Additionally, as a side-effect
* the canvas' target density is updated to match that of the bitmap.
*
* @param bitmap Specifies a mutable bitmap for the canvas to draw into.
* @see #setDensity(int)
* @see #getDensity()
*/
public void setBitmap(@Nullable Bitmap bitmap) {
if (isHardwareAccelerated()) {
throw new RuntimeException("Can't set a bitmap device on a HW accelerated canvas");
}
if (bitmap == null) {
native_setBitmap(mNativeCanvasWrapper, 0, false);
mDensity = Bitmap.DENSITY_NONE;
} else {
if (!bitmap.isMutable()) {
throw new IllegalStateException();
}
throwIfCannotDraw(bitmap);
native_setBitmap(mNativeCanvasWrapper, bitmap.ni(), true);
mDensity = bitmap.mDensity;
}
mBitmap = bitmap;
}
/**
* setBitmap() variant for native callers with a raw bitmap handle.
*/
private void setNativeBitmap(long bitmapHandle) {
native_setBitmap(mNativeCanvasWrapper, bitmapHandle, false);
}
/**
* Set the viewport dimensions if this canvas is GL based. If it is not,
* this method is ignored and no exception is thrown.
*
* @param width The width of the viewport
* @param height The height of the viewport
*
* @hide
*/
public void setViewport(int width, int height) {}
/** @hide */
public void setHighContrastText(boolean highContrastText) {}
/** @hide */
public void insertReorderBarrier() {}
/** @hide */
public void insertInorderBarrier() {}
/**
* Return true if the device that the current layer draws into is opaque
* (i.e. does not support per-pixel alpha).
*
* @return true if the device that the current layer draws into is opaque
*/
public boolean isOpaque() {
return native_isOpaque(mNativeCanvasWrapper);
}
/**
* Returns the width of the current drawing layer
*
* @return the width of the current drawing layer
*/
public int getWidth() {
return native_getWidth(mNativeCanvasWrapper);
}
/**
* Returns the height of the current drawing layer
*
* @return the height of the current drawing layer
*/
public int getHeight() {
return native_getHeight(mNativeCanvasWrapper);
}
/**
* <p>Returns the target density of the canvas. The default density is
* derived from the density of its backing bitmap, or
* {@link Bitmap#DENSITY_NONE} if there is not one.</p>
*
* @return Returns the current target density of the canvas, which is used
* to determine the scaling factor when drawing a bitmap into it.
*
* @see #setDensity(int)
* @see Bitmap#getDensity()
*/
public int getDensity() {
return mDensity;
}
/**
* <p>Specifies the density for this Canvas' backing bitmap. This modifies
* the target density of the canvas itself, as well as the density of its
* backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}.
*
* @param density The new target density of the canvas, which is used
* to determine the scaling factor when drawing a bitmap into it. Use
* {@link Bitmap#DENSITY_NONE} to disable bitmap scaling.
*
* @see #getDensity()
* @see Bitmap#setDensity(int)
*/
public void setDensity(int density) {
if (mBitmap != null) {
mBitmap.setDensity(density);
}
mDensity = density;
}
/** @hide */
public void setScreenDensity(int density) {
mScreenDensity = density;
}
/**
* Returns the maximum allowed width for bitmaps drawn with this canvas.
* Attempting to draw with a bitmap wider than this value will result
* in an error.
*
* @see #getMaximumBitmapHeight()
*/
public int getMaximumBitmapWidth() {
return MAXMIMUM_BITMAP_SIZE;
}
/**
* Returns the maximum allowed height for bitmaps drawn with this canvas.
* Attempting to draw with a bitmap taller than this value will result
* in an error.
*
* @see #getMaximumBitmapWidth()
*/
public int getMaximumBitmapHeight() {
return MAXMIMUM_BITMAP_SIZE;
}
// the SAVE_FLAG constants must match their native equivalents
/** @hide */
@IntDef(flag = true,
value = {
MATRIX_SAVE_FLAG,
CLIP_SAVE_FLAG,
HAS_ALPHA_LAYER_SAVE_FLAG,
FULL_COLOR_LAYER_SAVE_FLAG,
CLIP_TO_LAYER_SAVE_FLAG,
ALL_SAVE_FLAG
})
@Retention(RetentionPolicy.SOURCE)
public @interface Saveflags {}
/**
* Restore the current matrix when restore() is called.
*/
public static final int MATRIX_SAVE_FLAG = 0x01;
/**
* Restore the current clip when restore() is called.
*/
public static final int CLIP_SAVE_FLAG = 0x02;
/**
* The layer requires a per-pixel alpha channel.
*/
public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04;
/**
* The layer requires full 8-bit precision for each color channel.
*/
public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08;
/**
* Clip drawing to the bounds of the offscreen layer, omit at your own peril.
* <p class="note"><strong>Note:</strong> it is strongly recommended to not
* omit this flag for any call to <code>saveLayer()</code> and
* <code>saveLayerAlpha()</code> variants. Not passing this flag generally
* triggers extremely poor performance with hardware accelerated rendering.
*/
public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10;
/**
* Restore everything when restore() is called (standard save flags).
* <p class="note"><strong>Note:</strong> for performance reasons, it is
* strongly recommended to pass this - the complete set of flags - to any
* call to <code>saveLayer()</code> and <code>saveLayerAlpha()</code>
* variants.
*/
public static final int ALL_SAVE_FLAG = 0x1F;
/**
* Saves the current matrix and clip onto a private stack.
* <p>
* Subsequent calls to translate,scale,rotate,skew,concat or clipRect,
* clipPath will all operate as usual, but when the balancing call to
* restore() is made, those calls will be forgotten, and the settings that
* existed before the save() will be reinstated.
*
* @return The value to pass to restoreToCount() to balance this save()
*/
public int save() {
return native_save(mNativeCanvasWrapper, MATRIX_SAVE_FLAG | CLIP_SAVE_FLAG);
}
/**
* Based on saveFlags, can save the current matrix and clip onto a private
* stack.
* <p class="note"><strong>Note:</strong> if possible, use the
* parameter-less save(). It is simpler and faster than individually
* disabling the saving of matrix or clip with this method.
* <p>
* Subsequent calls to translate,scale,rotate,skew,concat or clipRect,
* clipPath will all operate as usual, but when the balancing call to
* restore() is made, those calls will be forgotten, and the settings that
* existed before the save() will be reinstated.
*
* @param saveFlags flag bits that specify which parts of the Canvas state
* to save/restore
* @return The value to pass to restoreToCount() to balance this save()
*/
public int save(@Saveflags int saveFlags) {
return native_save(mNativeCanvasWrapper, saveFlags);
}
/**
* This behaves the same as save(), but in addition it allocates and
* redirects drawing to an offscreen bitmap.
* <p class="note"><strong>Note:</strong> this method is very expensive,
* incurring more than double rendering cost for contained content. Avoid
* using this method, especially if the bounds provided are large, or if
* the {@link #CLIP_TO_LAYER_SAVE_FLAG} is omitted from the
* {@code saveFlags} parameter. It is recommended to use a
* {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View
* to apply an xfermode, color filter, or alpha, as it will perform much
* better than this method.
* <p>
* All drawing calls are directed to a newly allocated offscreen bitmap.
* Only when the balancing call to restore() is made, is that offscreen
* buffer drawn back to the current target of the Canvas (either the
* screen, it's target Bitmap, or the previous layer).
* <p>
* Attributes of the Paint - {@link Paint#getAlpha() alpha},
* {@link Paint#getXfermode() Xfermode}, and
* {@link Paint#getColorFilter() ColorFilter} are applied when the
* offscreen bitmap is drawn back when restore() is called.
*
* @param bounds May be null. The maximum size the offscreen bitmap
* needs to be (in local coordinates)
* @param paint This is copied, and is applied to the offscreen when
* restore() is called.
* @param saveFlags see _SAVE_FLAG constants, generally {@link #ALL_SAVE_FLAG} is recommended
* for performance reasons.
* @return value to pass to restoreToCount() to balance this save()
*/
public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags) {
if (bounds == null) {
bounds = new RectF(getClipBounds());
}
return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
}
/**
* Convenience for saveLayer(bounds, paint, {@link #ALL_SAVE_FLAG})
*/
public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint) {
return saveLayer(bounds, paint, ALL_SAVE_FLAG);
}
/**
* Helper version of saveLayer() that takes 4 values rather than a RectF.
*/
public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint,
@Saveflags int saveFlags) {
return native_saveLayer(mNativeCanvasWrapper, left, top, right, bottom,
paint != null ? paint.mNativePaint : 0,
saveFlags);
}
/**
* Convenience for saveLayer(left, top, right, bottom, paint, {@link #ALL_SAVE_FLAG})
*/
public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint) {
return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG);
}
/**
* This behaves the same as save(), but in addition it allocates and
* redirects drawing to an offscreen bitmap.
* <p class="note"><strong>Note:</strong> this method is very expensive,
* incurring more than double rendering cost for contained content. Avoid
* using this method, especially if the bounds provided are large, or if
* the {@link #CLIP_TO_LAYER_SAVE_FLAG} is omitted from the
* {@code saveFlags} parameter. It is recommended to use a
* {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View
* to apply an xfermode, color filter, or alpha, as it will perform much
* better than this method.
* <p>
* All drawing calls are directed to a newly allocated offscreen bitmap.
* Only when the balancing call to restore() is made, is that offscreen
* buffer drawn back to the current target of the Canvas (either the
* screen, it's target Bitmap, or the previous layer).
* <p>
* The {@code alpha} parameter is applied when the offscreen bitmap is
* drawn back when restore() is called.
*
* @param bounds The maximum size the offscreen bitmap needs to be
* (in local coordinates)
* @param alpha The alpha to apply to the offscreen when when it is
drawn during restore()
* @param saveFlags see _SAVE_FLAG constants, generally {@link #ALL_SAVE_FLAG} is recommended
* for performance reasons.
* @return value to pass to restoreToCount() to balance this call
*/
public int saveLayerAlpha(@Nullable RectF bounds, int alpha, @Saveflags int saveFlags) {
if (bounds == null) {
bounds = new RectF(getClipBounds());
}
return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, alpha, saveFlags);
}
/**
* Convenience for saveLayerAlpha(bounds, alpha, {@link #ALL_SAVE_FLAG})
*/
public int saveLayerAlpha(@Nullable RectF bounds, int alpha) {
return saveLayerAlpha(bounds, alpha, ALL_SAVE_FLAG);
}
/**
* Helper for saveLayerAlpha() that takes 4 values instead of a RectF.
*/
public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
@Saveflags int saveFlags) {
alpha = Math.min(255, Math.max(0, alpha));
return native_saveLayerAlpha(mNativeCanvasWrapper, left, top, right, bottom,
alpha, saveFlags);
}
/**
* Helper for saveLayerAlpha(left, top, right, bottom, alpha, {@link #ALL_SAVE_FLAG})
*/
public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
return saveLayerAlpha(left, top, right, bottom, alpha, ALL_SAVE_FLAG);
}
/**
* This call balances a previous call to save(), and is used to remove all
* modifications to the matrix/clip state since the last save call. It is
* an error to call restore() more times than save() was called.
*/
public void restore() {
native_restore(mNativeCanvasWrapper);
}
/**
* Returns the number of matrix/clip states on the Canvas' private stack.
* This will equal # save() calls - # restore() calls.
*/
public int getSaveCount() {
return native_getSaveCount(mNativeCanvasWrapper);
}
/**
* Efficient way to pop any calls to save() that happened after the save
* count reached saveCount. It is an error for saveCount to be less than 1.
*
* Example:
* int count = canvas.save();
* ... // more calls potentially to save()
* canvas.restoreToCount(count);
* // now the canvas is back in the same state it was before the initial
* // call to save().
*
* @param saveCount The save level to restore to.
*/
public void restoreToCount(int saveCount) {
native_restoreToCount(mNativeCanvasWrapper, saveCount);
}
/**
* Preconcat the current matrix with the specified translation
*
* @param dx The distance to translate in X
* @param dy The distance to translate in Y
*/
public void translate(float dx, float dy) {
native_translate(mNativeCanvasWrapper, dx, dy);
}
/**
* Preconcat the current matrix with the specified scale.
*
* @param sx The amount to scale in X
* @param sy The amount to scale in Y
*/
public void scale(float sx, float sy) {
native_scale(mNativeCanvasWrapper, sx, sy);
}
/**
* Preconcat the current matrix with the specified scale.
*
* @param sx The amount to scale in X
* @param sy The amount to scale in Y
* @param px The x-coord for the pivot point (unchanged by the scale)
* @param py The y-coord for the pivot point (unchanged by the scale)
*/
public final void scale(float sx, float sy, float px, float py) {
translate(px, py);
scale(sx, sy);
translate(-px, -py);
}
/**
* Preconcat the current matrix with the specified rotation.
*
* @param degrees The amount to rotate, in degrees
*/
public void rotate(float degrees) {
native_rotate(mNativeCanvasWrapper, degrees);
}
/**
* Preconcat the current matrix with the specified rotation.
*
* @param degrees The amount to rotate, in degrees
* @param px The x-coord for the pivot point (unchanged by the rotation)
* @param py The y-coord for the pivot point (unchanged by the rotation)
*/
public final void rotate(float degrees, float px, float py) {
translate(px, py);
rotate(degrees);
translate(-px, -py);
}
/**
* Preconcat the current matrix with the specified skew.
*
* @param sx The amount to skew in X
* @param sy The amount to skew in Y
*/
public void skew(float sx, float sy) {
native_skew(mNativeCanvasWrapper, sx, sy);
}
/**
* Preconcat the current matrix with the specified matrix. If the specified
* matrix is null, this method does nothing.
*
* @param matrix The matrix to preconcatenate with the current matrix
*/
public void concat(@Nullable Matrix matrix) {
if (matrix != null) native_concat(mNativeCanvasWrapper, matrix.native_instance);
}
/**
* Completely replace the current matrix with the specified matrix. If the
* matrix parameter is null, then the current matrix is reset to identity.
*
* <strong>Note:</strong> it is recommended to use {@link #concat(Matrix)},
* {@link #scale(float, float)}, {@link #translate(float, float)} and
* {@link #rotate(float)} instead of this method.
*
* @param matrix The matrix to replace the current matrix with. If it is
* null, set the current matrix to identity.
*
* @see #concat(Matrix)
*/
public void setMatrix(@Nullable Matrix matrix) {
native_setMatrix(mNativeCanvasWrapper,
matrix == null ? 0 : matrix.native_instance);
}
/**
* Return, in ctm, the current transformation matrix. This does not alter
* the matrix in the canvas, but just returns a copy of it.
*/
@Deprecated
public void getMatrix(@NonNull Matrix ctm) {
native_getCTM(mNativeCanvasWrapper, ctm.native_instance);
}
/**
* Return a new matrix with a copy of the canvas' current transformation
* matrix.
*/
@Deprecated
public final @NonNull Matrix getMatrix() {
Matrix m = new Matrix();
//noinspection deprecation
getMatrix(m);
return m;
}
/**
* Modify the current clip with the specified rectangle.
*
* @param rect The rect to intersect with the current clip
* @param op How the clip is modified
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(@NonNull RectF rect, @NonNull Region.Op op) {
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
op.nativeInt);
}
/**
* Modify the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param rect The rectangle to intersect with the current clip.
* @param op How the clip is modified
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(@NonNull Rect rect, @NonNull Region.Op op) {
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
op.nativeInt);
}
/**
* Intersect the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param rect The rectangle to intersect with the current clip.
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(@NonNull RectF rect) {
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
Region.Op.INTERSECT.nativeInt);
}
/**
* Intersect the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param rect The rectangle to intersect with the current clip.
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(@NonNull Rect rect) {
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
Region.Op.INTERSECT.nativeInt);
}
/**
* Modify the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param left The left side of the rectangle to intersect with the
* current clip
* @param top The top of the rectangle to intersect with the current
* clip
* @param right The right side of the rectangle to intersect with the
* current clip
* @param bottom The bottom of the rectangle to intersect with the current
* clip
* @param op How the clip is modified
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(float left, float top, float right, float bottom,
@NonNull Region.Op op) {
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, op.nativeInt);
}
/**
* Intersect the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param left The left side of the rectangle to intersect with the
* current clip
* @param top The top of the rectangle to intersect with the current clip
* @param right The right side of the rectangle to intersect with the
* current clip
* @param bottom The bottom of the rectangle to intersect with the current
* clip
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(float left, float top, float right, float bottom) {
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
Region.Op.INTERSECT.nativeInt);
}
/**
* Intersect the current clip with the specified rectangle, which is
* expressed in local coordinates.
*
* @param left The left side of the rectangle to intersect with the
* current clip
* @param top The top of the rectangle to intersect with the current clip
* @param right The right side of the rectangle to intersect with the
* current clip
* @param bottom The bottom of the rectangle to intersect with the current
* clip
* @return true if the resulting clip is non-empty
*/
public boolean clipRect(int left, int top, int right, int bottom) {
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
Region.Op.INTERSECT.nativeInt);
}
/**
* Modify the current clip with the specified path.
*
* @param path The path to operate on the current clip
* @param op How the clip is modified
* @return true if the resulting is non-empty
*/
public boolean clipPath(@NonNull Path path, @NonNull Region.Op op) {
return native_clipPath(mNativeCanvasWrapper, path.ni(), op.nativeInt);
}
/**
* Intersect the current clip with the specified path.
*
* @param path The path to intersect with the current clip
* @return true if the resulting is non-empty
*/
public boolean clipPath(@NonNull Path path) {
return clipPath(path, Region.Op.INTERSECT);
}
/**
* Modify the current clip with the specified region. Note that unlike
* clipRect() and clipPath() which transform their arguments by the
* current matrix, clipRegion() assumes its argument is already in the
* coordinate system of the current layer's bitmap, and so not
* transformation is performed.
*
* @param region The region to operate on the current clip, based on op
* @param op How the clip is modified
* @return true if the resulting is non-empty
*
* @deprecated Unlike all other clip calls this API does not respect the
* current matrix. Use {@link #clipRect(Rect)} as an alternative.
*/
public boolean clipRegion(@NonNull Region region, @NonNull Region.Op op) {
return native_clipRegion(mNativeCanvasWrapper, region.ni(), op.nativeInt);
}
/**
* Intersect the current clip with the specified region. Note that unlike
* clipRect() and clipPath() which transform their arguments by the
* current matrix, clipRegion() assumes its argument is already in the
* coordinate system of the current layer's bitmap, and so not
* transformation is performed.
*
* @param region The region to operate on the current clip, based on op
* @return true if the resulting is non-empty
*
* @deprecated Unlike all other clip calls this API does not respect the
* current matrix. Use {@link #clipRect(Rect)} as an alternative.
*/
public boolean clipRegion(@NonNull Region region) {
return clipRegion(region, Region.Op.INTERSECT);
}
public @Nullable DrawFilter getDrawFilter() {
return mDrawFilter;
}
public void setDrawFilter(@Nullable DrawFilter filter) {
long nativeFilter = 0;
if (filter != null) {
nativeFilter = filter.mNativeInt;
}
mDrawFilter = filter;
nativeSetDrawFilter(mNativeCanvasWrapper, nativeFilter);
}
public enum EdgeType {
/**
* Black-and-White: Treat edges by just rounding to nearest pixel boundary
*/
BW(0), //!< treat edges by just rounding to nearest pixel boundary
/**
* Antialiased: Treat edges by rounding-out, since they may be antialiased
*/
AA(1);
EdgeType(int nativeInt) {
this.nativeInt = nativeInt;
}
/**
* @hide
*/
public final int nativeInt;
}
/**
* Return true if the specified rectangle, after being transformed by the
* current matrix, would lie completely outside of the current clip. Call
* this to check if an area you intend to draw into is clipped out (and
* therefore you can skip making the draw calls).
*
* @param rect the rect to compare with the current clip
* @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
* since that means it may affect a larger area (more pixels) than
* non-antialiased ({@link Canvas.EdgeType#BW}).
* @return true if the rect (transformed by the canvas' matrix)
* does not intersect with the canvas' clip
*/
public boolean quickReject(@NonNull RectF rect, @NonNull EdgeType type) {
return native_quickReject(mNativeCanvasWrapper,
rect.left, rect.top, rect.right, rect.bottom);
}
/**
* Return true if the specified path, after being transformed by the
* current matrix, would lie completely outside of the current clip. Call
* this to check if an area you intend to draw into is clipped out (and
* therefore you can skip making the draw calls). Note: for speed it may
* return false even if the path itself might not intersect the clip
* (i.e. the bounds of the path intersects, but the path does not).
*
* @param path The path to compare with the current clip
* @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
* since that means it may affect a larger area (more pixels) than
* non-antialiased ({@link Canvas.EdgeType#BW}).
* @return true if the path (transformed by the canvas' matrix)
* does not intersect with the canvas' clip
*/
public boolean quickReject(@NonNull Path path, @NonNull EdgeType type) {
return native_quickReject(mNativeCanvasWrapper, path.ni());
}
/**
* Return true if the specified rectangle, after being transformed by the
* current matrix, would lie completely outside of the current clip. Call
* this to check if an area you intend to draw into is clipped out (and
* therefore you can skip making the draw calls).
*
* @param left The left side of the rectangle to compare with the
* current clip
* @param top The top of the rectangle to compare with the current
* clip
* @param right The right side of the rectangle to compare with the
* current clip
* @param bottom The bottom of the rectangle to compare with the
* current clip
* @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
* since that means it may affect a larger area (more pixels) than
* non-antialiased ({@link Canvas.EdgeType#BW}).
* @return true if the rect (transformed by the canvas' matrix)
* does not intersect with the canvas' clip
*/
public boolean quickReject(float left, float top, float right, float bottom,
@NonNull EdgeType type) {
return native_quickReject(mNativeCanvasWrapper, left, top, right, bottom);
}
/**
* Return the bounds of the current clip (in local coordinates) in the
* bounds parameter, and return true if it is non-empty. This can be useful
* in a way similar to quickReject, in that it tells you that drawing
* outside of these bounds will be clipped out.
*
* @param bounds Return the clip bounds here. If it is null, ignore it but
* still return true if the current clip is non-empty.
* @return true if the current clip is non-empty.
*/
public boolean getClipBounds(@Nullable Rect bounds) {
return native_getClipBounds(mNativeCanvasWrapper, bounds);
}
/**
* Retrieve the bounds of the current clip (in local coordinates).
*
* @return the clip bounds, or [0, 0, 0, 0] if the clip is empty.
*/
public final @NonNull Rect getClipBounds() {
Rect r = new Rect();
getClipBounds(r);
return r;
}
/**
* Fill the entire canvas' bitmap (restricted to the current clip) with the
* specified RGB color, using srcover porterduff mode.
*
* @param r red component (0..255) of the color to draw onto the canvas
* @param g green component (0..255) of the color to draw onto the canvas
* @param b blue component (0..255) of the color to draw onto the canvas
*/
public void drawRGB(int r, int g, int b) {
drawColor(Color.rgb(r, g, b));
}
/**
* Fill the entire canvas' bitmap (restricted to the current clip) with the
* specified ARGB color, using srcover porterduff mode.
*
* @param a alpha component (0..255) of the color to draw onto the canvas
* @param r red component (0..255) of the color to draw onto the canvas
* @param g green component (0..255) of the color to draw onto the canvas
* @param b blue component (0..255) of the color to draw onto the canvas
*/