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