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