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