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