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