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