1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics; 18 19 import android.annotation.ColorInt; 20 import android.annotation.ColorLong; 21 import android.annotation.IntDef; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.Size; 25 import android.compat.annotation.UnsupportedAppUsage; 26 import android.graphics.text.MeasuredText; 27 import android.os.Build; 28 29 import dalvik.annotation.optimization.CriticalNative; 30 import dalvik.annotation.optimization.FastNative; 31 32 import libcore.util.NativeAllocationRegistry; 33 34 import java.lang.annotation.Retention; 35 import java.lang.annotation.RetentionPolicy; 36 37 import javax.microedition.khronos.opengles.GL; 38 39 /** 40 * The Canvas class holds the "draw" calls. To draw something, you need 41 * 4 basic components: A Bitmap to hold the pixels, a Canvas to host 42 * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, 43 * Path, text, Bitmap), and a paint (to describe the colors and styles for the 44 * drawing). 45 * 46 * <div class="special reference"> 47 * <h3>Developer Guides</h3> 48 * <p>For more information about how to use Canvas, read the 49 * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html"> 50 * Canvas and Drawables</a> developer guide.</p></div> 51 */ 52 public class Canvas extends BaseCanvas { 53 private static int sCompatiblityVersion = 0; 54 /** @hide */ 55 public static boolean sCompatibilityRestore = false; 56 /** @hide */ 57 public static boolean sCompatibilitySetBitmap = false; 58 59 /** @hide */ 60 @UnsupportedAppUsage getNativeCanvasWrapper()61 public long getNativeCanvasWrapper() { 62 return mNativeCanvasWrapper; 63 } 64 65 /** @hide */ isRecordingFor(Object o)66 public boolean isRecordingFor(Object o) { return false; } 67 68 // may be null 69 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 117521088) 70 private Bitmap mBitmap; 71 72 // optional field set by the caller 73 private DrawFilter mDrawFilter; 74 75 // Maximum bitmap size as defined in Skia's native code 76 // (see SkCanvas.cpp, SkDraw.cpp) 77 private static final int MAXMIMUM_BITMAP_SIZE = 32766; 78 79 // Use a Holder to allow static initialization of Canvas in the boot image. 80 private static class NoImagePreloadHolder { 81 public static final NativeAllocationRegistry sRegistry = 82 NativeAllocationRegistry.createMalloced( 83 Canvas.class.getClassLoader(), nGetNativeFinalizer()); 84 } 85 86 // This field is used to finalize the native Canvas properly 87 private Runnable mFinalizer; 88 89 /** 90 * Construct an empty raster canvas. Use setBitmap() to specify a bitmap to 91 * draw into. The initial target density is {@link Bitmap#DENSITY_NONE}; 92 * this will typically be replaced when a target bitmap is set for the 93 * canvas. 94 */ Canvas()95 public Canvas() { 96 if (!isHardwareAccelerated()) { 97 // 0 means no native bitmap 98 mNativeCanvasWrapper = nInitRaster(0); 99 mFinalizer = NoImagePreloadHolder.sRegistry.registerNativeAllocation( 100 this, mNativeCanvasWrapper); 101 } else { 102 mFinalizer = null; 103 } 104 } 105 106 /** 107 * Construct a canvas with the specified bitmap to draw into. The bitmap 108 * must be mutable. 109 * 110 * <p>The initial target density of the canvas is the same as the given 111 * bitmap's density. 112 * 113 * @param bitmap Specifies a mutable bitmap for the canvas to draw into. 114 */ Canvas(@onNull Bitmap bitmap)115 public Canvas(@NonNull Bitmap bitmap) { 116 if (!bitmap.isMutable()) { 117 throw new IllegalStateException("Immutable bitmap passed to Canvas constructor"); 118 } 119 throwIfCannotDraw(bitmap); 120 mNativeCanvasWrapper = nInitRaster(bitmap.getNativeInstance()); 121 mFinalizer = NoImagePreloadHolder.sRegistry.registerNativeAllocation( 122 this, mNativeCanvasWrapper); 123 mBitmap = bitmap; 124 mDensity = bitmap.mDensity; 125 } 126 127 /** @hide */ 128 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) Canvas(long nativeCanvas)129 public Canvas(long nativeCanvas) { 130 if (nativeCanvas == 0) { 131 throw new IllegalStateException(); 132 } 133 mNativeCanvasWrapper = nativeCanvas; 134 mFinalizer = NoImagePreloadHolder.sRegistry.registerNativeAllocation( 135 this, mNativeCanvasWrapper); 136 mDensity = Bitmap.getDefaultDensity(); 137 } 138 139 /** 140 * Returns null. 141 * 142 * @deprecated This method is not supported and should not be invoked. 143 * 144 * @hide 145 */ 146 @Deprecated 147 @UnsupportedAppUsage getGL()148 protected GL getGL() { 149 return null; 150 } 151 152 /** 153 * Indicates whether this Canvas uses hardware acceleration. 154 * 155 * Note that this method does not define what type of hardware acceleration 156 * may or may not be used. 157 * 158 * @return True if drawing operations are hardware accelerated, 159 * false otherwise. 160 */ isHardwareAccelerated()161 public boolean isHardwareAccelerated() { 162 return false; 163 } 164 165 /** 166 * Specify a bitmap for the canvas to draw into. All canvas state such as 167 * layers, filters, and the save/restore stack are reset. Additionally, 168 * the canvas' target density is updated to match that of the bitmap. 169 * 170 * Prior to API level {@value Build.VERSION_CODES#O} the current matrix and 171 * clip stack were preserved. 172 * 173 * @param bitmap Specifies a mutable bitmap for the canvas to draw into. 174 * @see #setDensity(int) 175 * @see #getDensity() 176 */ setBitmap(@ullable Bitmap bitmap)177 public void setBitmap(@Nullable Bitmap bitmap) { 178 if (isHardwareAccelerated()) { 179 throw new RuntimeException("Can't set a bitmap device on a HW accelerated canvas"); 180 } 181 182 Matrix preservedMatrix = null; 183 if (bitmap != null && sCompatibilitySetBitmap) { 184 preservedMatrix = getMatrix(); 185 } 186 187 if (bitmap == null) { 188 nSetBitmap(mNativeCanvasWrapper, 0); 189 mDensity = Bitmap.DENSITY_NONE; 190 } else { 191 if (!bitmap.isMutable()) { 192 throw new IllegalStateException(); 193 } 194 throwIfCannotDraw(bitmap); 195 196 nSetBitmap(mNativeCanvasWrapper, bitmap.getNativeInstance()); 197 mDensity = bitmap.mDensity; 198 } 199 200 if (preservedMatrix != null) { 201 setMatrix(preservedMatrix); 202 } 203 204 mBitmap = bitmap; 205 } 206 207 /** 208 * @deprecated use {@link #enableZ()} instead 209 * @hide */ 210 @Deprecated insertReorderBarrier()211 public void insertReorderBarrier() { 212 enableZ(); 213 } 214 215 /** 216 * @deprecated use {@link #disableZ()} instead 217 * @hide */ 218 @Deprecated insertInorderBarrier()219 public void insertInorderBarrier() { 220 disableZ(); 221 } 222 223 /** 224 * <p>Enables Z support which defaults to disabled. This allows for RenderNodes drawn with 225 * {@link #drawRenderNode(RenderNode)} to be re-arranged based off of their 226 * {@link RenderNode#getElevation()} and {@link RenderNode#getTranslationZ()} 227 * values. It also enables rendering of shadows for RenderNodes with an elevation or 228 * translationZ.</p> 229 * 230 * <p>Any draw reordering will not be moved before this call. A typical usage of this might 231 * look something like: 232 * 233 * <pre class="prettyprint"> 234 * void draw(Canvas canvas) { 235 * // Draw any background content 236 * canvas.drawColor(backgroundColor); 237 * 238 * // Begin drawing that may be reordered based off of Z 239 * canvas.enableZ(); 240 * for (RenderNode child : children) { 241 * canvas.drawRenderNode(child); 242 * } 243 * // End drawing that may be reordered based off of Z 244 * canvas.disableZ(); 245 * 246 * // Draw any overlays 247 * canvas.drawText("I'm on top of everything!", 0, 0, paint); 248 * } 249 * </pre> 250 * </p> 251 * 252 * Note: This is not impacted by any {@link #save()} or {@link #restore()} calls as it is not 253 * considered to be part of the current matrix or clip. 254 * 255 * See {@link #disableZ()} 256 */ enableZ()257 public void enableZ() { 258 } 259 260 /** 261 * Disables Z support, preventing any RenderNodes drawn after this point from being 262 * visually reordered or having shadows rendered. 263 * 264 * Note: This is not impacted by any {@link #save()} or {@link #restore()} calls as it is not 265 * considered to be part of the current matrix or clip. 266 * 267 * See {@link #enableZ()} 268 */ disableZ()269 public void disableZ() { 270 } 271 272 /** 273 * Return true if the device that the current layer draws into is opaque 274 * (i.e. does not support per-pixel alpha). 275 * 276 * @return true if the device that the current layer draws into is opaque 277 */ isOpaque()278 public boolean isOpaque() { 279 return nIsOpaque(mNativeCanvasWrapper); 280 } 281 282 /** 283 * Returns the width of the current drawing layer 284 * 285 * @return the width of the current drawing layer 286 */ getWidth()287 public int getWidth() { 288 return nGetWidth(mNativeCanvasWrapper); 289 } 290 291 /** 292 * Returns the height of the current drawing layer 293 * 294 * @return the height of the current drawing layer 295 */ getHeight()296 public int getHeight() { 297 return nGetHeight(mNativeCanvasWrapper); 298 } 299 300 /** 301 * <p>Returns the target density of the canvas. The default density is 302 * derived from the density of its backing bitmap, or 303 * {@link Bitmap#DENSITY_NONE} if there is not one.</p> 304 * 305 * @return Returns the current target density of the canvas, which is used 306 * to determine the scaling factor when drawing a bitmap into it. 307 * 308 * @see #setDensity(int) 309 * @see Bitmap#getDensity() 310 */ getDensity()311 public int getDensity() { 312 return mDensity; 313 } 314 315 /** 316 * <p>Specifies the density for this Canvas' backing bitmap. This modifies 317 * the target density of the canvas itself, as well as the density of its 318 * backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}. 319 * 320 * @param density The new target density of the canvas, which is used 321 * to determine the scaling factor when drawing a bitmap into it. Use 322 * {@link Bitmap#DENSITY_NONE} to disable bitmap scaling. 323 * 324 * @see #getDensity() 325 * @see Bitmap#setDensity(int) 326 */ setDensity(int density)327 public void setDensity(int density) { 328 if (mBitmap != null) { 329 mBitmap.setDensity(density); 330 } 331 mDensity = density; 332 } 333 334 /** @hide */ 335 @UnsupportedAppUsage setScreenDensity(int density)336 public void setScreenDensity(int density) { 337 mScreenDensity = density; 338 } 339 340 /** 341 * Returns the maximum allowed width for bitmaps drawn with this canvas. 342 * Attempting to draw with a bitmap wider than this value will result 343 * in an error. 344 * 345 * @see #getMaximumBitmapHeight() 346 */ getMaximumBitmapWidth()347 public int getMaximumBitmapWidth() { 348 return MAXMIMUM_BITMAP_SIZE; 349 } 350 351 /** 352 * Returns the maximum allowed height for bitmaps drawn with this canvas. 353 * Attempting to draw with a bitmap taller than this value will result 354 * in an error. 355 * 356 * @see #getMaximumBitmapWidth() 357 */ getMaximumBitmapHeight()358 public int getMaximumBitmapHeight() { 359 return MAXMIMUM_BITMAP_SIZE; 360 } 361 362 // the SAVE_FLAG constants must match their native equivalents 363 364 /** @hide */ 365 @IntDef(flag = true, 366 value = { 367 ALL_SAVE_FLAG 368 }) 369 @Retention(RetentionPolicy.SOURCE) 370 public @interface Saveflags {} 371 372 /** 373 * Restore the current matrix when restore() is called. 374 * @removed 375 * @deprecated Use the flagless version of {@link #save()}, {@link #saveLayer(RectF, Paint)} or 376 * {@link #saveLayerAlpha(RectF, int)}. For saveLayer() calls the matrix 377 * was always restored for {@link #isHardwareAccelerated() Hardware accelerated} 378 * canvases and as of API level {@value Build.VERSION_CODES#O} that is the default 379 * behavior for all canvas types. 380 */ 381 public static final int MATRIX_SAVE_FLAG = 0x01; 382 383 /** 384 * Restore the current clip when restore() is called. 385 * 386 * @removed 387 * @deprecated Use the flagless version of {@link #save()}, {@link #saveLayer(RectF, Paint)} or 388 * {@link #saveLayerAlpha(RectF, int)}. For saveLayer() calls the clip 389 * was always restored for {@link #isHardwareAccelerated() Hardware accelerated} 390 * canvases and as of API level {@value Build.VERSION_CODES#O} that is the default 391 * behavior for all canvas types. 392 */ 393 public static final int CLIP_SAVE_FLAG = 0x02; 394 395 /** 396 * The layer requires a per-pixel alpha channel. 397 * 398 * @removed 399 * @deprecated This flag is ignored. Use the flagless version of {@link #saveLayer(RectF, Paint)} 400 * {@link #saveLayerAlpha(RectF, int)}. 401 */ 402 public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04; 403 404 /** 405 * The layer requires full 8-bit precision for each color channel. 406 * 407 * @removed 408 * @deprecated This flag is ignored. Use the flagless version of {@link #saveLayer(RectF, Paint)} 409 * {@link #saveLayerAlpha(RectF, int)}. 410 */ 411 public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08; 412 413 /** 414 * Clip drawing to the bounds of the offscreen layer, omit at your own peril. 415 * <p class="note"><strong>Note:</strong> it is strongly recommended to not 416 * omit this flag for any call to <code>saveLayer()</code> and 417 * <code>saveLayerAlpha()</code> variants. Not passing this flag generally 418 * triggers extremely poor performance with hardware accelerated rendering. 419 * 420 * @removed 421 * @deprecated This flag results in poor performance and the same effect can be achieved with 422 * a single layer or multiple draw commands with different clips. 423 * 424 */ 425 public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10; 426 427 /** 428 * Restore everything when restore() is called (standard save flags). 429 * <p class="note"><strong>Note:</strong> for performance reasons, it is 430 * strongly recommended to pass this - the complete set of flags - to any 431 * call to <code>saveLayer()</code> and <code>saveLayerAlpha()</code> 432 * variants. 433 * 434 * <p class="note"><strong>Note:</strong> all methods that accept this flag 435 * have flagless versions that are equivalent to passing this flag. 436 */ 437 public static final int ALL_SAVE_FLAG = 0x1F; 438 checkValidSaveFlags(int saveFlags)439 private static void checkValidSaveFlags(int saveFlags) { 440 if (sCompatiblityVersion >= Build.VERSION_CODES.P 441 && saveFlags != ALL_SAVE_FLAG) { 442 throw new IllegalArgumentException( 443 "Invalid Layer Save Flag - only ALL_SAVE_FLAGS is allowed"); 444 } 445 } 446 447 /** 448 * Saves the current matrix and clip onto a private stack. 449 * <p> 450 * Subsequent calls to translate,scale,rotate,skew,concat or clipRect, 451 * clipPath will all operate as usual, but when the balancing call to 452 * restore() is made, those calls will be forgotten, and the settings that 453 * existed before the save() will be reinstated. 454 * 455 * @return The value to pass to restoreToCount() to balance this save() 456 */ save()457 public int save() { 458 return nSave(mNativeCanvasWrapper, MATRIX_SAVE_FLAG | CLIP_SAVE_FLAG); 459 } 460 461 /** 462 * Based on saveFlags, can save the current matrix and clip onto a private 463 * stack. 464 * <p class="note"><strong>Note:</strong> if possible, use the 465 * parameter-less save(). It is simpler and faster than individually 466 * disabling the saving of matrix or clip with this method. 467 * <p> 468 * Subsequent calls to translate,scale,rotate,skew,concat or clipRect, 469 * clipPath will all operate as usual, but when the balancing call to 470 * restore() is made, those calls will be forgotten, and the settings that 471 * existed before the save() will be reinstated. 472 * 473 * @removed 474 * @deprecated Use {@link #save()} instead. 475 * @param saveFlags flag bits that specify which parts of the Canvas state 476 * to save/restore 477 * @return The value to pass to restoreToCount() to balance this save() 478 */ save(@aveflags int saveFlags)479 public int save(@Saveflags int saveFlags) { 480 return nSave(mNativeCanvasWrapper, saveFlags); 481 } 482 483 /** 484 * This behaves the same as save(), but in addition it allocates and 485 * redirects drawing to an offscreen bitmap. 486 * <p class="note"><strong>Note:</strong> this method is very expensive, 487 * incurring more than double rendering cost for contained content. Avoid 488 * using this method, especially if the bounds provided are large. It is 489 * recommended to use a {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View 490 * to apply an xfermode, color filter, or alpha, as it will perform much 491 * better than this method. 492 * <p> 493 * All drawing calls are directed to a newly allocated offscreen bitmap. 494 * Only when the balancing call to restore() is made, is that offscreen 495 * buffer drawn back to the current target of the Canvas (either the 496 * screen, it's target Bitmap, or the previous layer). 497 * <p> 498 * Attributes of the Paint - {@link Paint#getAlpha() alpha}, 499 * {@link Paint#getXfermode() Xfermode}, and 500 * {@link Paint#getColorFilter() ColorFilter} are applied when the 501 * offscreen bitmap is drawn back when restore() is called. 502 * 503 * As of API Level API level {@value Build.VERSION_CODES#P} the only valid 504 * {@code saveFlags} is {@link #ALL_SAVE_FLAG}. All other flags are ignored. 505 * 506 * @deprecated Use {@link #saveLayer(RectF, Paint)} instead. 507 * @param bounds May be null. The maximum size the offscreen bitmap 508 * needs to be (in local coordinates) 509 * @param paint This is copied, and is applied to the offscreen when 510 * restore() is called. 511 * @param saveFlags see _SAVE_FLAG constants, generally {@link #ALL_SAVE_FLAG} is recommended 512 * for performance reasons. 513 * @return value to pass to restoreToCount() to balance this save() 514 */ saveLayer(@ullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags)515 public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags) { 516 if (bounds == null) { 517 bounds = new RectF(getClipBounds()); 518 } 519 checkValidSaveFlags(saveFlags); 520 return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, 521 ALL_SAVE_FLAG); 522 } 523 524 /** 525 * This behaves the same as save(), but in addition it allocates and 526 * redirects drawing to an offscreen rendering target. 527 * <p class="note"><strong>Note:</strong> this method is very expensive, 528 * incurring more than double rendering cost for contained content. Avoid 529 * using this method when possible and instead use a 530 * {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View 531 * to apply an xfermode, color filter, or alpha, as it will perform much 532 * better than this method. 533 * <p> 534 * All drawing calls are directed to a newly allocated offscreen rendering target. 535 * Only when the balancing call to restore() is made, is that offscreen 536 * buffer drawn back to the current target of the Canvas (which can potentially be a previous 537 * layer if these calls are nested). 538 * <p> 539 * Attributes of the Paint - {@link Paint#getAlpha() alpha}, 540 * {@link Paint#getXfermode() Xfermode}, and 541 * {@link Paint#getColorFilter() ColorFilter} are applied when the 542 * offscreen rendering target is drawn back when restore() is called. 543 * 544 * @param bounds May be null. The maximum size the offscreen render target 545 * needs to be (in local coordinates) 546 * @param paint This is copied, and is applied to the offscreen when 547 * restore() is called. 548 * @return value to pass to restoreToCount() to balance this save() 549 */ saveLayer(@ullable RectF bounds, @Nullable Paint paint)550 public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint) { 551 return saveLayer(bounds, paint, ALL_SAVE_FLAG); 552 } 553 554 /** 555 * @hide 556 */ saveUnclippedLayer(int left, int top, int right, int bottom)557 public int saveUnclippedLayer(int left, int top, int right, int bottom) { 558 return nSaveUnclippedLayer(mNativeCanvasWrapper, left, top, right, bottom); 559 } 560 561 /** 562 * @hide 563 * @param saveCount The save level to restore to. 564 * @param paint This is copied and is applied to the area within the unclipped layer's 565 * bounds (i.e. equivalent to a drawPaint()) before restore() is called. 566 */ restoreUnclippedLayer(int saveCount, Paint paint)567 public void restoreUnclippedLayer(int saveCount, Paint paint) { 568 nRestoreUnclippedLayer(mNativeCanvasWrapper, saveCount, paint.getNativeInstance()); 569 } 570 571 /** 572 * Helper version of saveLayer() that takes 4 values rather than a RectF. 573 * 574 * As of API Level API level {@value Build.VERSION_CODES#P} the only valid 575 * {@code saveFlags} is {@link #ALL_SAVE_FLAG}. All other flags are ignored. 576 * 577 * @deprecated Use {@link #saveLayer(float, float, float, float, Paint)} instead. 578 */ saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint, @Saveflags int saveFlags)579 public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint, 580 @Saveflags int saveFlags) { 581 checkValidSaveFlags(saveFlags); 582 return nSaveLayer(mNativeCanvasWrapper, left, top, right, bottom, 583 paint != null ? paint.getNativeInstance() : 0, 584 ALL_SAVE_FLAG); 585 } 586 587 /** 588 * Convenience for {@link #saveLayer(RectF, Paint)} that takes the four float coordinates of the 589 * bounds rectangle. 590 */ saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint)591 public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint) { 592 return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG); 593 } 594 595 /** 596 * This behaves the same as save(), but in addition it allocates and 597 * redirects drawing to an offscreen bitmap. 598 * <p class="note"><strong>Note:</strong> this method is very expensive, 599 * incurring more than double rendering cost for contained content. Avoid 600 * using this method, especially if the bounds provided are large. It is 601 * recommended to use a {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View 602 * to apply an xfermode, color filter, or alpha, as it will perform much 603 * better than this method. 604 * <p> 605 * All drawing calls are directed to a newly allocated offscreen bitmap. 606 * Only when the balancing call to restore() is made, is that offscreen 607 * buffer drawn back to the current target of the Canvas (either the 608 * screen, it's target Bitmap, or the previous layer). 609 * <p> 610 * The {@code alpha} parameter is applied when the offscreen bitmap is 611 * drawn back when restore() is called. 612 * 613 * As of API Level API level {@value Build.VERSION_CODES#P} the only valid 614 * {@code saveFlags} is {@link #ALL_SAVE_FLAG}. All other flags are ignored. 615 * 616 * @deprecated Use {@link #saveLayerAlpha(RectF, int)} instead. 617 * @param bounds The maximum size the offscreen bitmap needs to be 618 * (in local coordinates) 619 * @param alpha The alpha to apply to the offscreen when it is 620 drawn during restore() 621 * @param saveFlags see _SAVE_FLAG constants, generally {@link #ALL_SAVE_FLAG} is recommended 622 * for performance reasons. 623 * @return value to pass to restoreToCount() to balance this call 624 */ saveLayerAlpha(@ullable RectF bounds, int alpha, @Saveflags int saveFlags)625 public int saveLayerAlpha(@Nullable RectF bounds, int alpha, @Saveflags int saveFlags) { 626 if (bounds == null) { 627 bounds = new RectF(getClipBounds()); 628 } 629 checkValidSaveFlags(saveFlags); 630 return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, alpha, 631 ALL_SAVE_FLAG); 632 } 633 634 /** 635 * Convenience for {@link #saveLayer(RectF, Paint)} but instead of taking a entire Paint object 636 * it takes only the {@code alpha} parameter. 637 * 638 * @param bounds The maximum size the offscreen bitmap needs to be 639 * (in local coordinates) 640 * @param alpha The alpha to apply to the offscreen when it is 641 drawn during restore() 642 */ saveLayerAlpha(@ullable RectF bounds, int alpha)643 public int saveLayerAlpha(@Nullable RectF bounds, int alpha) { 644 return saveLayerAlpha(bounds, alpha, ALL_SAVE_FLAG); 645 } 646 647 /** 648 * Helper for saveLayerAlpha() that takes 4 values instead of a RectF. 649 * 650 * As of API Level API level {@value Build.VERSION_CODES#P} the only valid 651 * {@code saveFlags} is {@link #ALL_SAVE_FLAG}. All other flags are ignored. 652 * 653 * @deprecated Use {@link #saveLayerAlpha(float, float, float, float, int)} instead. 654 */ saveLayerAlpha(float left, float top, float right, float bottom, int alpha, @Saveflags int saveFlags)655 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 656 @Saveflags int saveFlags) { 657 checkValidSaveFlags(saveFlags); 658 alpha = Math.min(255, Math.max(0, alpha)); 659 return nSaveLayerAlpha(mNativeCanvasWrapper, left, top, right, bottom, 660 alpha, ALL_SAVE_FLAG); 661 } 662 663 /** 664 * Convenience for {@link #saveLayerAlpha(RectF, int)} that takes the four float coordinates of 665 * the bounds rectangle. 666 */ saveLayerAlpha(float left, float top, float right, float bottom, int alpha)667 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) { 668 return saveLayerAlpha(left, top, right, bottom, alpha, ALL_SAVE_FLAG); 669 } 670 671 /** 672 * This call balances a previous call to save(), and is used to remove all 673 * modifications to the matrix/clip state since the last save call. It is 674 * an error to call restore() more times than save() was called. 675 */ restore()676 public void restore() { 677 if (!nRestore(mNativeCanvasWrapper) 678 && (!sCompatibilityRestore || !isHardwareAccelerated())) { 679 throw new IllegalStateException("Underflow in restore - more restores than saves"); 680 } 681 } 682 683 /** 684 * Returns the number of matrix/clip states on the Canvas' private stack. 685 * This will equal # save() calls - # restore() calls. 686 */ getSaveCount()687 public int getSaveCount() { 688 return nGetSaveCount(mNativeCanvasWrapper); 689 } 690 691 /** 692 * Efficient way to pop any calls to save() that happened after the save 693 * count reached saveCount. It is an error for saveCount to be less than 1. 694 * 695 * Example: 696 * int count = canvas.save(); 697 * ... // more calls potentially to save() 698 * canvas.restoreToCount(count); 699 * // now the canvas is back in the same state it was before the initial 700 * // call to save(). 701 * 702 * @param saveCount The save level to restore to. 703 */ restoreToCount(int saveCount)704 public void restoreToCount(int saveCount) { 705 if (saveCount < 1) { 706 if (!sCompatibilityRestore || !isHardwareAccelerated()) { 707 // do nothing and throw without restoring 708 throw new IllegalArgumentException( 709 "Underflow in restoreToCount - more restores than saves"); 710 } 711 // compat behavior - restore as far as possible 712 saveCount = 1; 713 } 714 nRestoreToCount(mNativeCanvasWrapper, saveCount); 715 } 716 717 /** 718 * Preconcat the current matrix with the specified translation 719 * 720 * @param dx The distance to translate in X 721 * @param dy The distance to translate in Y 722 */ translate(float dx, float dy)723 public void translate(float dx, float dy) { 724 if (dx == 0.0f && dy == 0.0f) return; 725 nTranslate(mNativeCanvasWrapper, dx, dy); 726 } 727 728 /** 729 * Preconcat the current matrix with the specified scale. 730 * 731 * @param sx The amount to scale in X 732 * @param sy The amount to scale in Y 733 */ scale(float sx, float sy)734 public void scale(float sx, float sy) { 735 if (sx == 1.0f && sy == 1.0f) return; 736 nScale(mNativeCanvasWrapper, sx, sy); 737 } 738 739 /** 740 * Preconcat the current matrix with the specified scale. 741 * 742 * @param sx The amount to scale in X 743 * @param sy The amount to scale in Y 744 * @param px The x-coord for the pivot point (unchanged by the scale) 745 * @param py The y-coord for the pivot point (unchanged by the scale) 746 */ scale(float sx, float sy, float px, float py)747 public final void scale(float sx, float sy, float px, float py) { 748 if (sx == 1.0f && sy == 1.0f) return; 749 translate(px, py); 750 scale(sx, sy); 751 translate(-px, -py); 752 } 753 754 /** 755 * Preconcat the current matrix with the specified rotation. 756 * 757 * @param degrees The amount to rotate, in degrees 758 */ rotate(float degrees)759 public void rotate(float degrees) { 760 if (degrees == 0.0f) return; 761 nRotate(mNativeCanvasWrapper, degrees); 762 } 763 764 /** 765 * Preconcat the current matrix with the specified rotation. 766 * 767 * @param degrees The amount to rotate, in degrees 768 * @param px The x-coord for the pivot point (unchanged by the rotation) 769 * @param py The y-coord for the pivot point (unchanged by the rotation) 770 */ rotate(float degrees, float px, float py)771 public final void rotate(float degrees, float px, float py) { 772 if (degrees == 0.0f) return; 773 translate(px, py); 774 rotate(degrees); 775 translate(-px, -py); 776 } 777 778 /** 779 * Preconcat the current matrix with the specified skew. 780 * 781 * @param sx The amount to skew in X 782 * @param sy The amount to skew in Y 783 */ skew(float sx, float sy)784 public void skew(float sx, float sy) { 785 if (sx == 0.0f && sy == 0.0f) return; 786 nSkew(mNativeCanvasWrapper, sx, sy); 787 } 788 789 /** 790 * Preconcat the current matrix with the specified matrix. If the specified 791 * matrix is null, this method does nothing. 792 * 793 * @param matrix The matrix to preconcatenate with the current matrix 794 */ concat(@ullable Matrix matrix)795 public void concat(@Nullable Matrix matrix) { 796 if (matrix != null) nConcat(mNativeCanvasWrapper, matrix.native_instance); 797 } 798 799 /** 800 * Completely replace the current matrix with the specified matrix. If the 801 * matrix parameter is null, then the current matrix is reset to identity. 802 * 803 * <strong>Note:</strong> it is recommended to use {@link #concat(Matrix)}, 804 * {@link #scale(float, float)}, {@link #translate(float, float)} and 805 * {@link #rotate(float)} instead of this method. 806 * 807 * @param matrix The matrix to replace the current matrix with. If it is 808 * null, set the current matrix to identity. 809 * 810 * @see #concat(Matrix) 811 */ setMatrix(@ullable Matrix matrix)812 public void setMatrix(@Nullable Matrix matrix) { 813 nSetMatrix(mNativeCanvasWrapper, 814 matrix == null ? 0 : matrix.native_instance); 815 } 816 817 /** 818 * Return, in ctm, the current transformation matrix. This does not alter 819 * the matrix in the canvas, but just returns a copy of it. 820 * 821 * @deprecated {@link #isHardwareAccelerated() Hardware accelerated} canvases may have any 822 * matrix when passed to a View or Drawable, as it is implementation defined where in the 823 * hierarchy such canvases are created. It is recommended in such cases to either draw contents 824 * irrespective of the current matrix, or to track relevant transform state outside of the 825 * canvas. 826 */ 827 @Deprecated getMatrix(@onNull Matrix ctm)828 public void getMatrix(@NonNull Matrix ctm) { 829 nGetMatrix(mNativeCanvasWrapper, ctm.native_instance); 830 } 831 832 /** 833 * Return a new matrix with a copy of the canvas' current transformation 834 * matrix. 835 * 836 * @deprecated {@link #isHardwareAccelerated() Hardware accelerated} canvases may have any 837 * matrix when passed to a View or Drawable, as it is implementation defined where in the 838 * hierarchy such canvases are created. It is recommended in such cases to either draw contents 839 * irrespective of the current matrix, or to track relevant transform state outside of the 840 * canvas. 841 */ 842 @Deprecated getMatrix()843 public final @NonNull Matrix getMatrix() { 844 Matrix m = new Matrix(); 845 //noinspection deprecation 846 getMatrix(m); 847 return m; 848 } 849 checkValidClipOp(@onNull Region.Op op)850 private static void checkValidClipOp(@NonNull Region.Op op) { 851 if (sCompatiblityVersion >= Build.VERSION_CODES.P 852 && op != Region.Op.INTERSECT && op != Region.Op.DIFFERENCE) { 853 throw new IllegalArgumentException( 854 "Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed"); 855 } 856 } 857 858 /** 859 * Modify the current clip with the specified rectangle. 860 * 861 * @param rect The rect to intersect with the current clip 862 * @param op How the clip is modified 863 * @return true if the resulting clip is non-empty 864 * 865 * @deprecated Region.Op values other than {@link Region.Op#INTERSECT} and 866 * {@link Region.Op#DIFFERENCE} have the ability to expand the clip. The canvas clipping APIs 867 * are intended to only expand the clip as a result of a restore operation. This enables a view 868 * parent to clip a canvas to clearly define the maximal drawing area of its children. The 869 * recommended alternative calls are {@link #clipRect(RectF)} and {@link #clipOutRect(RectF)}; 870 * 871 * As of API Level API level {@value Build.VERSION_CODES#P} only {@link Region.Op#INTERSECT} and 872 * {@link Region.Op#DIFFERENCE} are valid Region.Op parameters. 873 */ 874 @Deprecated clipRect(@onNull RectF rect, @NonNull Region.Op op)875 public boolean clipRect(@NonNull RectF rect, @NonNull Region.Op op) { 876 checkValidClipOp(op); 877 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 878 op.nativeInt); 879 } 880 881 /** 882 * Modify the current clip with the specified rectangle, which is 883 * expressed in local coordinates. 884 * 885 * @param rect The rectangle to intersect with the current clip. 886 * @param op How the clip is modified 887 * @return true if the resulting clip is non-empty 888 * 889 * @deprecated Region.Op values other than {@link Region.Op#INTERSECT} and 890 * {@link Region.Op#DIFFERENCE} have the ability to expand the clip. The canvas clipping APIs 891 * are intended to only expand the clip as a result of a restore operation. This enables a view 892 * parent to clip a canvas to clearly define the maximal drawing area of its children. The 893 * recommended alternative calls are {@link #clipRect(Rect)} and {@link #clipOutRect(Rect)}; 894 * 895 * As of API Level API level {@value Build.VERSION_CODES#P} only {@link Region.Op#INTERSECT} and 896 * {@link Region.Op#DIFFERENCE} are valid Region.Op parameters. 897 */ 898 @Deprecated clipRect(@onNull Rect rect, @NonNull Region.Op op)899 public boolean clipRect(@NonNull Rect rect, @NonNull Region.Op op) { 900 checkValidClipOp(op); 901 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 902 op.nativeInt); 903 } 904 905 /** 906 * DON'T USE THIS METHOD. It exists only to support a particular legacy behavior in 907 * the view system and will be removed as soon as that code is refactored to no longer 908 * depend on this behavior. 909 * @hide 910 */ clipRectUnion(@onNull Rect rect)911 public boolean clipRectUnion(@NonNull Rect rect) { 912 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 913 Region.Op.UNION.nativeInt); 914 } 915 916 /** 917 * Intersect the current clip with the specified rectangle, which is 918 * expressed in local coordinates. 919 * 920 * @param rect The rectangle to intersect with the current clip. 921 * @return true if the resulting clip is non-empty 922 */ clipRect(@onNull RectF rect)923 public boolean clipRect(@NonNull RectF rect) { 924 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 925 Region.Op.INTERSECT.nativeInt); 926 } 927 928 /** 929 * Set the clip to the difference of the current clip and the specified rectangle, which is 930 * expressed in local coordinates. 931 * 932 * @param rect The rectangle to perform a difference op with the current clip. 933 * @return true if the resulting clip is non-empty 934 */ clipOutRect(@onNull RectF rect)935 public boolean clipOutRect(@NonNull RectF rect) { 936 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 937 Region.Op.DIFFERENCE.nativeInt); 938 } 939 940 /** 941 * Intersect the current clip with the specified rectangle, which is 942 * expressed in local coordinates. 943 * 944 * @param rect The rectangle to intersect with the current clip. 945 * @return true if the resulting clip is non-empty 946 */ clipRect(@onNull Rect rect)947 public boolean clipRect(@NonNull Rect rect) { 948 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 949 Region.Op.INTERSECT.nativeInt); 950 } 951 952 /** 953 * Set the clip to the difference of the current clip and the specified rectangle, which is 954 * expressed in local coordinates. 955 * 956 * @param rect The rectangle to perform a difference op with the current clip. 957 * @return true if the resulting clip is non-empty 958 */ clipOutRect(@onNull Rect rect)959 public boolean clipOutRect(@NonNull Rect rect) { 960 return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 961 Region.Op.DIFFERENCE.nativeInt); 962 } 963 964 /** 965 * Modify the current clip with the specified rectangle, which is 966 * expressed in local coordinates. 967 * 968 * @param left The left side of the rectangle to intersect with the 969 * current clip 970 * @param top The top of the rectangle to intersect with the current 971 * clip 972 * @param right The right side of the rectangle to intersect with the 973 * current clip 974 * @param bottom The bottom of the rectangle to intersect with the current 975 * clip 976 * @param op How the clip is modified 977 * @return true if the resulting clip is non-empty 978 * 979 * @deprecated Region.Op values other than {@link Region.Op#INTERSECT} and 980 * {@link Region.Op#DIFFERENCE} have the ability to expand the clip. The canvas clipping APIs 981 * are intended to only expand the clip as a result of a restore operation. This enables a view 982 * parent to clip a canvas to clearly define the maximal drawing area of its children. The 983 * recommended alternative calls are {@link #clipRect(float,float,float,float)} and 984 * {@link #clipOutRect(float,float,float,float)}; 985 * 986 * As of API Level API level {@value Build.VERSION_CODES#P} only {@link Region.Op#INTERSECT} and 987 * {@link Region.Op#DIFFERENCE} are valid Region.Op parameters. 988 */ 989 @Deprecated clipRect(float left, float top, float right, float bottom, @NonNull Region.Op op)990 public boolean clipRect(float left, float top, float right, float bottom, 991 @NonNull Region.Op op) { 992 checkValidClipOp(op); 993 return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, op.nativeInt); 994 } 995 996 /** 997 * Intersect the current clip with the specified rectangle, which is 998 * expressed in local coordinates. 999 * 1000 * @param left The left side of the rectangle to intersect with the 1001 * current clip 1002 * @param top The top of the rectangle to intersect with the current clip 1003 * @param right The right side of the rectangle to intersect with the 1004 * current clip 1005 * @param bottom The bottom of the rectangle to intersect with the current 1006 * clip 1007 * @return true if the resulting clip is non-empty 1008 */ clipRect(float left, float top, float right, float bottom)1009 public boolean clipRect(float left, float top, float right, float bottom) { 1010 return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, 1011 Region.Op.INTERSECT.nativeInt); 1012 } 1013 1014 /** 1015 * Set the clip to the difference of the current clip and the specified rectangle, which is 1016 * expressed in local coordinates. 1017 * 1018 * @param left The left side of the rectangle used in the difference operation 1019 * @param top The top of the rectangle used in the difference operation 1020 * @param right The right side of the rectangle used in the difference operation 1021 * @param bottom The bottom of the rectangle used in the difference operation 1022 * @return true if the resulting clip is non-empty 1023 */ clipOutRect(float left, float top, float right, float bottom)1024 public boolean clipOutRect(float left, float top, float right, float bottom) { 1025 return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, 1026 Region.Op.DIFFERENCE.nativeInt); 1027 } 1028 1029 /** 1030 * Intersect the current clip with the specified rectangle, which is 1031 * expressed in local coordinates. 1032 * 1033 * @param left The left side of the rectangle to intersect with the 1034 * current clip 1035 * @param top The top of the rectangle to intersect with the current clip 1036 * @param right The right side of the rectangle to intersect with the 1037 * current clip 1038 * @param bottom The bottom of the rectangle to intersect with the current 1039 * clip 1040 * @return true if the resulting clip is non-empty 1041 */ clipRect(int left, int top, int right, int bottom)1042 public boolean clipRect(int left, int top, int right, int bottom) { 1043 return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, 1044 Region.Op.INTERSECT.nativeInt); 1045 } 1046 1047 /** 1048 * Set the clip to the difference of the current clip and the specified rectangle, which is 1049 * expressed in local coordinates. 1050 * 1051 * @param left The left side of the rectangle used in the difference operation 1052 * @param top The top of the rectangle used in the difference operation 1053 * @param right The right side of the rectangle used in the difference operation 1054 * @param bottom The bottom of the rectangle used in the difference operation 1055 * @return true if the resulting clip is non-empty 1056 */ clipOutRect(int left, int top, int right, int bottom)1057 public boolean clipOutRect(int left, int top, int right, int bottom) { 1058 return nClipRect(mNativeCanvasWrapper, left, top, right, bottom, 1059 Region.Op.DIFFERENCE.nativeInt); 1060 } 1061 1062 /** 1063 * Modify the current clip with the specified path. 1064 * 1065 * @param path The path to operate on the current clip 1066 * @param op How the clip is modified 1067 * @return true if the resulting is non-empty 1068 * 1069 * @deprecated Region.Op values other than {@link Region.Op#INTERSECT} and 1070 * {@link Region.Op#DIFFERENCE} have the ability to expand the clip. The canvas clipping APIs 1071 * are intended to only expand the clip as a result of a restore operation. This enables a view 1072 * parent to clip a canvas to clearly define the maximal drawing area of its children. The 1073 * recommended alternative calls are {@link #clipPath(Path)} and 1074 * {@link #clipOutPath(Path)}; 1075 * 1076 * As of API Level API level {@value Build.VERSION_CODES#P} only {@link Region.Op#INTERSECT} and 1077 * {@link Region.Op#DIFFERENCE} are valid Region.Op parameters. 1078 */ 1079 @Deprecated clipPath(@onNull Path path, @NonNull Region.Op op)1080 public boolean clipPath(@NonNull Path path, @NonNull Region.Op op) { 1081 checkValidClipOp(op); 1082 return nClipPath(mNativeCanvasWrapper, path.readOnlyNI(), op.nativeInt); 1083 } 1084 1085 /** 1086 * Intersect the current clip with the specified path. 1087 * 1088 * @param path The path to intersect with the current clip 1089 * @return true if the resulting clip is non-empty 1090 */ clipPath(@onNull Path path)1091 public boolean clipPath(@NonNull Path path) { 1092 return clipPath(path, Region.Op.INTERSECT); 1093 } 1094 1095 /** 1096 * Set the clip to the difference of the current clip and the specified path. 1097 * 1098 * @param path The path used in the difference operation 1099 * @return true if the resulting clip is non-empty 1100 */ clipOutPath(@onNull Path path)1101 public boolean clipOutPath(@NonNull Path path) { 1102 return clipPath(path, Region.Op.DIFFERENCE); 1103 } 1104 1105 /** 1106 * Modify the current clip with the specified region. Note that unlike 1107 * clipRect() and clipPath() which transform their arguments by the 1108 * current matrix, clipRegion() assumes its argument is already in the 1109 * coordinate system of the current layer's bitmap, and so not 1110 * transformation is performed. 1111 * 1112 * @param region The region to operate on the current clip, based on op 1113 * @param op How the clip is modified 1114 * @return true if the resulting is non-empty 1115 * 1116 * @removed 1117 * @deprecated Unlike all other clip calls this API does not respect the 1118 * current matrix. Use {@link #clipRect(Rect)} as an alternative. 1119 */ 1120 @Deprecated clipRegion(@onNull Region region, @NonNull Region.Op op)1121 public boolean clipRegion(@NonNull Region region, @NonNull Region.Op op) { 1122 return false; 1123 } 1124 1125 /** 1126 * Intersect the current clip with the specified region. Note that unlike 1127 * clipRect() and clipPath() which transform their arguments by the 1128 * current matrix, clipRegion() assumes its argument is already in the 1129 * coordinate system of the current layer's bitmap, and so not 1130 * transformation is performed. 1131 * 1132 * @param region The region to operate on the current clip, based on op 1133 * @return true if the resulting is non-empty 1134 * 1135 * @removed 1136 * @deprecated Unlike all other clip calls this API does not respect the 1137 * current matrix. Use {@link #clipRect(Rect)} as an alternative. 1138 */ 1139 @Deprecated clipRegion(@onNull Region region)1140 public boolean clipRegion(@NonNull Region region) { 1141 return false; 1142 } 1143 getDrawFilter()1144 public @Nullable DrawFilter getDrawFilter() { 1145 return mDrawFilter; 1146 } 1147 setDrawFilter(@ullable DrawFilter filter)1148 public void setDrawFilter(@Nullable DrawFilter filter) { 1149 long nativeFilter = 0; 1150 if (filter != null) { 1151 nativeFilter = filter.mNativeInt; 1152 } 1153 mDrawFilter = filter; 1154 nSetDrawFilter(mNativeCanvasWrapper, nativeFilter); 1155 } 1156 1157 /** 1158 * Constant values used as parameters to {@code quickReject()} calls. These values 1159 * specify how much space around the shape should be accounted for, depending on whether 1160 * the shaped area is antialiased or not. 1161 * 1162 * @see #quickReject(float, float, float, float, EdgeType) 1163 * @see #quickReject(Path, EdgeType) 1164 * @see #quickReject(RectF, EdgeType) 1165 * @deprecated quickReject no longer uses this. 1166 */ 1167 public enum EdgeType { 1168 1169 /** 1170 * Black-and-White: Treat edges by just rounding to nearest pixel boundary 1171 */ 1172 BW(0), //!< treat edges by just rounding to nearest pixel boundary 1173 1174 /** 1175 * Antialiased: Treat edges by rounding-out, since they may be antialiased 1176 */ 1177 AA(1); 1178 EdgeType(int nativeInt)1179 EdgeType(int nativeInt) { 1180 this.nativeInt = nativeInt; 1181 } 1182 1183 /** 1184 * @hide 1185 */ 1186 public final int nativeInt; 1187 } 1188 1189 /** 1190 * Return true if the specified rectangle, after being transformed by the 1191 * current matrix, would lie completely outside of the current clip. Call 1192 * this to check if an area you intend to draw into is clipped out (and 1193 * therefore you can skip making the draw calls). 1194 * 1195 * @param rect the rect to compare with the current clip 1196 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1197 * since that means it may affect a larger area (more pixels) than 1198 * non-antialiased ({@link Canvas.EdgeType#BW}). 1199 * @return true if the rect (transformed by the canvas' matrix) 1200 * does not intersect with the canvas' clip 1201 * @deprecated The EdgeType is ignored. Use {@link #quickReject(RectF)} instead. 1202 */ 1203 @Deprecated quickReject(@onNull RectF rect, @NonNull EdgeType type)1204 public boolean quickReject(@NonNull RectF rect, @NonNull EdgeType type) { 1205 return nQuickReject(mNativeCanvasWrapper, 1206 rect.left, rect.top, rect.right, rect.bottom); 1207 } 1208 1209 /** 1210 * Return true if the specified rectangle, after being transformed by the 1211 * current matrix, would lie completely outside of the current clip. Call 1212 * this to check if an area you intend to draw into is clipped out (and 1213 * therefore you can skip making the draw calls). 1214 * 1215 * @param rect the rect to compare with the current clip 1216 * @return true if the rect (transformed by the canvas' matrix) 1217 * does not intersect with the canvas' clip 1218 */ quickReject(@onNull RectF rect)1219 public boolean quickReject(@NonNull RectF rect) { 1220 return nQuickReject(mNativeCanvasWrapper, 1221 rect.left, rect.top, rect.right, rect.bottom); 1222 } 1223 1224 /** 1225 * Return true if the specified path, after being transformed by the 1226 * current matrix, would lie completely outside of the current clip. Call 1227 * this to check if an area you intend to draw into is clipped out (and 1228 * therefore you can skip making the draw calls). Note: for speed it may 1229 * return false even if the path itself might not intersect the clip 1230 * (i.e. the bounds of the path intersects, but the path does not). 1231 * 1232 * @param path The path to compare with the current clip 1233 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1234 * since that means it may affect a larger area (more pixels) than 1235 * non-antialiased ({@link Canvas.EdgeType#BW}). 1236 * @return true if the path (transformed by the canvas' matrix) 1237 * does not intersect with the canvas' clip 1238 * @deprecated The EdgeType is ignored. Use {@link #quickReject(Path)} instead. 1239 */ 1240 @Deprecated quickReject(@onNull Path path, @NonNull EdgeType type)1241 public boolean quickReject(@NonNull Path path, @NonNull EdgeType type) { 1242 return nQuickReject(mNativeCanvasWrapper, path.readOnlyNI()); 1243 } 1244 1245 /** 1246 * Return true if the specified path, after being transformed by the 1247 * current matrix, would lie completely outside of the current clip. Call 1248 * this to check if an area you intend to draw into is clipped out (and 1249 * therefore you can skip making the draw calls). Note: for speed it may 1250 * return false even if the path itself might not intersect the clip 1251 * (i.e. the bounds of the path intersects, but the path does not). 1252 * 1253 * @param path The path to compare with the current clip 1254 * @return true if the path (transformed by the canvas' matrix) 1255 * does not intersect with the canvas' clip 1256 */ quickReject(@onNull Path path)1257 public boolean quickReject(@NonNull Path path) { 1258 return nQuickReject(mNativeCanvasWrapper, path.readOnlyNI()); 1259 } 1260 1261 /** 1262 * Return true if the specified rectangle, after being transformed by the 1263 * current matrix, would lie completely outside of the current clip. Call 1264 * this to check if an area you intend to draw into is clipped out (and 1265 * therefore you can skip making the draw calls). 1266 * 1267 * @param left The left side of the rectangle to compare with the 1268 * current clip 1269 * @param top The top of the rectangle to compare with the current 1270 * clip 1271 * @param right The right side of the rectangle to compare with the 1272 * current clip 1273 * @param bottom The bottom of the rectangle to compare with the 1274 * current clip 1275 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1276 * since that means it may affect a larger area (more pixels) than 1277 * non-antialiased ({@link Canvas.EdgeType#BW}). 1278 * @return true if the rect (transformed by the canvas' matrix) 1279 * does not intersect with the canvas' clip 1280 * @deprecated The EdgeType is ignored. Use {@link #quickReject(float, float, float, float)} 1281 * instead. 1282 */ 1283 @Deprecated quickReject(float left, float top, float right, float bottom, @NonNull EdgeType type)1284 public boolean quickReject(float left, float top, float right, float bottom, 1285 @NonNull EdgeType type) { 1286 return nQuickReject(mNativeCanvasWrapper, left, top, right, bottom); 1287 } 1288 1289 /** 1290 * Return true if the specified rectangle, after being transformed by the 1291 * current matrix, would lie completely outside of the current clip. Call 1292 * this to check if an area you intend to draw into is clipped out (and 1293 * therefore you can skip making the draw calls). 1294 * 1295 * @param left The left side of the rectangle to compare with the 1296 * current clip 1297 * @param top The top of the rectangle to compare with the current 1298 * clip 1299 * @param right The right side of the rectangle to compare with the 1300 * current clip 1301 * @param bottom The bottom of the rectangle to compare with the 1302 * current clip 1303 * @return true if the rect (transformed by the canvas' matrix) 1304 * does not intersect with the canvas' clip 1305 */ quickReject(float left, float top, float right, float bottom)1306 public boolean quickReject(float left, float top, float right, float bottom) { 1307 return nQuickReject(mNativeCanvasWrapper, left, top, right, bottom); 1308 } 1309 1310 /** 1311 * Return the bounds of the current clip (in local coordinates) in the 1312 * bounds parameter, and return true if it is non-empty. This can be useful 1313 * in a way similar to quickReject, in that it tells you that drawing 1314 * outside of these bounds will be clipped out. 1315 * 1316 * @param bounds Return the clip bounds here. If it is null, ignore it but 1317 * still return true if the current clip is non-empty. 1318 * @return true if the current clip is non-empty. 1319 */ getClipBounds(@ullable Rect bounds)1320 public boolean getClipBounds(@Nullable Rect bounds) { 1321 return nGetClipBounds(mNativeCanvasWrapper, bounds); 1322 } 1323 1324 /** 1325 * Retrieve the bounds of the current clip (in local coordinates). 1326 * 1327 * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty. 1328 */ getClipBounds()1329 public final @NonNull Rect getClipBounds() { 1330 Rect r = new Rect(); 1331 getClipBounds(r); 1332 return r; 1333 } 1334 1335 /** 1336 * Save the canvas state, draw the picture, and restore the canvas state. 1337 * This differs from picture.draw(canvas), which does not perform any 1338 * save/restore. 1339 * 1340 * <p> 1341 * <strong>Note:</strong> This forces the picture to internally call 1342 * {@link Picture#endRecording} in order to prepare for playback. 1343 * 1344 * @param picture The picture to be drawn 1345 */ drawPicture(@onNull Picture picture)1346 public void drawPicture(@NonNull Picture picture) { 1347 picture.endRecording(); 1348 int restoreCount = save(); 1349 picture.draw(this); 1350 restoreToCount(restoreCount); 1351 } 1352 1353 /** 1354 * Draw the picture, stretched to fit into the dst rectangle. 1355 */ drawPicture(@onNull Picture picture, @NonNull RectF dst)1356 public void drawPicture(@NonNull Picture picture, @NonNull RectF dst) { 1357 save(); 1358 translate(dst.left, dst.top); 1359 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1360 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1361 } 1362 drawPicture(picture); 1363 restore(); 1364 } 1365 1366 /** 1367 * Draw the picture, stretched to fit into the dst rectangle. 1368 */ drawPicture(@onNull Picture picture, @NonNull Rect dst)1369 public void drawPicture(@NonNull Picture picture, @NonNull Rect dst) { 1370 save(); 1371 translate(dst.left, dst.top); 1372 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1373 scale((float) dst.width() / picture.getWidth(), 1374 (float) dst.height() / picture.getHeight()); 1375 } 1376 drawPicture(picture); 1377 restore(); 1378 } 1379 1380 public enum VertexMode { 1381 TRIANGLES(0), 1382 TRIANGLE_STRIP(1), 1383 TRIANGLE_FAN(2); 1384 VertexMode(int nativeInt)1385 VertexMode(int nativeInt) { 1386 this.nativeInt = nativeInt; 1387 } 1388 1389 /** 1390 * @hide 1391 */ 1392 public final int nativeInt; 1393 } 1394 1395 /** 1396 * Releases the resources associated with this canvas. 1397 * 1398 * @hide 1399 */ 1400 @UnsupportedAppUsage release()1401 public void release() { 1402 mNativeCanvasWrapper = 0; 1403 if (mFinalizer != null) { 1404 mFinalizer.run(); 1405 mFinalizer = null; 1406 } 1407 } 1408 1409 /** 1410 * Free up as much memory as possible from private caches (e.g. fonts, images) 1411 * 1412 * @hide 1413 */ 1414 @UnsupportedAppUsage freeCaches()1415 public static void freeCaches() { 1416 nFreeCaches(); 1417 } 1418 1419 /** 1420 * Free up text layout caches 1421 * 1422 * @hide 1423 */ 1424 @UnsupportedAppUsage freeTextLayoutCaches()1425 public static void freeTextLayoutCaches() { 1426 nFreeTextLayoutCaches(); 1427 } 1428 1429 /** @hide */ setCompatibilityVersion(int apiLevel)1430 public static void setCompatibilityVersion(int apiLevel) { 1431 sCompatiblityVersion = apiLevel; 1432 nSetCompatibilityVersion(apiLevel); 1433 } 1434 nFreeCaches()1435 private static native void nFreeCaches(); nFreeTextLayoutCaches()1436 private static native void nFreeTextLayoutCaches(); nGetNativeFinalizer()1437 private static native long nGetNativeFinalizer(); nSetCompatibilityVersion(int apiLevel)1438 private static native void nSetCompatibilityVersion(int apiLevel); 1439 1440 // ---------------- @FastNative ------------------- 1441 1442 @FastNative nInitRaster(long bitmapHandle)1443 private static native long nInitRaster(long bitmapHandle); 1444 1445 @FastNative nSetBitmap(long canvasHandle, long bitmapHandle)1446 private static native void nSetBitmap(long canvasHandle, long bitmapHandle); 1447 1448 @FastNative nGetClipBounds(long nativeCanvas, Rect bounds)1449 private static native boolean nGetClipBounds(long nativeCanvas, Rect bounds); 1450 1451 // ---------------- @CriticalNative ------------------- 1452 1453 @CriticalNative nIsOpaque(long canvasHandle)1454 private static native boolean nIsOpaque(long canvasHandle); 1455 @CriticalNative nGetWidth(long canvasHandle)1456 private static native int nGetWidth(long canvasHandle); 1457 @CriticalNative nGetHeight(long canvasHandle)1458 private static native int nGetHeight(long canvasHandle); 1459 1460 @CriticalNative nSave(long canvasHandle, int saveFlags)1461 private static native int nSave(long canvasHandle, int saveFlags); 1462 @CriticalNative nSaveLayer(long nativeCanvas, float l, float t, float r, float b, long nativePaint, int layerFlags)1463 private static native int nSaveLayer(long nativeCanvas, float l, float t, float r, float b, 1464 long nativePaint, int layerFlags); 1465 @CriticalNative nSaveLayerAlpha(long nativeCanvas, float l, float t, float r, float b, int alpha, int layerFlags)1466 private static native int nSaveLayerAlpha(long nativeCanvas, float l, float t, float r, float b, 1467 int alpha, int layerFlags); 1468 @CriticalNative nSaveUnclippedLayer(long nativeCanvas, int l, int t, int r, int b)1469 private static native int nSaveUnclippedLayer(long nativeCanvas, int l, int t, int r, int b); 1470 @CriticalNative nRestoreUnclippedLayer(long nativeCanvas, int saveCount, long nativePaint)1471 private static native void nRestoreUnclippedLayer(long nativeCanvas, int saveCount, 1472 long nativePaint); 1473 @CriticalNative nRestore(long canvasHandle)1474 private static native boolean nRestore(long canvasHandle); 1475 @CriticalNative nRestoreToCount(long canvasHandle, int saveCount)1476 private static native void nRestoreToCount(long canvasHandle, int saveCount); 1477 @CriticalNative nGetSaveCount(long canvasHandle)1478 private static native int nGetSaveCount(long canvasHandle); 1479 1480 @CriticalNative nTranslate(long canvasHandle, float dx, float dy)1481 private static native void nTranslate(long canvasHandle, float dx, float dy); 1482 @CriticalNative nScale(long canvasHandle, float sx, float sy)1483 private static native void nScale(long canvasHandle, float sx, float sy); 1484 @CriticalNative nRotate(long canvasHandle, float degrees)1485 private static native void nRotate(long canvasHandle, float degrees); 1486 @CriticalNative nSkew(long canvasHandle, float sx, float sy)1487 private static native void nSkew(long canvasHandle, float sx, float sy); 1488 @CriticalNative nConcat(long nativeCanvas, long nativeMatrix)1489 private static native void nConcat(long nativeCanvas, long nativeMatrix); 1490 @CriticalNative nSetMatrix(long nativeCanvas, long nativeMatrix)1491 private static native void nSetMatrix(long nativeCanvas, long nativeMatrix); 1492 @CriticalNative nClipRect(long nativeCanvas, float left, float top, float right, float bottom, int regionOp)1493 private static native boolean nClipRect(long nativeCanvas, 1494 float left, float top, float right, float bottom, int regionOp); 1495 @CriticalNative nClipPath(long nativeCanvas, long nativePath, int regionOp)1496 private static native boolean nClipPath(long nativeCanvas, long nativePath, int regionOp); 1497 @CriticalNative nSetDrawFilter(long nativeCanvas, long nativeFilter)1498 private static native void nSetDrawFilter(long nativeCanvas, long nativeFilter); 1499 @CriticalNative nGetMatrix(long nativeCanvas, long nativeMatrix)1500 private static native void nGetMatrix(long nativeCanvas, long nativeMatrix); 1501 @CriticalNative nQuickReject(long nativeCanvas, long nativePath)1502 private static native boolean nQuickReject(long nativeCanvas, long nativePath); 1503 @CriticalNative nQuickReject(long nativeCanvas, float left, float top, float right, float bottom)1504 private static native boolean nQuickReject(long nativeCanvas, float left, float top, 1505 float right, float bottom); 1506 1507 1508 // ---------------- Draw Methods ------------------- 1509 1510 /** 1511 * <p> 1512 * Draw the specified arc, which will be scaled to fit inside the specified oval. 1513 * </p> 1514 * <p> 1515 * If the start angle is negative or >= 360, the start angle is treated as start angle modulo 1516 * 360. 1517 * </p> 1518 * <p> 1519 * If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs 1520 * slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is 1521 * negative, the sweep angle is treated as sweep angle modulo 360 1522 * </p> 1523 * <p> 1524 * The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 1525 * degrees (3 o'clock on a watch.) 1526 * </p> 1527 * 1528 * @param oval The bounds of oval used to define the shape and size of the arc 1529 * @param startAngle Starting angle (in degrees) where the arc begins 1530 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1531 * @param useCenter If true, include the center of the oval in the arc, and close it if it is 1532 * being stroked. This will draw a wedge 1533 * @param paint The paint used to draw the arc 1534 */ drawArc(@onNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)1535 public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, 1536 @NonNull Paint paint) { 1537 super.drawArc(oval, startAngle, sweepAngle, useCenter, paint); 1538 } 1539 1540 /** 1541 * <p> 1542 * Draw the specified arc, which will be scaled to fit inside the specified oval. 1543 * </p> 1544 * <p> 1545 * If the start angle is negative or >= 360, the start angle is treated as start angle modulo 1546 * 360. 1547 * </p> 1548 * <p> 1549 * If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs 1550 * slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is 1551 * negative, the sweep angle is treated as sweep angle modulo 360 1552 * </p> 1553 * <p> 1554 * The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 1555 * degrees (3 o'clock on a watch.) 1556 * </p> 1557 * 1558 * @param startAngle Starting angle (in degrees) where the arc begins 1559 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1560 * @param useCenter If true, include the center of the oval in the arc, and close it if it is 1561 * being stroked. This will draw a wedge 1562 * @param paint The paint used to draw the arc 1563 */ drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)1564 public void drawArc(float left, float top, float right, float bottom, float startAngle, 1565 float sweepAngle, boolean useCenter, @NonNull Paint paint) { 1566 super.drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint); 1567 } 1568 1569 /** 1570 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified ARGB 1571 * color, using srcover porterduff mode. 1572 * 1573 * @param a alpha component (0..255) of the color to draw onto the canvas 1574 * @param r red component (0..255) of the color to draw onto the canvas 1575 * @param g green component (0..255) of the color to draw onto the canvas 1576 * @param b blue component (0..255) of the color to draw onto the canvas 1577 */ drawARGB(int a, int r, int g, int b)1578 public void drawARGB(int a, int r, int g, int b) { 1579 super.drawARGB(a, r, g, b); 1580 } 1581 1582 /** 1583 * Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, 1584 * transformed by the current matrix. 1585 * <p> 1586 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1587 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1588 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1589 * the edge color replicated. 1590 * <p> 1591 * If the bitmap and canvas have different densities, this function will take care of 1592 * automatically scaling the bitmap to draw at the same density as the canvas. 1593 * 1594 * @param bitmap The bitmap to be drawn 1595 * @param left The position of the left side of the bitmap being drawn 1596 * @param top The position of the top side of the bitmap being drawn 1597 * @param paint The paint used to draw the bitmap (may be null) 1598 */ drawBitmap(@onNull Bitmap bitmap, float left, float top, @Nullable Paint paint)1599 public void drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint) { 1600 super.drawBitmap(bitmap, left, top, paint); 1601 } 1602 1603 /** 1604 * Draw the specified bitmap, scaling/translating automatically to fill the destination 1605 * rectangle. If the source rectangle is not null, it specifies the subset of the bitmap to 1606 * draw. 1607 * <p> 1608 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1609 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1610 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1611 * the edge color replicated. 1612 * <p> 1613 * This function <em>ignores the density associated with the bitmap</em>. This is because the 1614 * source and destination rectangle coordinate spaces are in their respective densities, so must 1615 * already have the appropriate scaling factor applied. 1616 * 1617 * @param bitmap The bitmap to be drawn 1618 * @param src May be null. The subset of the bitmap to be drawn 1619 * @param dst The rectangle that the bitmap will be scaled/translated to fit into 1620 * @param paint May be null. The paint used to draw the bitmap 1621 */ drawBitmap(@onNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)1622 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, 1623 @Nullable Paint paint) { 1624 super.drawBitmap(bitmap, src, dst, paint); 1625 } 1626 1627 /** 1628 * Draw the specified bitmap, scaling/translating automatically to fill the destination 1629 * rectangle. If the source rectangle is not null, it specifies the subset of the bitmap to 1630 * draw. 1631 * <p> 1632 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1633 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1634 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1635 * the edge color replicated. 1636 * <p> 1637 * This function <em>ignores the density associated with the bitmap</em>. This is because the 1638 * source and destination rectangle coordinate spaces are in their respective densities, so must 1639 * already have the appropriate scaling factor applied. 1640 * 1641 * @param bitmap The bitmap to be drawn 1642 * @param src May be null. The subset of the bitmap to be drawn 1643 * @param dst The rectangle that the bitmap will be scaled/translated to fit into 1644 * @param paint May be null. The paint used to draw the bitmap 1645 */ drawBitmap(@onNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)1646 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, 1647 @Nullable Paint paint) { 1648 super.drawBitmap(bitmap, src, dst, paint); 1649 } 1650 1651 /** 1652 * Treat the specified array of colors as a bitmap, and draw it. This gives the same result as 1653 * first creating a bitmap from the array, and then drawing it, but this method avoids 1654 * explicitly creating a bitmap object which can be more efficient if the colors are changing 1655 * often. 1656 * 1657 * @param colors Array of colors representing the pixels of the bitmap 1658 * @param offset Offset into the array of colors for the first pixel 1659 * @param stride The number of colors in the array between rows (must be >= width or <= -width). 1660 * @param x The X coordinate for where to draw the bitmap 1661 * @param y The Y coordinate for where to draw the bitmap 1662 * @param width The width of the bitmap 1663 * @param height The height of the bitmap 1664 * @param hasAlpha True if the alpha channel of the colors contains valid values. If false, the 1665 * alpha byte is ignored (assumed to be 0xFF for every pixel). 1666 * @param paint May be null. The paint used to draw the bitmap 1667 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1668 * requires an internal copy of color buffer contents every time this method is 1669 * called. Using a Bitmap avoids this copy, and allows the application to more 1670 * explicitly control the lifetime and copies of pixel data. 1671 */ 1672 @Deprecated drawBitmap(@onNull int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, @Nullable Paint paint)1673 public void drawBitmap(@NonNull int[] colors, int offset, int stride, float x, float y, 1674 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1675 super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint); 1676 } 1677 1678 /** 1679 * Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1680 * 1681 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1682 * requires an internal copy of color buffer contents every time this method is 1683 * called. Using a Bitmap avoids this copy, and allows the application to more 1684 * explicitly control the lifetime and copies of pixel data. 1685 */ 1686 @Deprecated drawBitmap(@onNull int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, @Nullable Paint paint)1687 public void drawBitmap(@NonNull int[] colors, int offset, int stride, int x, int y, 1688 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1689 super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint); 1690 } 1691 1692 /** 1693 * Draw the bitmap using the specified matrix. 1694 * 1695 * @param bitmap The bitmap to draw 1696 * @param matrix The matrix used to transform the bitmap when it is drawn 1697 * @param paint May be null. The paint used to draw the bitmap 1698 */ drawBitmap(@onNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)1699 public void drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint) { 1700 super.drawBitmap(bitmap, matrix, paint); 1701 } 1702 1703 /** 1704 * Draw the bitmap through the mesh, where mesh vertices are evenly distributed across the 1705 * bitmap. There are meshWidth+1 vertices across, and meshHeight+1 vertices down. The verts 1706 * array is accessed in row-major order, so that the first meshWidth+1 vertices are distributed 1707 * across the top of the bitmap from left to right. A more general version of this method is 1708 * drawVertices(). 1709 * 1710 * Prior to API level {@value Build.VERSION_CODES#P} vertOffset and colorOffset were ignored, 1711 * effectively treating them as zeros. In API level {@value Build.VERSION_CODES#P} and above 1712 * these parameters will be respected. 1713 * 1714 * @param bitmap The bitmap to draw using the mesh 1715 * @param meshWidth The number of columns in the mesh. Nothing is drawn if this is 0 1716 * @param meshHeight The number of rows in the mesh. Nothing is drawn if this is 0 1717 * @param verts Array of x,y pairs, specifying where the mesh should be drawn. There must be at 1718 * least (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values in the array 1719 * @param vertOffset Number of verts elements to skip before drawing 1720 * @param colors May be null. Specifies a color at each vertex, which is interpolated across the 1721 * cell, and whose values are multiplied by the corresponding bitmap colors. If not 1722 * null, there must be at least (meshWidth+1) * (meshHeight+1) + colorOffset values 1723 * in the array. 1724 * @param colorOffset Number of color elements to skip before drawing 1725 * @param paint May be null. The paint used to draw the bitmap 1726 */ drawBitmapMesh(@onNull Bitmap bitmap, int meshWidth, int meshHeight, @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, @Nullable Paint paint)1727 public void drawBitmapMesh(@NonNull Bitmap bitmap, int meshWidth, int meshHeight, 1728 @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, 1729 @Nullable Paint paint) { 1730 super.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, 1731 paint); 1732 } 1733 1734 /** 1735 * Draw the specified circle using the specified paint. If radius is <= 0, then nothing will be 1736 * drawn. The circle will be filled or framed based on the Style in the paint. 1737 * 1738 * @param cx The x-coordinate of the center of the circle to be drawn 1739 * @param cy The y-coordinate of the center of the circle to be drawn 1740 * @param radius The radius of the circle to be drawn 1741 * @param paint The paint used to draw the circle 1742 */ drawCircle(float cx, float cy, float radius, @NonNull Paint paint)1743 public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) { 1744 super.drawCircle(cx, cy, radius, paint); 1745 } 1746 1747 /** 1748 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, 1749 * using srcover porterduff mode. 1750 * 1751 * @param color the color to draw onto the canvas 1752 */ drawColor(@olorInt int color)1753 public void drawColor(@ColorInt int color) { 1754 super.drawColor(color); 1755 } 1756 1757 /** 1758 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, 1759 * using srcover porterduff mode. 1760 * 1761 * @param color the {@code ColorLong} to draw onto the canvas. See the {@link Color} 1762 * class for details about {@code ColorLong}s. 1763 * @throws IllegalArgumentException if the color space encoded in the {@code ColorLong} 1764 * is invalid or unknown. 1765 */ drawColor(@olorLong long color)1766 public void drawColor(@ColorLong long color) { 1767 super.drawColor(color, BlendMode.SRC_OVER); 1768 } 1769 1770 /** 1771 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1772 * porter-duff xfermode. 1773 * 1774 * @param color the color to draw onto the canvas 1775 * @param mode the porter-duff mode to apply to the color 1776 */ drawColor(@olorInt int color, @NonNull PorterDuff.Mode mode)1777 public void drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode) { 1778 super.drawColor(color, mode); 1779 } 1780 1781 /** 1782 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1783 * blendmode. 1784 * 1785 * @param color the color to draw onto the canvas 1786 * @param mode the blendmode to apply to the color 1787 */ drawColor(@olorInt int color, @NonNull BlendMode mode)1788 public void drawColor(@ColorInt int color, @NonNull BlendMode mode) { 1789 super.drawColor(color, mode); 1790 } 1791 1792 /** 1793 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1794 * blendmode. 1795 * 1796 * @param color the {@code ColorLong} to draw onto the canvas. See the {@link Color} 1797 * class for details about {@code ColorLong}s. 1798 * @param mode the blendmode to apply to the color 1799 * @throws IllegalArgumentException if the color space encoded in the {@code ColorLong} 1800 * is invalid or unknown. 1801 */ drawColor(@olorLong long color, @NonNull BlendMode mode)1802 public void drawColor(@ColorLong long color, @NonNull BlendMode mode) { 1803 super.drawColor(color, mode); 1804 } 1805 1806 /** 1807 * Draw a line segment with the specified start and stop x,y coordinates, using the specified 1808 * paint. 1809 * <p> 1810 * Note that since a line is always "framed", the Style is ignored in the paint. 1811 * </p> 1812 * <p> 1813 * Degenerate lines (length is 0) will not be drawn. 1814 * </p> 1815 * 1816 * @param startX The x-coordinate of the start point of the line 1817 * @param startY The y-coordinate of the start point of the line 1818 * @param paint The paint used to draw the line 1819 */ drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint)1820 public void drawLine(float startX, float startY, float stopX, float stopY, 1821 @NonNull Paint paint) { 1822 super.drawLine(startX, startY, stopX, stopY, paint); 1823 } 1824 1825 /** 1826 * Draw a series of lines. Each line is taken from 4 consecutive values in the pts array. Thus 1827 * to draw 1 line, the array must contain at least 4 values. This is logically the same as 1828 * drawing the array as follows: drawLine(pts[0], pts[1], pts[2], pts[3]) followed by 1829 * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on. 1830 * 1831 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 1832 * @param offset Number of values in the array to skip before drawing. 1833 * @param count The number of values in the array to process, after skipping "offset" of them. 1834 * Since each line uses 4 values, the number of "lines" that are drawn is really 1835 * (count >> 2). 1836 * @param paint The paint used to draw the points 1837 */ drawLines(@izemultiple = 4) @onNull float[] pts, int offset, int count, @NonNull Paint paint)1838 public void drawLines(@Size(multiple = 4) @NonNull float[] pts, int offset, int count, 1839 @NonNull Paint paint) { 1840 super.drawLines(pts, offset, count, paint); 1841 } 1842 drawLines(@izemultiple = 4) @onNull float[] pts, @NonNull Paint paint)1843 public void drawLines(@Size(multiple = 4) @NonNull float[] pts, @NonNull Paint paint) { 1844 super.drawLines(pts, paint); 1845 } 1846 1847 /** 1848 * Draw the specified oval using the specified paint. The oval will be filled or framed based on 1849 * the Style in the paint. 1850 * 1851 * @param oval The rectangle bounds of the oval to be drawn 1852 */ drawOval(@onNull RectF oval, @NonNull Paint paint)1853 public void drawOval(@NonNull RectF oval, @NonNull Paint paint) { 1854 super.drawOval(oval, paint); 1855 } 1856 1857 /** 1858 * Draw the specified oval using the specified paint. The oval will be filled or framed based on 1859 * the Style in the paint. 1860 */ drawOval(float left, float top, float right, float bottom, @NonNull Paint paint)1861 public void drawOval(float left, float top, float right, float bottom, @NonNull Paint paint) { 1862 super.drawOval(left, top, right, bottom, paint); 1863 } 1864 1865 /** 1866 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified paint. 1867 * This is equivalent (but faster) to drawing an infinitely large rectangle with the specified 1868 * paint. 1869 * 1870 * @param paint The paint used to draw onto the canvas 1871 */ drawPaint(@onNull Paint paint)1872 public void drawPaint(@NonNull Paint paint) { 1873 super.drawPaint(paint); 1874 } 1875 1876 /** 1877 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1878 * 1879 * @param patch The ninepatch object to render 1880 * @param dst The destination rectangle. 1881 * @param paint The paint to draw the bitmap with. may be null 1882 * @hide 1883 */ drawPatch(@onNull NinePatch patch, @NonNull Rect dst, @Nullable Paint paint)1884 public void drawPatch(@NonNull NinePatch patch, @NonNull Rect dst, @Nullable Paint paint) { 1885 super.drawPatch(patch, dst, paint); 1886 } 1887 1888 /** 1889 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1890 * 1891 * @param patch The ninepatch object to render 1892 * @param dst The destination rectangle. 1893 * @param paint The paint to draw the bitmap with. may be null 1894 * @hide 1895 */ drawPatch(@onNull NinePatch patch, @NonNull RectF dst, @Nullable Paint paint)1896 public void drawPatch(@NonNull NinePatch patch, @NonNull RectF dst, @Nullable Paint paint) { 1897 super.drawPatch(patch, dst, paint); 1898 } 1899 1900 /** 1901 * Draw the specified path using the specified paint. The path will be filled or framed based on 1902 * the Style in the paint. 1903 * 1904 * @param path The path to be drawn 1905 * @param paint The paint used to draw the path 1906 */ drawPath(@onNull Path path, @NonNull Paint paint)1907 public void drawPath(@NonNull Path path, @NonNull Paint paint) { 1908 super.drawPath(path, paint); 1909 } 1910 1911 /** 1912 * Helper for drawPoints() for drawing a single point. 1913 */ drawPoint(float x, float y, @NonNull Paint paint)1914 public void drawPoint(float x, float y, @NonNull Paint paint) { 1915 super.drawPoint(x, y, paint); 1916 } 1917 1918 /** 1919 * Draw a series of points. Each point is centered at the coordinate specified by pts[], and its 1920 * diameter is specified by the paint's stroke width (as transformed by the canvas' CTM), with 1921 * special treatment for a stroke width of 0, which always draws exactly 1 pixel (or at most 4 1922 * if antialiasing is enabled). The shape of the point is controlled by the paint's Cap type. 1923 * The shape is a square, unless the cap type is Round, in which case the shape is a circle. 1924 * 1925 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 1926 * @param offset Number of values to skip before starting to draw. 1927 * @param count The number of values to process, after skipping offset of them. Since one point 1928 * uses two values, the number of "points" that are drawn is really (count >> 1). 1929 * @param paint The paint used to draw the points 1930 */ drawPoints(@izemultiple = 2) float[] pts, int offset, int count, @NonNull Paint paint)1931 public void drawPoints(@Size(multiple = 2) float[] pts, int offset, int count, 1932 @NonNull Paint paint) { 1933 super.drawPoints(pts, offset, count, paint); 1934 } 1935 1936 /** 1937 * Helper for drawPoints() that assumes you want to draw the entire array 1938 */ drawPoints(@izemultiple = 2) @onNull float[] pts, @NonNull Paint paint)1939 public void drawPoints(@Size(multiple = 2) @NonNull float[] pts, @NonNull Paint paint) { 1940 super.drawPoints(pts, paint); 1941 } 1942 1943 /** 1944 * Draw the text in the array, with each character's origin specified by the pos array. 1945 * 1946 * @param text The text to be drawn 1947 * @param index The index of the first character to draw 1948 * @param count The number of characters to draw, starting from index. 1949 * @param pos Array of [x,y] positions, used to position each character 1950 * @param paint The paint used for the text (e.g. color, size, style) 1951 * @deprecated This method does not support glyph composition and decomposition and should 1952 * therefore not be used to render complex scripts. It also doesn't handle 1953 * supplementary characters (eg emoji). 1954 */ 1955 @Deprecated drawPosText(@onNull char[] text, int index, int count, @NonNull @Size(multiple = 2) float[] pos, @NonNull Paint paint)1956 public void drawPosText(@NonNull char[] text, int index, int count, 1957 @NonNull @Size(multiple = 2) float[] pos, 1958 @NonNull Paint paint) { 1959 super.drawPosText(text, index, count, pos, paint); 1960 } 1961 1962 /** 1963 * Draw the text in the array, with each character's origin specified by the pos array. 1964 * 1965 * @param text The text to be drawn 1966 * @param pos Array of [x,y] positions, used to position each character 1967 * @param paint The paint used for the text (e.g. color, size, style) 1968 * @deprecated This method does not support glyph composition and decomposition and should 1969 * therefore not be used to render complex scripts. It also doesn't handle 1970 * supplementary characters (eg emoji). 1971 */ 1972 @Deprecated drawPosText(@onNull String text, @NonNull @Size(multiple = 2) float[] pos, @NonNull Paint paint)1973 public void drawPosText(@NonNull String text, @NonNull @Size(multiple = 2) float[] pos, 1974 @NonNull Paint paint) { 1975 super.drawPosText(text, pos, paint); 1976 } 1977 1978 /** 1979 * Draw the specified Rect using the specified paint. The rectangle will be filled or framed 1980 * based on the Style in the paint. 1981 * 1982 * @param rect The rect to be drawn 1983 * @param paint The paint used to draw the rect 1984 */ drawRect(@onNull RectF rect, @NonNull Paint paint)1985 public void drawRect(@NonNull RectF rect, @NonNull Paint paint) { 1986 super.drawRect(rect, paint); 1987 } 1988 1989 /** 1990 * Draw the specified Rect using the specified Paint. The rectangle will be filled or framed 1991 * based on the Style in the paint. 1992 * 1993 * @param r The rectangle to be drawn. 1994 * @param paint The paint used to draw the rectangle 1995 */ drawRect(@onNull Rect r, @NonNull Paint paint)1996 public void drawRect(@NonNull Rect r, @NonNull Paint paint) { 1997 super.drawRect(r, paint); 1998 } 1999 2000 /** 2001 * Draw the specified Rect using the specified paint. The rectangle will be filled or framed 2002 * based on the Style in the paint. 2003 * 2004 * @param left The left side of the rectangle to be drawn 2005 * @param top The top side of the rectangle to be drawn 2006 * @param right The right side of the rectangle to be drawn 2007 * @param bottom The bottom side of the rectangle to be drawn 2008 * @param paint The paint used to draw the rect 2009 */ drawRect(float left, float top, float right, float bottom, @NonNull Paint paint)2010 public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) { 2011 super.drawRect(left, top, right, bottom, paint); 2012 } 2013 2014 /** 2015 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified RGB color, 2016 * using srcover porterduff mode. 2017 * 2018 * @param r red component (0..255) of the color to draw onto the canvas 2019 * @param g green component (0..255) of the color to draw onto the canvas 2020 * @param b blue component (0..255) of the color to draw onto the canvas 2021 */ drawRGB(int r, int g, int b)2022 public void drawRGB(int r, int g, int b) { 2023 super.drawRGB(r, g, b); 2024 } 2025 2026 /** 2027 * Draw the specified round-rect using the specified paint. The roundrect will be filled or 2028 * framed based on the Style in the paint. 2029 * 2030 * @param rect The rectangular bounds of the roundRect to be drawn 2031 * @param rx The x-radius of the oval used to round the corners 2032 * @param ry The y-radius of the oval used to round the corners 2033 * @param paint The paint used to draw the roundRect 2034 */ drawRoundRect(@onNull RectF rect, float rx, float ry, @NonNull Paint paint)2035 public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { 2036 super.drawRoundRect(rect, rx, ry, paint); 2037 } 2038 2039 /** 2040 * Draw the specified round-rect using the specified paint. The roundrect will be filled or 2041 * framed based on the Style in the paint. 2042 * 2043 * @param rx The x-radius of the oval used to round the corners 2044 * @param ry The y-radius of the oval used to round the corners 2045 * @param paint The paint used to draw the roundRect 2046 */ drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint)2047 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 2048 @NonNull Paint paint) { 2049 super.drawRoundRect(left, top, right, bottom, rx, ry, paint); 2050 } 2051 2052 /** 2053 * Draws a double rounded rectangle using the specified paint. The resultant round rect 2054 * will be filled in the area defined between the outer and inner rectangular bounds if 2055 * the {@link Paint} configured with {@link Paint.Style#FILL}. 2056 * Otherwise if {@link Paint.Style#STROKE} is used, then 2 rounded rect strokes will 2057 * be drawn at the outer and inner rounded rectangles 2058 * 2059 * @param outer The outer rectangular bounds of the roundRect to be drawn 2060 * @param outerRx The x-radius of the oval used to round the corners on the outer rectangle 2061 * @param outerRy The y-radius of the oval used to round the corners on the outer rectangle 2062 * @param inner The inner rectangular bounds of the roundRect to be drawn 2063 * @param innerRx The x-radius of the oval used to round the corners on the inner rectangle 2064 * @param innerRy The y-radius of the oval used to round the corners on the outer rectangle 2065 * @param paint The paint used to draw the double roundRect 2066 */ 2067 @Override drawDoubleRoundRect(@onNull RectF outer, float outerRx, float outerRy, @NonNull RectF inner, float innerRx, float innerRy, @NonNull Paint paint)2068 public void drawDoubleRoundRect(@NonNull RectF outer, float outerRx, float outerRy, 2069 @NonNull RectF inner, float innerRx, float innerRy, @NonNull Paint paint) { 2070 super.drawDoubleRoundRect(outer, outerRx, outerRy, inner, innerRx, innerRy, paint); 2071 } 2072 2073 /** 2074 * Draws a double rounded rectangle using the specified paint. The resultant round rect 2075 * will be filled in the area defined between the outer and inner rectangular bounds if 2076 * the {@link Paint} configured with {@link Paint.Style#FILL}. 2077 * Otherwise if {@link Paint.Style#STROKE} is used, then 2 rounded rect strokes will 2078 * be drawn at the outer and inner rounded rectangles 2079 * 2080 * @param outer The outer rectangular bounds of the roundRect to be drawn 2081 * @param outerRadii Array of 8 float representing the x, y corner radii for top left, 2082 * top right, bottom right, bottom left corners respectively on the outer 2083 * rounded rectangle 2084 * 2085 * @param inner The inner rectangular bounds of the roundRect to be drawn 2086 * @param innerRadii Array of 8 float representing the x, y corner radii for top left, 2087 * top right, bottom right, bottom left corners respectively on the 2088 * outer rounded rectangle 2089 * @param paint The paint used to draw the double roundRect 2090 */ 2091 @Override drawDoubleRoundRect(@onNull RectF outer, @NonNull float[] outerRadii, @NonNull RectF inner, @NonNull float[] innerRadii, @NonNull Paint paint)2092 public void drawDoubleRoundRect(@NonNull RectF outer, @NonNull float[] outerRadii, 2093 @NonNull RectF inner, @NonNull float[] innerRadii, @NonNull Paint paint) { 2094 super.drawDoubleRoundRect(outer, outerRadii, inner, innerRadii, paint); 2095 } 2096 2097 /** 2098 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2099 * based on the Align setting in the paint. 2100 * 2101 * @param text The text to be drawn 2102 * @param x The x-coordinate of the origin of the text being drawn 2103 * @param y The y-coordinate of the baseline of the text being drawn 2104 * @param paint The paint used for the text (e.g. color, size, style) 2105 */ drawText(@onNull char[] text, int index, int count, float x, float y, @NonNull Paint paint)2106 public void drawText(@NonNull char[] text, int index, int count, float x, float y, 2107 @NonNull Paint paint) { 2108 super.drawText(text, index, count, x, y, paint); 2109 } 2110 2111 /** 2112 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2113 * based on the Align setting in the paint. 2114 * 2115 * @param text The text to be drawn 2116 * @param x The x-coordinate of the origin of the text being drawn 2117 * @param y The y-coordinate of the baseline of the text being drawn 2118 * @param paint The paint used for the text (e.g. color, size, style) 2119 */ drawText(@onNull String text, float x, float y, @NonNull Paint paint)2120 public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { 2121 super.drawText(text, x, y, paint); 2122 } 2123 2124 /** 2125 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2126 * based on the Align setting in the paint. 2127 * 2128 * @param text The text to be drawn 2129 * @param start The index of the first character in text to draw 2130 * @param end (end - 1) is the index of the last character in text to draw 2131 * @param x The x-coordinate of the origin of the text being drawn 2132 * @param y The y-coordinate of the baseline of the text being drawn 2133 * @param paint The paint used for the text (e.g. color, size, style) 2134 */ drawText(@onNull String text, int start, int end, float x, float y, @NonNull Paint paint)2135 public void drawText(@NonNull String text, int start, int end, float x, float y, 2136 @NonNull Paint paint) { 2137 super.drawText(text, start, end, x, y, paint); 2138 } 2139 2140 /** 2141 * Draw the specified range of text, specified by start/end, with its origin at (x,y), in the 2142 * specified Paint. The origin is interpreted based on the Align setting in the Paint. 2143 * 2144 * @param text The text to be drawn 2145 * @param start The index of the first character in text to draw 2146 * @param end (end - 1) is the index of the last character in text to draw 2147 * @param x The x-coordinate of origin for where to draw the text 2148 * @param y The y-coordinate of origin for where to draw the text 2149 * @param paint The paint used for the text (e.g. color, size, style) 2150 */ drawText(@onNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint)2151 public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, 2152 @NonNull Paint paint) { 2153 super.drawText(text, start, end, x, y, paint); 2154 } 2155 2156 /** 2157 * Draw the text, with origin at (x,y), using the specified paint, along the specified path. The 2158 * paint's Align setting determines where along the path to start the text. 2159 * 2160 * @param text The text to be drawn 2161 * @param index The starting index within the text to be drawn 2162 * @param count Starting from index, the number of characters to draw 2163 * @param path The path the text should follow for its baseline 2164 * @param hOffset The distance along the path to add to the text's starting position 2165 * @param vOffset The distance above(-) or below(+) the path to position the text 2166 * @param paint The paint used for the text (e.g. color, size, style) 2167 */ drawTextOnPath(@onNull char[] text, int index, int count, @NonNull Path path, float hOffset, float vOffset, @NonNull Paint paint)2168 public void drawTextOnPath(@NonNull char[] text, int index, int count, @NonNull Path path, 2169 float hOffset, float vOffset, @NonNull Paint paint) { 2170 super.drawTextOnPath(text, index, count, path, hOffset, vOffset, paint); 2171 } 2172 2173 /** 2174 * Draw the text, with origin at (x,y), using the specified paint, along the specified path. The 2175 * paint's Align setting determines where along the path to start the text. 2176 * 2177 * @param text The text to be drawn 2178 * @param path The path the text should follow for its baseline 2179 * @param hOffset The distance along the path to add to the text's starting position 2180 * @param vOffset The distance above(-) or below(+) the path to position the text 2181 * @param paint The paint used for the text (e.g. color, size, style) 2182 */ drawTextOnPath(@onNull String text, @NonNull Path path, float hOffset, float vOffset, @NonNull Paint paint)2183 public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset, 2184 float vOffset, @NonNull Paint paint) { 2185 super.drawTextOnPath(text, path, hOffset, vOffset, paint); 2186 } 2187 2188 /** 2189 * Draw a run of text, all in a single direction, with optional context for complex text 2190 * shaping. 2191 * <p> 2192 * See {@link #drawTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)} for 2193 * more details. This method uses a character array rather than CharSequence to represent the 2194 * string. Also, to be consistent with the pattern established in {@link #drawText}, in this 2195 * method {@code count} and {@code contextCount} are used rather than offsets of the end 2196 * position; {@code count = end - start, contextCount = contextEnd - 2197 * contextStart}. 2198 * 2199 * @param text the text to render 2200 * @param index the start of the text to render 2201 * @param count the count of chars to render 2202 * @param contextIndex the start of the context for shaping. Must be no greater than index. 2203 * @param contextCount the number of characters in the context for shaping. contexIndex + 2204 * contextCount must be no less than index + count. 2205 * @param x the x position at which to draw the text 2206 * @param y the y position at which to draw the text 2207 * @param isRtl whether the run is in RTL direction 2208 * @param paint the paint 2209 */ drawTextRun(@onNull char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint)2210 public void drawTextRun(@NonNull char[] text, int index, int count, int contextIndex, 2211 int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint) { 2212 super.drawTextRun(text, index, count, contextIndex, contextCount, x, y, isRtl, paint); 2213 } 2214 2215 /** 2216 * Draw a run of text, all in a single direction, with optional context for complex text 2217 * shaping. 2218 * <p> 2219 * The run of text includes the characters from {@code start} to {@code end} in the text. In 2220 * addition, the range {@code contextStart} to {@code contextEnd} is used as context for the 2221 * purpose of complex text shaping, such as Arabic text potentially shaped differently based on 2222 * the text next to it. 2223 * <p> 2224 * All text outside the range {@code contextStart..contextEnd} is ignored. The text between 2225 * {@code start} and {@code end} will be laid out and drawn. The context range is useful for 2226 * contextual shaping, e.g. Kerning, Arabic contextural form. 2227 * <p> 2228 * The direction of the run is explicitly specified by {@code isRtl}. Thus, this method is 2229 * suitable only for runs of a single direction. Alignment of the text is as determined by the 2230 * Paint's TextAlign value. Further, {@code 0 <= contextStart <= start <= end <= contextEnd 2231 * <= text.length} must hold on entry. 2232 * <p> 2233 * Also see {@link android.graphics.Paint#getRunAdvance} for a corresponding method to measure 2234 * the text; the advance width of the text drawn matches the value obtained from that method. 2235 * 2236 * @param text the text to render 2237 * @param start the start of the text to render. Data before this position can be used for 2238 * shaping context. 2239 * @param end the end of the text to render. Data at or after this position can be used for 2240 * shaping context. 2241 * @param contextStart the index of the start of the shaping context 2242 * @param contextEnd the index of the end of the shaping context 2243 * @param x the x position at which to draw the text 2244 * @param y the y position at which to draw the text 2245 * @param isRtl whether the run is in RTL direction 2246 * @param paint the paint 2247 * @see #drawTextRun(char[], int, int, int, int, float, float, boolean, Paint) 2248 */ drawTextRun(@onNull CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint)2249 public void drawTextRun(@NonNull CharSequence text, int start, int end, int contextStart, 2250 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 2251 super.drawTextRun(text, start, end, contextStart, contextEnd, x, y, isRtl, paint); 2252 } 2253 2254 /** 2255 * Draw a run of text, all in a single direction, with optional context for complex text 2256 * shaping. 2257 * <p> 2258 * See {@link #drawTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)} for 2259 * more details. This method uses a {@link MeasuredText} rather than CharSequence to represent 2260 * the string. 2261 * 2262 * @param text the text to render 2263 * @param start the start of the text to render. Data before this position can be used for 2264 * shaping context. 2265 * @param end the end of the text to render. Data at or after this position can be used for 2266 * shaping context. 2267 * @param contextStart the index of the start of the shaping context 2268 * @param contextEnd the index of the end of the shaping context 2269 * @param x the x position at which to draw the text 2270 * @param y the y position at which to draw the text 2271 * @param isRtl whether the run is in RTL direction 2272 * @param paint the paint 2273 */ drawTextRun(@onNull MeasuredText text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint)2274 public void drawTextRun(@NonNull MeasuredText text, int start, int end, int contextStart, 2275 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 2276 super.drawTextRun(text, start, end, contextStart, contextEnd, x, y, isRtl, paint); 2277 } 2278 2279 /** 2280 * Draw the array of vertices, interpreted as triangles (based on mode). The verts array is 2281 * required, and specifies the x,y pairs for each vertex. If texs is non-null, then it is used 2282 * to specify the coordinate in shader coordinates to use at each vertex (the paint must have a 2283 * shader in this case). If there is no texs array, but there is a color array, then each color 2284 * is interpolated across its corresponding triangle in a gradient. If both texs and colors 2285 * arrays are present, then they behave as before, but the resulting color at each pixels is the 2286 * result of multiplying the colors from the shader and the color-gradient together. The indices 2287 * array is optional, but if it is present, then it is used to specify the index of each 2288 * triangle, rather than just walking through the arrays in order. 2289 * 2290 * @param mode How to interpret the array of vertices 2291 * @param vertexCount The number of values in the vertices array (and corresponding texs and 2292 * colors arrays if non-null). Each logical vertex is two values (x, y), vertexCount 2293 * must be a multiple of 2. 2294 * @param verts Array of vertices for the mesh 2295 * @param vertOffset Number of values in the verts to skip before drawing. 2296 * @param texs May be null. If not null, specifies the coordinates to sample into the current 2297 * shader (e.g. bitmap tile or gradient) 2298 * @param texOffset Number of values in texs to skip before drawing. 2299 * @param colors May be null. If not null, specifies a color for each vertex, to be interpolated 2300 * across the triangle. 2301 * @param colorOffset Number of values in colors to skip before drawing. 2302 * @param indices If not null, array of indices to reference into the vertex (texs, colors) 2303 * array. 2304 * @param indexCount number of entries in the indices array (if not null). 2305 * @param paint Specifies the shader to use if the texs array is non-null. 2306 */ drawVertices(@onNull VertexMode mode, int vertexCount, @NonNull float[] verts, int vertOffset, @Nullable float[] texs, int texOffset, @Nullable int[] colors, int colorOffset, @Nullable short[] indices, int indexOffset, int indexCount, @NonNull Paint paint)2307 public void drawVertices(@NonNull VertexMode mode, int vertexCount, @NonNull float[] verts, 2308 int vertOffset, @Nullable float[] texs, int texOffset, @Nullable int[] colors, 2309 int colorOffset, @Nullable short[] indices, int indexOffset, int indexCount, 2310 @NonNull Paint paint) { 2311 super.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, 2312 colors, colorOffset, indices, indexOffset, indexCount, paint); 2313 } 2314 2315 /** 2316 * Draws the given RenderNode. This is only supported in hardware rendering, which can be 2317 * verified by asserting that {@link #isHardwareAccelerated()} is true. If 2318 * {@link #isHardwareAccelerated()} is false then this throws an exception. 2319 * 2320 * See {@link RenderNode} for more information on what a RenderNode is and how to use it. 2321 * 2322 * @param renderNode The RenderNode to draw, must be non-null. 2323 */ drawRenderNode(@onNull RenderNode renderNode)2324 public void drawRenderNode(@NonNull RenderNode renderNode) { 2325 throw new IllegalArgumentException("Software rendering doesn't support drawRenderNode"); 2326 } 2327 } 2328