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