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.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 */ 1166 public enum EdgeType { 1167 1168 /** 1169 * Black-and-White: Treat edges by just rounding to nearest pixel boundary 1170 */ 1171 BW(0), //!< treat edges by just rounding to nearest pixel boundary 1172 1173 /** 1174 * Antialiased: Treat edges by rounding-out, since they may be antialiased 1175 */ 1176 AA(1); 1177 EdgeType(int nativeInt)1178 EdgeType(int nativeInt) { 1179 this.nativeInt = nativeInt; 1180 } 1181 1182 /** 1183 * @hide 1184 */ 1185 public final int nativeInt; 1186 } 1187 1188 /** 1189 * Return true if the specified rectangle, after being transformed by the 1190 * current matrix, would lie completely outside of the current clip. Call 1191 * this to check if an area you intend to draw into is clipped out (and 1192 * therefore you can skip making the draw calls). 1193 * 1194 * @param rect the rect to compare with the current clip 1195 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1196 * since that means it may affect a larger area (more pixels) than 1197 * non-antialiased ({@link Canvas.EdgeType#BW}). 1198 * @return true if the rect (transformed by the canvas' matrix) 1199 * does not intersect with the canvas' clip 1200 */ quickReject(@onNull RectF rect, @NonNull EdgeType type)1201 public boolean quickReject(@NonNull RectF rect, @NonNull EdgeType type) { 1202 return nQuickReject(mNativeCanvasWrapper, 1203 rect.left, rect.top, rect.right, rect.bottom); 1204 } 1205 1206 /** 1207 * Return true if the specified path, after being transformed by the 1208 * current matrix, would lie completely outside of the current clip. Call 1209 * this to check if an area you intend to draw into is clipped out (and 1210 * therefore you can skip making the draw calls). Note: for speed it may 1211 * return false even if the path itself might not intersect the clip 1212 * (i.e. the bounds of the path intersects, but the path does not). 1213 * 1214 * @param path The path to compare with the current clip 1215 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1216 * since that means it may affect a larger area (more pixels) than 1217 * non-antialiased ({@link Canvas.EdgeType#BW}). 1218 * @return true if the path (transformed by the canvas' matrix) 1219 * does not intersect with the canvas' clip 1220 */ quickReject(@onNull Path path, @NonNull EdgeType type)1221 public boolean quickReject(@NonNull Path path, @NonNull EdgeType type) { 1222 return nQuickReject(mNativeCanvasWrapper, path.readOnlyNI()); 1223 } 1224 1225 /** 1226 * Return true if the specified rectangle, after being transformed by the 1227 * current matrix, would lie completely outside of the current clip. Call 1228 * this to check if an area you intend to draw into is clipped out (and 1229 * therefore you can skip making the draw calls). 1230 * 1231 * @param left The left side of the rectangle to compare with the 1232 * current clip 1233 * @param top The top of the rectangle to compare with the current 1234 * clip 1235 * @param right The right side of the rectangle to compare with the 1236 * current clip 1237 * @param bottom The bottom of the rectangle to compare with the 1238 * current clip 1239 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 1240 * since that means it may affect a larger area (more pixels) than 1241 * non-antialiased ({@link Canvas.EdgeType#BW}). 1242 * @return true if the rect (transformed by the canvas' matrix) 1243 * does not intersect with the canvas' clip 1244 */ quickReject(float left, float top, float right, float bottom, @NonNull EdgeType type)1245 public boolean quickReject(float left, float top, float right, float bottom, 1246 @NonNull EdgeType type) { 1247 return nQuickReject(mNativeCanvasWrapper, left, top, right, bottom); 1248 } 1249 1250 /** 1251 * Return the bounds of the current clip (in local coordinates) in the 1252 * bounds parameter, and return true if it is non-empty. This can be useful 1253 * in a way similar to quickReject, in that it tells you that drawing 1254 * outside of these bounds will be clipped out. 1255 * 1256 * @param bounds Return the clip bounds here. If it is null, ignore it but 1257 * still return true if the current clip is non-empty. 1258 * @return true if the current clip is non-empty. 1259 */ getClipBounds(@ullable Rect bounds)1260 public boolean getClipBounds(@Nullable Rect bounds) { 1261 return nGetClipBounds(mNativeCanvasWrapper, bounds); 1262 } 1263 1264 /** 1265 * Retrieve the bounds of the current clip (in local coordinates). 1266 * 1267 * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty. 1268 */ getClipBounds()1269 public final @NonNull Rect getClipBounds() { 1270 Rect r = new Rect(); 1271 getClipBounds(r); 1272 return r; 1273 } 1274 1275 /** 1276 * Save the canvas state, draw the picture, and restore the canvas state. 1277 * This differs from picture.draw(canvas), which does not perform any 1278 * save/restore. 1279 * 1280 * <p> 1281 * <strong>Note:</strong> This forces the picture to internally call 1282 * {@link Picture#endRecording} in order to prepare for playback. 1283 * 1284 * @param picture The picture to be drawn 1285 */ drawPicture(@onNull Picture picture)1286 public void drawPicture(@NonNull Picture picture) { 1287 picture.endRecording(); 1288 int restoreCount = save(); 1289 picture.draw(this); 1290 restoreToCount(restoreCount); 1291 } 1292 1293 /** 1294 * Draw the picture, stretched to fit into the dst rectangle. 1295 */ drawPicture(@onNull Picture picture, @NonNull RectF dst)1296 public void drawPicture(@NonNull Picture picture, @NonNull RectF dst) { 1297 save(); 1298 translate(dst.left, dst.top); 1299 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1300 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1301 } 1302 drawPicture(picture); 1303 restore(); 1304 } 1305 1306 /** 1307 * Draw the picture, stretched to fit into the dst rectangle. 1308 */ drawPicture(@onNull Picture picture, @NonNull Rect dst)1309 public void drawPicture(@NonNull Picture picture, @NonNull Rect dst) { 1310 save(); 1311 translate(dst.left, dst.top); 1312 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1313 scale((float) dst.width() / picture.getWidth(), 1314 (float) dst.height() / picture.getHeight()); 1315 } 1316 drawPicture(picture); 1317 restore(); 1318 } 1319 1320 public enum VertexMode { 1321 TRIANGLES(0), 1322 TRIANGLE_STRIP(1), 1323 TRIANGLE_FAN(2); 1324 VertexMode(int nativeInt)1325 VertexMode(int nativeInt) { 1326 this.nativeInt = nativeInt; 1327 } 1328 1329 /** 1330 * @hide 1331 */ 1332 public final int nativeInt; 1333 } 1334 1335 /** 1336 * Releases the resources associated with this canvas. 1337 * 1338 * @hide 1339 */ 1340 @UnsupportedAppUsage release()1341 public void release() { 1342 mNativeCanvasWrapper = 0; 1343 if (mFinalizer != null) { 1344 mFinalizer.run(); 1345 mFinalizer = null; 1346 } 1347 } 1348 1349 /** 1350 * Free up as much memory as possible from private caches (e.g. fonts, images) 1351 * 1352 * @hide 1353 */ 1354 @UnsupportedAppUsage freeCaches()1355 public static void freeCaches() { 1356 nFreeCaches(); 1357 } 1358 1359 /** 1360 * Free up text layout caches 1361 * 1362 * @hide 1363 */ 1364 @UnsupportedAppUsage freeTextLayoutCaches()1365 public static void freeTextLayoutCaches() { 1366 nFreeTextLayoutCaches(); 1367 } 1368 1369 /** @hide */ setCompatibilityVersion(int apiLevel)1370 public static void setCompatibilityVersion(int apiLevel) { 1371 sCompatiblityVersion = apiLevel; 1372 nSetCompatibilityVersion(apiLevel); 1373 } 1374 nFreeCaches()1375 private static native void nFreeCaches(); nFreeTextLayoutCaches()1376 private static native void nFreeTextLayoutCaches(); nGetNativeFinalizer()1377 private static native long nGetNativeFinalizer(); nSetCompatibilityVersion(int apiLevel)1378 private static native void nSetCompatibilityVersion(int apiLevel); 1379 1380 // ---------------- @FastNative ------------------- 1381 1382 @FastNative nInitRaster(long bitmapHandle)1383 private static native long nInitRaster(long bitmapHandle); 1384 1385 @FastNative nSetBitmap(long canvasHandle, long bitmapHandle)1386 private static native void nSetBitmap(long canvasHandle, long bitmapHandle); 1387 1388 @FastNative nGetClipBounds(long nativeCanvas, Rect bounds)1389 private static native boolean nGetClipBounds(long nativeCanvas, Rect bounds); 1390 1391 // ---------------- @CriticalNative ------------------- 1392 1393 @CriticalNative nIsOpaque(long canvasHandle)1394 private static native boolean nIsOpaque(long canvasHandle); 1395 @CriticalNative nGetWidth(long canvasHandle)1396 private static native int nGetWidth(long canvasHandle); 1397 @CriticalNative nGetHeight(long canvasHandle)1398 private static native int nGetHeight(long canvasHandle); 1399 1400 @CriticalNative nSave(long canvasHandle, int saveFlags)1401 private static native int nSave(long canvasHandle, int saveFlags); 1402 @CriticalNative nSaveLayer(long nativeCanvas, float l, float t, float r, float b, long nativePaint, int layerFlags)1403 private static native int nSaveLayer(long nativeCanvas, float l, float t, float r, float b, 1404 long nativePaint, int layerFlags); 1405 @CriticalNative nSaveLayerAlpha(long nativeCanvas, float l, float t, float r, float b, int alpha, int layerFlags)1406 private static native int nSaveLayerAlpha(long nativeCanvas, float l, float t, float r, float b, 1407 int alpha, int layerFlags); 1408 @CriticalNative nSaveUnclippedLayer(long nativeCanvas, int l, int t, int r, int b)1409 private static native int nSaveUnclippedLayer(long nativeCanvas, int l, int t, int r, int b); 1410 @CriticalNative nRestoreUnclippedLayer(long nativeCanvas, int saveCount, long nativePaint)1411 private static native void nRestoreUnclippedLayer(long nativeCanvas, int saveCount, 1412 long nativePaint); 1413 @CriticalNative nRestore(long canvasHandle)1414 private static native boolean nRestore(long canvasHandle); 1415 @CriticalNative nRestoreToCount(long canvasHandle, int saveCount)1416 private static native void nRestoreToCount(long canvasHandle, int saveCount); 1417 @CriticalNative nGetSaveCount(long canvasHandle)1418 private static native int nGetSaveCount(long canvasHandle); 1419 1420 @CriticalNative nTranslate(long canvasHandle, float dx, float dy)1421 private static native void nTranslate(long canvasHandle, float dx, float dy); 1422 @CriticalNative nScale(long canvasHandle, float sx, float sy)1423 private static native void nScale(long canvasHandle, float sx, float sy); 1424 @CriticalNative nRotate(long canvasHandle, float degrees)1425 private static native void nRotate(long canvasHandle, float degrees); 1426 @CriticalNative nSkew(long canvasHandle, float sx, float sy)1427 private static native void nSkew(long canvasHandle, float sx, float sy); 1428 @CriticalNative nConcat(long nativeCanvas, long nativeMatrix)1429 private static native void nConcat(long nativeCanvas, long nativeMatrix); 1430 @CriticalNative nSetMatrix(long nativeCanvas, long nativeMatrix)1431 private static native void nSetMatrix(long nativeCanvas, long nativeMatrix); 1432 @CriticalNative nClipRect(long nativeCanvas, float left, float top, float right, float bottom, int regionOp)1433 private static native boolean nClipRect(long nativeCanvas, 1434 float left, float top, float right, float bottom, int regionOp); 1435 @CriticalNative nClipPath(long nativeCanvas, long nativePath, int regionOp)1436 private static native boolean nClipPath(long nativeCanvas, long nativePath, int regionOp); 1437 @CriticalNative nSetDrawFilter(long nativeCanvas, long nativeFilter)1438 private static native void nSetDrawFilter(long nativeCanvas, long nativeFilter); 1439 @CriticalNative nGetMatrix(long nativeCanvas, long nativeMatrix)1440 private static native void nGetMatrix(long nativeCanvas, long nativeMatrix); 1441 @CriticalNative nQuickReject(long nativeCanvas, long nativePath)1442 private static native boolean nQuickReject(long nativeCanvas, long nativePath); 1443 @CriticalNative nQuickReject(long nativeCanvas, float left, float top, float right, float bottom)1444 private static native boolean nQuickReject(long nativeCanvas, float left, float top, 1445 float right, float bottom); 1446 1447 1448 // ---------------- Draw Methods ------------------- 1449 1450 /** 1451 * <p> 1452 * Draw the specified arc, which will be scaled to fit inside the specified oval. 1453 * </p> 1454 * <p> 1455 * If the start angle is negative or >= 360, the start angle is treated as start angle modulo 1456 * 360. 1457 * </p> 1458 * <p> 1459 * If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs 1460 * slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is 1461 * negative, the sweep angle is treated as sweep angle modulo 360 1462 * </p> 1463 * <p> 1464 * The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 1465 * degrees (3 o'clock on a watch.) 1466 * </p> 1467 * 1468 * @param oval The bounds of oval used to define the shape and size of the arc 1469 * @param startAngle Starting angle (in degrees) where the arc begins 1470 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1471 * @param useCenter If true, include the center of the oval in the arc, and close it if it is 1472 * being stroked. This will draw a wedge 1473 * @param paint The paint used to draw the arc 1474 */ drawArc(@onNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)1475 public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, 1476 @NonNull Paint paint) { 1477 super.drawArc(oval, startAngle, sweepAngle, useCenter, paint); 1478 } 1479 1480 /** 1481 * <p> 1482 * Draw the specified arc, which will be scaled to fit inside the specified oval. 1483 * </p> 1484 * <p> 1485 * If the start angle is negative or >= 360, the start angle is treated as start angle modulo 1486 * 360. 1487 * </p> 1488 * <p> 1489 * If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs 1490 * slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is 1491 * negative, the sweep angle is treated as sweep angle modulo 360 1492 * </p> 1493 * <p> 1494 * The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 1495 * degrees (3 o'clock on a watch.) 1496 * </p> 1497 * 1498 * @param startAngle Starting angle (in degrees) where the arc begins 1499 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1500 * @param useCenter If true, include the center of the oval in the arc, and close it if it is 1501 * being stroked. This will draw a wedge 1502 * @param paint The paint used to draw the arc 1503 */ drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)1504 public void drawArc(float left, float top, float right, float bottom, float startAngle, 1505 float sweepAngle, boolean useCenter, @NonNull Paint paint) { 1506 super.drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint); 1507 } 1508 1509 /** 1510 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified ARGB 1511 * color, using srcover porterduff mode. 1512 * 1513 * @param a alpha component (0..255) of the color to draw onto the canvas 1514 * @param r red component (0..255) of the color to draw onto the canvas 1515 * @param g green component (0..255) of the color to draw onto the canvas 1516 * @param b blue component (0..255) of the color to draw onto the canvas 1517 */ drawARGB(int a, int r, int g, int b)1518 public void drawARGB(int a, int r, int g, int b) { 1519 super.drawARGB(a, r, g, b); 1520 } 1521 1522 /** 1523 * Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, 1524 * transformed by the current matrix. 1525 * <p> 1526 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1527 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1528 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1529 * the edge color replicated. 1530 * <p> 1531 * If the bitmap and canvas have different densities, this function will take care of 1532 * automatically scaling the bitmap to draw at the same density as the canvas. 1533 * 1534 * @param bitmap The bitmap to be drawn 1535 * @param left The position of the left side of the bitmap being drawn 1536 * @param top The position of the top side of the bitmap being drawn 1537 * @param paint The paint used to draw the bitmap (may be null) 1538 */ drawBitmap(@onNull Bitmap bitmap, float left, float top, @Nullable Paint paint)1539 public void drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint) { 1540 super.drawBitmap(bitmap, left, top, paint); 1541 } 1542 1543 /** 1544 * Draw the specified bitmap, scaling/translating automatically to fill the destination 1545 * rectangle. If the source rectangle is not null, it specifies the subset of the bitmap to 1546 * draw. 1547 * <p> 1548 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1549 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1550 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1551 * the edge color replicated. 1552 * <p> 1553 * This function <em>ignores the density associated with the bitmap</em>. This is because the 1554 * source and destination rectangle coordinate spaces are in their respective densities, so must 1555 * already have the appropriate scaling factor applied. 1556 * 1557 * @param bitmap The bitmap to be drawn 1558 * @param src May be null. The subset of the bitmap to be drawn 1559 * @param dst The rectangle that the bitmap will be scaled/translated to fit into 1560 * @param paint May be null. The paint used to draw the bitmap 1561 */ drawBitmap(@onNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)1562 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, 1563 @Nullable Paint paint) { 1564 super.drawBitmap(bitmap, src, dst, paint); 1565 } 1566 1567 /** 1568 * Draw the specified bitmap, scaling/translating automatically to fill the destination 1569 * rectangle. If the source rectangle is not null, it specifies the subset of the bitmap to 1570 * draw. 1571 * <p> 1572 * Note: if the paint contains a maskfilter that generates a mask which extends beyond the 1573 * bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it 1574 * were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be 1575 * the edge color replicated. 1576 * <p> 1577 * This function <em>ignores the density associated with the bitmap</em>. This is because the 1578 * source and destination rectangle coordinate spaces are in their respective densities, so must 1579 * already have the appropriate scaling factor applied. 1580 * 1581 * @param bitmap The bitmap to be drawn 1582 * @param src May be null. The subset of the bitmap to be drawn 1583 * @param dst The rectangle that the bitmap will be scaled/translated to fit into 1584 * @param paint May be null. The paint used to draw the bitmap 1585 */ drawBitmap(@onNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)1586 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, 1587 @Nullable Paint paint) { 1588 super.drawBitmap(bitmap, src, dst, paint); 1589 } 1590 1591 /** 1592 * Treat the specified array of colors as a bitmap, and draw it. This gives the same result as 1593 * first creating a bitmap from the array, and then drawing it, but this method avoids 1594 * explicitly creating a bitmap object which can be more efficient if the colors are changing 1595 * often. 1596 * 1597 * @param colors Array of colors representing the pixels of the bitmap 1598 * @param offset Offset into the array of colors for the first pixel 1599 * @param stride The number of colors in the array between rows (must be >= width or <= -width). 1600 * @param x The X coordinate for where to draw the bitmap 1601 * @param y The Y coordinate for where to draw the bitmap 1602 * @param width The width of the bitmap 1603 * @param height The height of the bitmap 1604 * @param hasAlpha True if the alpha channel of the colors contains valid values. If false, the 1605 * alpha byte is ignored (assumed to be 0xFF for every pixel). 1606 * @param paint May be null. The paint used to draw the bitmap 1607 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1608 * requires an internal copy of color buffer contents every time this method is 1609 * called. Using a Bitmap avoids this copy, and allows the application to more 1610 * explicitly control the lifetime and copies of pixel data. 1611 */ 1612 @Deprecated drawBitmap(@onNull int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, @Nullable Paint paint)1613 public void drawBitmap(@NonNull int[] colors, int offset, int stride, float x, float y, 1614 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1615 super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint); 1616 } 1617 1618 /** 1619 * Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1620 * 1621 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1622 * requires an internal copy of color buffer contents every time this method is 1623 * called. Using a Bitmap avoids this copy, and allows the application to more 1624 * explicitly control the lifetime and copies of pixel data. 1625 */ 1626 @Deprecated drawBitmap(@onNull int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, @Nullable Paint paint)1627 public void drawBitmap(@NonNull int[] colors, int offset, int stride, int x, int y, 1628 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1629 super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint); 1630 } 1631 1632 /** 1633 * Draw the bitmap using the specified matrix. 1634 * 1635 * @param bitmap The bitmap to draw 1636 * @param matrix The matrix used to transform the bitmap when it is drawn 1637 * @param paint May be null. The paint used to draw the bitmap 1638 */ drawBitmap(@onNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)1639 public void drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint) { 1640 super.drawBitmap(bitmap, matrix, paint); 1641 } 1642 1643 /** 1644 * Draw the bitmap through the mesh, where mesh vertices are evenly distributed across the 1645 * bitmap. There are meshWidth+1 vertices across, and meshHeight+1 vertices down. The verts 1646 * array is accessed in row-major order, so that the first meshWidth+1 vertices are distributed 1647 * across the top of the bitmap from left to right. A more general version of this method is 1648 * drawVertices(). 1649 * 1650 * Prior to API level {@value Build.VERSION_CODES#P} vertOffset and colorOffset were ignored, 1651 * effectively treating them as zeros. In API level {@value Build.VERSION_CODES#P} and above 1652 * these parameters will be respected. 1653 * 1654 * @param bitmap The bitmap to draw using the mesh 1655 * @param meshWidth The number of columns in the mesh. Nothing is drawn if this is 0 1656 * @param meshHeight The number of rows in the mesh. Nothing is drawn if this is 0 1657 * @param verts Array of x,y pairs, specifying where the mesh should be drawn. There must be at 1658 * least (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values in the array 1659 * @param vertOffset Number of verts elements to skip before drawing 1660 * @param colors May be null. Specifies a color at each vertex, which is interpolated across the 1661 * cell, and whose values are multiplied by the corresponding bitmap colors. If not 1662 * null, there must be at least (meshWidth+1) * (meshHeight+1) + colorOffset values 1663 * in the array. 1664 * @param colorOffset Number of color elements to skip before drawing 1665 * @param paint May be null. The paint used to draw the bitmap 1666 */ drawBitmapMesh(@onNull Bitmap bitmap, int meshWidth, int meshHeight, @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, @Nullable Paint paint)1667 public void drawBitmapMesh(@NonNull Bitmap bitmap, int meshWidth, int meshHeight, 1668 @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, 1669 @Nullable Paint paint) { 1670 super.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, 1671 paint); 1672 } 1673 1674 /** 1675 * Draw the specified circle using the specified paint. If radius is <= 0, then nothing will be 1676 * drawn. The circle will be filled or framed based on the Style in the paint. 1677 * 1678 * @param cx The x-coordinate of the center of the circle to be drawn 1679 * @param cy The y-coordinate of the center of the circle to be drawn 1680 * @param radius The radius of the circle to be drawn 1681 * @param paint The paint used to draw the circle 1682 */ drawCircle(float cx, float cy, float radius, @NonNull Paint paint)1683 public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) { 1684 super.drawCircle(cx, cy, radius, paint); 1685 } 1686 1687 /** 1688 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, 1689 * using srcover porterduff mode. 1690 * 1691 * @param color the color to draw onto the canvas 1692 */ drawColor(@olorInt int color)1693 public void drawColor(@ColorInt int color) { 1694 super.drawColor(color); 1695 } 1696 1697 /** 1698 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, 1699 * using srcover porterduff mode. 1700 * 1701 * @param color the {@code ColorLong} to draw onto the canvas. See the {@link Color} 1702 * class for details about {@code ColorLong}s. 1703 * @throws IllegalArgumentException if the color space encoded in the {@code ColorLong} 1704 * is invalid or unknown. 1705 */ drawColor(@olorLong long color)1706 public void drawColor(@ColorLong long color) { 1707 super.drawColor(color, BlendMode.SRC_OVER); 1708 } 1709 1710 /** 1711 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1712 * porter-duff xfermode. 1713 * 1714 * @param color the color to draw onto the canvas 1715 * @param mode the porter-duff mode to apply to the color 1716 */ drawColor(@olorInt int color, @NonNull PorterDuff.Mode mode)1717 public void drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode) { 1718 super.drawColor(color, mode); 1719 } 1720 1721 /** 1722 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1723 * blendmode. 1724 * 1725 * @param color the color to draw onto the canvas 1726 * @param mode the blendmode to apply to the color 1727 */ drawColor(@olorInt int color, @NonNull BlendMode mode)1728 public void drawColor(@ColorInt int color, @NonNull BlendMode mode) { 1729 super.drawColor(color, mode); 1730 } 1731 1732 /** 1733 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and 1734 * blendmode. 1735 * 1736 * @param color the {@code ColorLong} to draw onto the canvas. See the {@link Color} 1737 * class for details about {@code ColorLong}s. 1738 * @param mode the blendmode to apply to the color 1739 * @throws IllegalArgumentException if the color space encoded in the {@code ColorLong} 1740 * is invalid or unknown. 1741 */ drawColor(@olorLong long color, @NonNull BlendMode mode)1742 public void drawColor(@ColorLong long color, @NonNull BlendMode mode) { 1743 super.drawColor(color, mode); 1744 } 1745 1746 /** 1747 * Draw a line segment with the specified start and stop x,y coordinates, using the specified 1748 * paint. 1749 * <p> 1750 * Note that since a line is always "framed", the Style is ignored in the paint. 1751 * </p> 1752 * <p> 1753 * Degenerate lines (length is 0) will not be drawn. 1754 * </p> 1755 * 1756 * @param startX The x-coordinate of the start point of the line 1757 * @param startY The y-coordinate of the start point of the line 1758 * @param paint The paint used to draw the line 1759 */ drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint)1760 public void drawLine(float startX, float startY, float stopX, float stopY, 1761 @NonNull Paint paint) { 1762 super.drawLine(startX, startY, stopX, stopY, paint); 1763 } 1764 1765 /** 1766 * Draw a series of lines. Each line is taken from 4 consecutive values in the pts array. Thus 1767 * to draw 1 line, the array must contain at least 4 values. This is logically the same as 1768 * drawing the array as follows: drawLine(pts[0], pts[1], pts[2], pts[3]) followed by 1769 * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on. 1770 * 1771 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 1772 * @param offset Number of values in the array to skip before drawing. 1773 * @param count The number of values in the array to process, after skipping "offset" of them. 1774 * Since each line uses 4 values, the number of "lines" that are drawn is really 1775 * (count >> 2). 1776 * @param paint The paint used to draw the points 1777 */ drawLines(@izemultiple = 4) @onNull float[] pts, int offset, int count, @NonNull Paint paint)1778 public void drawLines(@Size(multiple = 4) @NonNull float[] pts, int offset, int count, 1779 @NonNull Paint paint) { 1780 super.drawLines(pts, offset, count, paint); 1781 } 1782 drawLines(@izemultiple = 4) @onNull float[] pts, @NonNull Paint paint)1783 public void drawLines(@Size(multiple = 4) @NonNull float[] pts, @NonNull Paint paint) { 1784 super.drawLines(pts, paint); 1785 } 1786 1787 /** 1788 * Draw the specified oval using the specified paint. The oval will be filled or framed based on 1789 * the Style in the paint. 1790 * 1791 * @param oval The rectangle bounds of the oval to be drawn 1792 */ drawOval(@onNull RectF oval, @NonNull Paint paint)1793 public void drawOval(@NonNull RectF oval, @NonNull Paint paint) { 1794 super.drawOval(oval, paint); 1795 } 1796 1797 /** 1798 * Draw the specified oval using the specified paint. The oval will be filled or framed based on 1799 * the Style in the paint. 1800 */ drawOval(float left, float top, float right, float bottom, @NonNull Paint paint)1801 public void drawOval(float left, float top, float right, float bottom, @NonNull Paint paint) { 1802 super.drawOval(left, top, right, bottom, paint); 1803 } 1804 1805 /** 1806 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified paint. 1807 * This is equivalent (but faster) to drawing an infinitely large rectangle with the specified 1808 * paint. 1809 * 1810 * @param paint The paint used to draw onto the canvas 1811 */ drawPaint(@onNull Paint paint)1812 public void drawPaint(@NonNull Paint paint) { 1813 super.drawPaint(paint); 1814 } 1815 1816 /** 1817 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1818 * 1819 * @param patch The ninepatch object to render 1820 * @param dst The destination rectangle. 1821 * @param paint The paint to draw the bitmap with. may be null 1822 * @hide 1823 */ drawPatch(@onNull NinePatch patch, @NonNull Rect dst, @Nullable Paint paint)1824 public void drawPatch(@NonNull NinePatch patch, @NonNull Rect dst, @Nullable Paint paint) { 1825 super.drawPatch(patch, dst, paint); 1826 } 1827 1828 /** 1829 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1830 * 1831 * @param patch The ninepatch object to render 1832 * @param dst The destination rectangle. 1833 * @param paint The paint to draw the bitmap with. may be null 1834 * @hide 1835 */ drawPatch(@onNull NinePatch patch, @NonNull RectF dst, @Nullable Paint paint)1836 public void drawPatch(@NonNull NinePatch patch, @NonNull RectF dst, @Nullable Paint paint) { 1837 super.drawPatch(patch, dst, paint); 1838 } 1839 1840 /** 1841 * Draw the specified path using the specified paint. The path will be filled or framed based on 1842 * the Style in the paint. 1843 * 1844 * @param path The path to be drawn 1845 * @param paint The paint used to draw the path 1846 */ drawPath(@onNull Path path, @NonNull Paint paint)1847 public void drawPath(@NonNull Path path, @NonNull Paint paint) { 1848 super.drawPath(path, paint); 1849 } 1850 1851 /** 1852 * Helper for drawPoints() for drawing a single point. 1853 */ drawPoint(float x, float y, @NonNull Paint paint)1854 public void drawPoint(float x, float y, @NonNull Paint paint) { 1855 super.drawPoint(x, y, paint); 1856 } 1857 1858 /** 1859 * Draw a series of points. Each point is centered at the coordinate specified by pts[], and its 1860 * diameter is specified by the paint's stroke width (as transformed by the canvas' CTM), with 1861 * special treatment for a stroke width of 0, which always draws exactly 1 pixel (or at most 4 1862 * if antialiasing is enabled). The shape of the point is controlled by the paint's Cap type. 1863 * The shape is a square, unless the cap type is Round, in which case the shape is a circle. 1864 * 1865 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 1866 * @param offset Number of values to skip before starting to draw. 1867 * @param count The number of values to process, after skipping offset of them. Since one point 1868 * uses two values, the number of "points" that are drawn is really (count >> 1). 1869 * @param paint The paint used to draw the points 1870 */ drawPoints(@izemultiple = 2) float[] pts, int offset, int count, @NonNull Paint paint)1871 public void drawPoints(@Size(multiple = 2) float[] pts, int offset, int count, 1872 @NonNull Paint paint) { 1873 super.drawPoints(pts, offset, count, paint); 1874 } 1875 1876 /** 1877 * Helper for drawPoints() that assumes you want to draw the entire array 1878 */ drawPoints(@izemultiple = 2) @onNull float[] pts, @NonNull Paint paint)1879 public void drawPoints(@Size(multiple = 2) @NonNull float[] pts, @NonNull Paint paint) { 1880 super.drawPoints(pts, paint); 1881 } 1882 1883 /** 1884 * Draw the text in the array, with each character's origin specified by the pos array. 1885 * 1886 * @param text The text to be drawn 1887 * @param index The index of the first character to draw 1888 * @param count The number of characters to draw, starting from index. 1889 * @param pos Array of [x,y] positions, used to position each character 1890 * @param paint The paint used for the text (e.g. color, size, style) 1891 * @deprecated This method does not support glyph composition and decomposition and should 1892 * therefore not be used to render complex scripts. It also doesn't handle 1893 * supplementary characters (eg emoji). 1894 */ 1895 @Deprecated drawPosText(@onNull char[] text, int index, int count, @NonNull @Size(multiple = 2) float[] pos, @NonNull Paint paint)1896 public void drawPosText(@NonNull char[] text, int index, int count, 1897 @NonNull @Size(multiple = 2) float[] pos, 1898 @NonNull Paint paint) { 1899 super.drawPosText(text, index, count, pos, paint); 1900 } 1901 1902 /** 1903 * Draw the text in the array, with each character's origin specified by the pos array. 1904 * 1905 * @param text The text to be drawn 1906 * @param pos Array of [x,y] positions, used to position each character 1907 * @param paint The paint used for the text (e.g. color, size, style) 1908 * @deprecated This method does not support glyph composition and decomposition and should 1909 * therefore not be used to render complex scripts. It also doesn't handle 1910 * supplementary characters (eg emoji). 1911 */ 1912 @Deprecated drawPosText(@onNull String text, @NonNull @Size(multiple = 2) float[] pos, @NonNull Paint paint)1913 public void drawPosText(@NonNull String text, @NonNull @Size(multiple = 2) float[] pos, 1914 @NonNull Paint paint) { 1915 super.drawPosText(text, pos, paint); 1916 } 1917 1918 /** 1919 * Draw the specified Rect using the specified paint. The rectangle will be filled or framed 1920 * based on the Style in the paint. 1921 * 1922 * @param rect The rect to be drawn 1923 * @param paint The paint used to draw the rect 1924 */ drawRect(@onNull RectF rect, @NonNull Paint paint)1925 public void drawRect(@NonNull RectF rect, @NonNull Paint paint) { 1926 super.drawRect(rect, paint); 1927 } 1928 1929 /** 1930 * Draw the specified Rect using the specified Paint. The rectangle will be filled or framed 1931 * based on the Style in the paint. 1932 * 1933 * @param r The rectangle to be drawn. 1934 * @param paint The paint used to draw the rectangle 1935 */ drawRect(@onNull Rect r, @NonNull Paint paint)1936 public void drawRect(@NonNull Rect r, @NonNull Paint paint) { 1937 super.drawRect(r, paint); 1938 } 1939 1940 /** 1941 * Draw the specified Rect using the specified paint. The rectangle will be filled or framed 1942 * based on the Style in the paint. 1943 * 1944 * @param left The left side of the rectangle to be drawn 1945 * @param top The top side of the rectangle to be drawn 1946 * @param right The right side of the rectangle to be drawn 1947 * @param bottom The bottom side of the rectangle to be drawn 1948 * @param paint The paint used to draw the rect 1949 */ drawRect(float left, float top, float right, float bottom, @NonNull Paint paint)1950 public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) { 1951 super.drawRect(left, top, right, bottom, paint); 1952 } 1953 1954 /** 1955 * Fill the entire canvas' bitmap (restricted to the current clip) with the specified RGB color, 1956 * using srcover porterduff mode. 1957 * 1958 * @param r red component (0..255) of the color to draw onto the canvas 1959 * @param g green component (0..255) of the color to draw onto the canvas 1960 * @param b blue component (0..255) of the color to draw onto the canvas 1961 */ drawRGB(int r, int g, int b)1962 public void drawRGB(int r, int g, int b) { 1963 super.drawRGB(r, g, b); 1964 } 1965 1966 /** 1967 * Draw the specified round-rect using the specified paint. The roundrect will be filled or 1968 * framed based on the Style in the paint. 1969 * 1970 * @param rect The rectangular bounds of the roundRect to be drawn 1971 * @param rx The x-radius of the oval used to round the corners 1972 * @param ry The y-radius of the oval used to round the corners 1973 * @param paint The paint used to draw the roundRect 1974 */ drawRoundRect(@onNull RectF rect, float rx, float ry, @NonNull Paint paint)1975 public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { 1976 super.drawRoundRect(rect, rx, ry, paint); 1977 } 1978 1979 /** 1980 * Draw the specified round-rect using the specified paint. The roundrect will be filled or 1981 * framed based on the Style in the paint. 1982 * 1983 * @param rx The x-radius of the oval used to round the corners 1984 * @param ry The y-radius of the oval used to round the corners 1985 * @param paint The paint used to draw the roundRect 1986 */ drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint)1987 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 1988 @NonNull Paint paint) { 1989 super.drawRoundRect(left, top, right, bottom, rx, ry, paint); 1990 } 1991 1992 /** 1993 * Draws a double rounded rectangle using the specified paint. The resultant round rect 1994 * will be filled in the area defined between the outer and inner rectangular bounds if 1995 * the {@link Paint} configured with {@link Paint.Style#FILL}. 1996 * Otherwise if {@link Paint.Style#STROKE} is used, then 2 rounded rect strokes will 1997 * be drawn at the outer and inner rounded rectangles 1998 * 1999 * @param outer The outer rectangular bounds of the roundRect to be drawn 2000 * @param outerRx The x-radius of the oval used to round the corners on the outer rectangle 2001 * @param outerRy The y-radius of the oval used to round the corners on the outer rectangle 2002 * @param inner The inner rectangular bounds of the roundRect to be drawn 2003 * @param innerRx The x-radius of the oval used to round the corners on the inner rectangle 2004 * @param innerRy The y-radius of the oval used to round the corners on the outer rectangle 2005 * @param paint The paint used to draw the double roundRect 2006 */ 2007 @Override drawDoubleRoundRect(@onNull RectF outer, float outerRx, float outerRy, @NonNull RectF inner, float innerRx, float innerRy, @NonNull Paint paint)2008 public void drawDoubleRoundRect(@NonNull RectF outer, float outerRx, float outerRy, 2009 @NonNull RectF inner, float innerRx, float innerRy, @NonNull Paint paint) { 2010 super.drawDoubleRoundRect(outer, outerRx, outerRy, inner, innerRx, innerRy, paint); 2011 } 2012 2013 /** 2014 * Draws a double rounded rectangle using the specified paint. The resultant round rect 2015 * will be filled in the area defined between the outer and inner rectangular bounds if 2016 * the {@link Paint} configured with {@link Paint.Style#FILL}. 2017 * Otherwise if {@link Paint.Style#STROKE} is used, then 2 rounded rect strokes will 2018 * be drawn at the outer and inner rounded rectangles 2019 * 2020 * @param outer The outer rectangular bounds of the roundRect to be drawn 2021 * @param outerRadii Array of 8 float representing the x, y corner radii for top left, 2022 * top right, bottom right, bottom left corners respectively on the outer 2023 * rounded rectangle 2024 * 2025 * @param inner The inner rectangular bounds of the roundRect to be drawn 2026 * @param innerRadii Array of 8 float representing the x, y corner radii for top left, 2027 * top right, bottom right, bottom left corners respectively on the 2028 * outer rounded rectangle 2029 * @param paint The paint used to draw the double roundRect 2030 */ 2031 @Override drawDoubleRoundRect(@onNull RectF outer, @NonNull float[] outerRadii, @NonNull RectF inner, @NonNull float[] innerRadii, @NonNull Paint paint)2032 public void drawDoubleRoundRect(@NonNull RectF outer, @NonNull float[] outerRadii, 2033 @NonNull RectF inner, @NonNull float[] innerRadii, @NonNull Paint paint) { 2034 super.drawDoubleRoundRect(outer, outerRadii, inner, innerRadii, paint); 2035 } 2036 2037 /** 2038 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2039 * based on the Align setting in the paint. 2040 * 2041 * @param text The text to be drawn 2042 * @param x The x-coordinate of the origin of the text being drawn 2043 * @param y The y-coordinate of the baseline of the text being drawn 2044 * @param paint The paint used for the text (e.g. color, size, style) 2045 */ drawText(@onNull char[] text, int index, int count, float x, float y, @NonNull Paint paint)2046 public void drawText(@NonNull char[] text, int index, int count, float x, float y, 2047 @NonNull Paint paint) { 2048 super.drawText(text, index, count, x, y, paint); 2049 } 2050 2051 /** 2052 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2053 * based on the Align setting in the paint. 2054 * 2055 * @param text The text to be drawn 2056 * @param x The x-coordinate of the origin of the text being drawn 2057 * @param y The y-coordinate of the baseline of the text being drawn 2058 * @param paint The paint used for the text (e.g. color, size, style) 2059 */ drawText(@onNull String text, float x, float y, @NonNull Paint paint)2060 public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { 2061 super.drawText(text, x, y, paint); 2062 } 2063 2064 /** 2065 * Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted 2066 * based on the Align setting in the paint. 2067 * 2068 * @param text The text to be drawn 2069 * @param start The index of the first character in text to draw 2070 * @param end (end - 1) is the index of the last character in text to draw 2071 * @param x The x-coordinate of the origin of the text being drawn 2072 * @param y The y-coordinate of the baseline of the text being drawn 2073 * @param paint The paint used for the text (e.g. color, size, style) 2074 */ drawText(@onNull String text, int start, int end, float x, float y, @NonNull Paint paint)2075 public void drawText(@NonNull String text, int start, int end, float x, float y, 2076 @NonNull Paint paint) { 2077 super.drawText(text, start, end, x, y, paint); 2078 } 2079 2080 /** 2081 * Draw the specified range of text, specified by start/end, with its origin at (x,y), in the 2082 * specified Paint. The origin is interpreted based on the Align setting in the Paint. 2083 * 2084 * @param text The text to be drawn 2085 * @param start The index of the first character in text to draw 2086 * @param end (end - 1) is the index of the last character in text to draw 2087 * @param x The x-coordinate of origin for where to draw the text 2088 * @param y The y-coordinate of origin for where to draw the text 2089 * @param paint The paint used for the text (e.g. color, size, style) 2090 */ drawText(@onNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint)2091 public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, 2092 @NonNull Paint paint) { 2093 super.drawText(text, start, end, x, y, paint); 2094 } 2095 2096 /** 2097 * Draw the text, with origin at (x,y), using the specified paint, along the specified path. The 2098 * paint's Align setting determins where along the path to start the text. 2099 * 2100 * @param text The text to be drawn 2101 * @param path The path the text should follow for its baseline 2102 * @param hOffset The distance along the path to add to the text's starting position 2103 * @param vOffset The distance above(-) or below(+) the path to position the text 2104 * @param paint The paint used for the text (e.g. color, size, style) 2105 */ drawTextOnPath(@onNull char[] text, int index, int count, @NonNull Path path, float hOffset, float vOffset, @NonNull Paint paint)2106 public void drawTextOnPath(@NonNull char[] text, int index, int count, @NonNull Path path, 2107 float hOffset, float vOffset, @NonNull Paint paint) { 2108 super.drawTextOnPath(text, index, count, path, hOffset, vOffset, paint); 2109 } 2110 2111 /** 2112 * Draw the text, with origin at (x,y), using the specified paint, along the specified path. The 2113 * paint's Align setting determins where along the path to start the text. 2114 * 2115 * @param text The text to be drawn 2116 * @param path The path the text should follow for its baseline 2117 * @param hOffset The distance along the path to add to the text's starting position 2118 * @param vOffset The distance above(-) or below(+) the path to position the text 2119 * @param paint The paint used for the text (e.g. color, size, style) 2120 */ drawTextOnPath(@onNull String text, @NonNull Path path, float hOffset, float vOffset, @NonNull Paint paint)2121 public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset, 2122 float vOffset, @NonNull Paint paint) { 2123 super.drawTextOnPath(text, path, hOffset, vOffset, paint); 2124 } 2125 2126 /** 2127 * Draw a run of text, all in a single direction, with optional context for complex text 2128 * shaping. 2129 * <p> 2130 * See {@link #drawTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)} for 2131 * more details. This method uses a character array rather than CharSequence to represent the 2132 * string. Also, to be consistent with the pattern established in {@link #drawText}, in this 2133 * method {@code count} and {@code contextCount} are used rather than offsets of the end 2134 * position; {@code count = end - start, contextCount = contextEnd - 2135 * contextStart}. 2136 * 2137 * @param text the text to render 2138 * @param index the start of the text to render 2139 * @param count the count of chars to render 2140 * @param contextIndex the start of the context for shaping. Must be no greater than index. 2141 * @param contextCount the number of characters in the context for shaping. contexIndex + 2142 * contextCount must be no less than index + count. 2143 * @param x the x position at which to draw the text 2144 * @param y the y position at which to draw the text 2145 * @param isRtl whether the run is in RTL direction 2146 * @param paint the paint 2147 */ drawTextRun(@onNull char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint)2148 public void drawTextRun(@NonNull char[] text, int index, int count, int contextIndex, 2149 int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint) { 2150 super.drawTextRun(text, index, count, contextIndex, contextCount, x, y, isRtl, paint); 2151 } 2152 2153 /** 2154 * Draw a run of text, all in a single direction, with optional context for complex text 2155 * shaping. 2156 * <p> 2157 * The run of text includes the characters from {@code start} to {@code end} in the text. In 2158 * addition, the range {@code contextStart} to {@code contextEnd} is used as context for the 2159 * purpose of complex text shaping, such as Arabic text potentially shaped differently based on 2160 * the text next to it. 2161 * <p> 2162 * All text outside the range {@code contextStart..contextEnd} is ignored. The text between 2163 * {@code start} and {@code end} will be laid out and drawn. The context range is useful for 2164 * contextual shaping, e.g. Kerning, Arabic contextural form. 2165 * <p> 2166 * The direction of the run is explicitly specified by {@code isRtl}. Thus, this method is 2167 * suitable only for runs of a single direction. Alignment of the text is as determined by the 2168 * Paint's TextAlign value. Further, {@code 0 <= contextStart <= start <= end <= contextEnd 2169 * <= text.length} must hold on entry. 2170 * <p> 2171 * Also see {@link android.graphics.Paint#getRunAdvance} for a corresponding method to measure 2172 * the text; the advance width of the text drawn matches the value obtained from that method. 2173 * 2174 * @param text the text to render 2175 * @param start the start of the text to render. Data before this position can be used for 2176 * shaping context. 2177 * @param end the end of the text to render. Data at or after this position can be used for 2178 * shaping context. 2179 * @param contextStart the index of the start of the shaping context 2180 * @param contextEnd the index of the end of the shaping context 2181 * @param x the x position at which to draw the text 2182 * @param y the y position at which to draw the text 2183 * @param isRtl whether the run is in RTL direction 2184 * @param paint the paint 2185 * @see #drawTextRun(char[], int, int, int, int, float, float, boolean, Paint) 2186 */ drawTextRun(@onNull CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint)2187 public void drawTextRun(@NonNull CharSequence text, int start, int end, int contextStart, 2188 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 2189 super.drawTextRun(text, start, end, contextStart, contextEnd, x, y, isRtl, paint); 2190 } 2191 2192 /** 2193 * Draw a run of text, all in a single direction, with optional context for complex text 2194 * shaping. 2195 * <p> 2196 * See {@link #drawTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)} for 2197 * more details. This method uses a {@link MeasuredText} rather than CharSequence to represent 2198 * the string. 2199 * 2200 * @param text the text to render 2201 * @param start the start of the text to render. Data before this position can be used for 2202 * shaping context. 2203 * @param end the end of the text to render. Data at or after this position can be used for 2204 * shaping context. 2205 * @param contextStart the index of the start of the shaping context 2206 * @param contextEnd the index of the end of the shaping context 2207 * @param x the x position at which to draw the text 2208 * @param y the y position at which to draw the text 2209 * @param isRtl whether the run is in RTL direction 2210 * @param paint the paint 2211 */ drawTextRun(@onNull MeasuredText text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint)2212 public void drawTextRun(@NonNull MeasuredText text, int start, int end, int contextStart, 2213 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 2214 super.drawTextRun(text, start, end, contextStart, contextEnd, x, y, isRtl, paint); 2215 } 2216 2217 /** 2218 * Draw the array of vertices, interpreted as triangles (based on mode). The verts array is 2219 * required, and specifies the x,y pairs for each vertex. If texs is non-null, then it is used 2220 * to specify the coordinate in shader coordinates to use at each vertex (the paint must have a 2221 * shader in this case). If there is no texs array, but there is a color array, then each color 2222 * is interpolated across its corresponding triangle in a gradient. If both texs and colors 2223 * arrays are present, then they behave as before, but the resulting color at each pixels is the 2224 * result of multiplying the colors from the shader and the color-gradient together. The indices 2225 * array is optional, but if it is present, then it is used to specify the index of each 2226 * triangle, rather than just walking through the arrays in order. 2227 * 2228 * @param mode How to interpret the array of vertices 2229 * @param vertexCount The number of values in the vertices array (and corresponding texs and 2230 * colors arrays if non-null). Each logical vertex is two values (x, y), vertexCount 2231 * must be a multiple of 2. 2232 * @param verts Array of vertices for the mesh 2233 * @param vertOffset Number of values in the verts to skip before drawing. 2234 * @param texs May be null. If not null, specifies the coordinates to sample into the current 2235 * shader (e.g. bitmap tile or gradient) 2236 * @param texOffset Number of values in texs to skip before drawing. 2237 * @param colors May be null. If not null, specifies a color for each vertex, to be interpolated 2238 * across the triangle. 2239 * @param colorOffset Number of values in colors to skip before drawing. 2240 * @param indices If not null, array of indices to reference into the vertex (texs, colors) 2241 * array. 2242 * @param indexCount number of entries in the indices array (if not null). 2243 * @param paint Specifies the shader to use if the texs array is non-null. 2244 */ 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)2245 public void drawVertices(@NonNull VertexMode mode, int vertexCount, @NonNull float[] verts, 2246 int vertOffset, @Nullable float[] texs, int texOffset, @Nullable int[] colors, 2247 int colorOffset, @Nullable short[] indices, int indexOffset, int indexCount, 2248 @NonNull Paint paint) { 2249 super.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, 2250 colors, colorOffset, indices, indexOffset, indexCount, paint); 2251 } 2252 2253 /** 2254 * Draws the given RenderNode. This is only supported in hardware rendering, which can be 2255 * verified by asserting that {@link #isHardwareAccelerated()} is true. If 2256 * {@link #isHardwareAccelerated()} is false then this throws an exception. 2257 * 2258 * See {@link RenderNode} for more information on what a RenderNode is and how to use it. 2259 * 2260 * @param renderNode The RenderNode to draw, must be non-null. 2261 */ drawRenderNode(@onNull RenderNode renderNode)2262 public void drawRenderNode(@NonNull RenderNode renderNode) { 2263 throw new IllegalArgumentException("Software rendering doesn't support drawRenderNode"); 2264 } 2265 } 2266