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