Canvas.java revision 24609581330bc350f797179e3c1a59789c645ec2
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 = initCanvas(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 native_drawRGB(mNativeCanvasWrapper, 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 native_drawARGB(mNativeCanvasWrapper, 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); 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 native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), src, dst, 1306 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1307 } 1308 1309 /** 1310 * Draw the specified bitmap, scaling/translating automatically to fill 1311 * the destination rectangle. If the source rectangle is not null, it 1312 * specifies the subset of the bitmap to draw. 1313 * 1314 * <p>Note: if the paint contains a maskfilter that generates a mask which 1315 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1316 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1317 * Thus the color outside of the original width/height will be the edge 1318 * color replicated. 1319 * 1320 * <p>This function <em>ignores the density associated with the bitmap</em>. 1321 * This is because the source and destination rectangle coordinate 1322 * spaces are in their respective densities, so must already have the 1323 * appropriate scaling factor applied. 1324 * 1325 * @param bitmap The bitmap to be drawn 1326 * @param src May be null. The subset of the bitmap to be drawn 1327 * @param dst The rectangle that the bitmap will be scaled/translated 1328 * to fit into 1329 * @param paint May be null. The paint used to draw the bitmap 1330 */ 1331 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, 1332 @Nullable Paint paint) { 1333 if (dst == null) { 1334 throw new NullPointerException(); 1335 } 1336 throwIfCannotDraw(bitmap); 1337 native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), src, dst, 1338 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1339 } 1340 1341 /** 1342 * Treat the specified array of colors as a bitmap, and draw it. This gives 1343 * the same result as first creating a bitmap from the array, and then 1344 * drawing it, but this method avoids explicitly creating a bitmap object 1345 * which can be more efficient if the colors are changing often. 1346 * 1347 * @param colors Array of colors representing the pixels of the bitmap 1348 * @param offset Offset into the array of colors for the first pixel 1349 * @param stride The number of colors in the array between rows (must be 1350 * >= width or <= -width). 1351 * @param x The X coordinate for where to draw the bitmap 1352 * @param y The Y coordinate for where to draw the bitmap 1353 * @param width The width of the bitmap 1354 * @param height The height of the bitmap 1355 * @param hasAlpha True if the alpha channel of the colors contains valid 1356 * values. If false, the alpha byte is ignored (assumed to 1357 * be 0xFF for every pixel). 1358 * @param paint May be null. The paint used to draw the bitmap 1359 * 1360 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1361 * requires an internal copy of color buffer contents every time this method is called. Using a 1362 * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime 1363 * and copies of pixel data. 1364 */ 1365 @Deprecated 1366 public void drawBitmap(@NonNull int[] colors, int offset, int stride, float x, float y, 1367 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1368 // check for valid input 1369 if (width < 0) { 1370 throw new IllegalArgumentException("width must be >= 0"); 1371 } 1372 if (height < 0) { 1373 throw new IllegalArgumentException("height must be >= 0"); 1374 } 1375 if (Math.abs(stride) < width) { 1376 throw new IllegalArgumentException("abs(stride) must be >= width"); 1377 } 1378 int lastScanline = offset + (height - 1) * stride; 1379 int length = colors.length; 1380 if (offset < 0 || (offset + width > length) || lastScanline < 0 1381 || (lastScanline + width > length)) { 1382 throw new ArrayIndexOutOfBoundsException(); 1383 } 1384 // quick escape if there's nothing to draw 1385 if (width == 0 || height == 0) { 1386 return; 1387 } 1388 // punch down to native for the actual draw 1389 native_drawBitmap(mNativeCanvasWrapper, colors, offset, stride, x, y, width, height, hasAlpha, 1390 paint != null ? paint.mNativePaint : 0); 1391 } 1392 1393 /** 1394 * Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1395 * 1396 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1397 * requires an internal copy of color buffer contents every time this method is called. Using a 1398 * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime 1399 * and copies of pixel data. 1400 */ 1401 @Deprecated 1402 public void drawBitmap(@NonNull int[] colors, int offset, int stride, int x, int y, 1403 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1404 // call through to the common float version 1405 drawBitmap(colors, offset, stride, (float)x, (float)y, width, height, 1406 hasAlpha, paint); 1407 } 1408 1409 /** 1410 * Draw the bitmap using the specified matrix. 1411 * 1412 * @param bitmap The bitmap to draw 1413 * @param matrix The matrix used to transform the bitmap when it is drawn 1414 * @param paint May be null. The paint used to draw the bitmap 1415 */ 1416 public void drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint) { 1417 nativeDrawBitmapMatrix(mNativeCanvasWrapper, bitmap.ni(), matrix.ni(), 1418 paint != null ? paint.mNativePaint : 0); 1419 } 1420 1421 /** 1422 * @hide 1423 */ 1424 protected static void checkRange(int length, int offset, int count) { 1425 if ((offset | count) < 0 || offset + count > length) { 1426 throw new ArrayIndexOutOfBoundsException(); 1427 } 1428 } 1429 1430 /** 1431 * Draw the bitmap through the mesh, where mesh vertices are evenly 1432 * distributed across the bitmap. There are meshWidth+1 vertices across, and 1433 * meshHeight+1 vertices down. The verts array is accessed in row-major 1434 * order, so that the first meshWidth+1 vertices are distributed across the 1435 * top of the bitmap from left to right. A more general version of this 1436 * method is drawVertices(). 1437 * 1438 * @param bitmap The bitmap to draw using the mesh 1439 * @param meshWidth The number of columns in the mesh. Nothing is drawn if 1440 * this is 0 1441 * @param meshHeight The number of rows in the mesh. Nothing is drawn if 1442 * this is 0 1443 * @param verts Array of x,y pairs, specifying where the mesh should be 1444 * drawn. There must be at least 1445 * (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values 1446 * in the array 1447 * @param vertOffset Number of verts elements to skip before drawing 1448 * @param colors May be null. Specifies a color at each vertex, which is 1449 * interpolated across the cell, and whose values are 1450 * multiplied by the corresponding bitmap colors. If not null, 1451 * there must be at least (meshWidth+1) * (meshHeight+1) + 1452 * colorOffset values in the array. 1453 * @param colorOffset Number of color elements to skip before drawing 1454 * @param paint May be null. The paint used to draw the bitmap 1455 */ 1456 public void drawBitmapMesh(@NonNull Bitmap bitmap, int meshWidth, int meshHeight, 1457 @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, 1458 @Nullable Paint paint) { 1459 if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) { 1460 throw new ArrayIndexOutOfBoundsException(); 1461 } 1462 if (meshWidth == 0 || meshHeight == 0) { 1463 return; 1464 } 1465 int count = (meshWidth + 1) * (meshHeight + 1); 1466 // we mul by 2 since we need two floats per vertex 1467 checkRange(verts.length, vertOffset, count * 2); 1468 if (colors != null) { 1469 // no mul by 2, since we need only 1 color per vertex 1470 checkRange(colors.length, colorOffset, count); 1471 } 1472 nativeDrawBitmapMesh(mNativeCanvasWrapper, bitmap.ni(), meshWidth, meshHeight, 1473 verts, vertOffset, colors, colorOffset, 1474 paint != null ? paint.mNativePaint : 0); 1475 } 1476 1477 public enum VertexMode { 1478 TRIANGLES(0), 1479 TRIANGLE_STRIP(1), 1480 TRIANGLE_FAN(2); 1481 1482 VertexMode(int nativeInt) { 1483 this.nativeInt = nativeInt; 1484 } 1485 1486 /** 1487 * @hide 1488 */ 1489 public final int nativeInt; 1490 } 1491 1492 /** 1493 * Draw the array of vertices, interpreted as triangles (based on mode). The 1494 * verts array is required, and specifies the x,y pairs for each vertex. If 1495 * texs is non-null, then it is used to specify the coordinate in shader 1496 * coordinates to use at each vertex (the paint must have a shader in this 1497 * case). If there is no texs array, but there is a color array, then each 1498 * color is interpolated across its corresponding triangle in a gradient. If 1499 * both texs and colors arrays are present, then they behave as before, but 1500 * the resulting color at each pixels is the result of multiplying the 1501 * colors from the shader and the color-gradient together. The indices array 1502 * is optional, but if it is present, then it is used to specify the index 1503 * of each triangle, rather than just walking through the arrays in order. 1504 * 1505 * @param mode How to interpret the array of vertices 1506 * @param vertexCount The number of values in the vertices array (and 1507 * corresponding texs and colors arrays if non-null). Each logical 1508 * vertex is two values (x, y), vertexCount must be a multiple of 2. 1509 * @param verts Array of vertices for the mesh 1510 * @param vertOffset Number of values in the verts to skip before drawing. 1511 * @param texs May be null. If not null, specifies the coordinates to sample 1512 * into the current shader (e.g. bitmap tile or gradient) 1513 * @param texOffset Number of values in texs to skip before drawing. 1514 * @param colors May be null. If not null, specifies a color for each 1515 * vertex, to be interpolated across the triangle. 1516 * @param colorOffset Number of values in colors to skip before drawing. 1517 * @param indices If not null, array of indices to reference into the 1518 * vertex (texs, colors) array. 1519 * @param indexCount number of entries in the indices array (if not null). 1520 * @param paint Specifies the shader to use if the texs array is non-null. 1521 */ 1522 public void drawVertices(@NonNull VertexMode mode, int vertexCount, @NonNull float[] verts, 1523 int vertOffset, @Nullable float[] texs, int texOffset, @Nullable int[] colors, 1524 int colorOffset, @Nullable short[] indices, int indexOffset, int indexCount, 1525 @NonNull Paint paint) { 1526 checkRange(verts.length, vertOffset, vertexCount); 1527 if (texs != null) { 1528 checkRange(texs.length, texOffset, vertexCount); 1529 } 1530 if (colors != null) { 1531 checkRange(colors.length, colorOffset, vertexCount / 2); 1532 } 1533 if (indices != null) { 1534 checkRange(indices.length, indexOffset, indexCount); 1535 } 1536 nativeDrawVertices(mNativeCanvasWrapper, mode.nativeInt, vertexCount, verts, 1537 vertOffset, texs, texOffset, colors, colorOffset, 1538 indices, indexOffset, indexCount, paint.mNativePaint); 1539 } 1540 1541 /** 1542 * Draw the text, with origin at (x,y), using the specified paint. The 1543 * origin is interpreted based on the Align setting in the paint. 1544 * 1545 * @param text The text to be drawn 1546 * @param x The x-coordinate of the origin of the text being drawn 1547 * @param y The y-coordinate of the origin of the text being drawn 1548 * @param paint The paint used for the text (e.g. color, size, style) 1549 */ 1550 public void drawText(@NonNull char[] text, int index, int count, float x, float y, 1551 @NonNull Paint paint) { 1552 if ((index | count | (index + count) | 1553 (text.length - index - count)) < 0) { 1554 throw new IndexOutOfBoundsException(); 1555 } 1556 native_drawText(mNativeCanvasWrapper, text, index, count, x, y, paint.mBidiFlags, 1557 paint.mNativePaint, paint.mNativeTypeface); 1558 } 1559 1560 /** 1561 * Draw the text, with origin at (x,y), using the specified paint. The 1562 * origin is interpreted based on the Align setting in the paint. 1563 * 1564 * @param text The text to be drawn 1565 * @param x The x-coordinate of the origin of the text being drawn 1566 * @param y The y-coordinate of the origin of the text being drawn 1567 * @param paint The paint used for the text (e.g. color, size, style) 1568 */ 1569 public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { 1570 native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags, 1571 paint.mNativePaint, paint.mNativeTypeface); 1572 } 1573 1574 /** 1575 * Draw the text, with origin at (x,y), using the specified paint. 1576 * The origin is interpreted based on the Align setting in the paint. 1577 * 1578 * @param text The text to be drawn 1579 * @param start The index of the first character in text to draw 1580 * @param end (end - 1) is the index of the last character in text to draw 1581 * @param x The x-coordinate of the origin of the text being drawn 1582 * @param y The y-coordinate of the origin of the text being drawn 1583 * @param paint The paint used for the text (e.g. color, size, style) 1584 */ 1585 public void drawText(@NonNull String text, int start, int end, float x, float y, 1586 @NonNull Paint paint) { 1587 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1588 throw new IndexOutOfBoundsException(); 1589 } 1590 native_drawText(mNativeCanvasWrapper, text, start, end, x, y, paint.mBidiFlags, 1591 paint.mNativePaint, paint.mNativeTypeface); 1592 } 1593 1594 /** 1595 * Draw the specified range of text, specified by start/end, with its 1596 * origin at (x,y), in the specified Paint. The origin is interpreted 1597 * based on the Align setting in the Paint. 1598 * 1599 * @param text The text to be drawn 1600 * @param start The index of the first character in text to draw 1601 * @param end (end - 1) is the index of the last character in text 1602 * to draw 1603 * @param x The x-coordinate of origin for where to draw the text 1604 * @param y The y-coordinate of origin for where to draw the text 1605 * @param paint The paint used for the text (e.g. color, size, style) 1606 */ 1607 public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, 1608 @NonNull Paint paint) { 1609 if (text instanceof String || text instanceof SpannedString || 1610 text instanceof SpannableString) { 1611 native_drawText(mNativeCanvasWrapper, text.toString(), start, end, x, y, 1612 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 1613 } else if (text instanceof GraphicsOperations) { 1614 ((GraphicsOperations) text).drawText(this, start, end, x, y, 1615 paint); 1616 } else { 1617 char[] buf = TemporaryBuffer.obtain(end - start); 1618 TextUtils.getChars(text, start, end, buf, 0); 1619 native_drawText(mNativeCanvasWrapper, buf, 0, end - start, x, y, 1620 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 1621 TemporaryBuffer.recycle(buf); 1622 } 1623 } 1624 1625 /** 1626 * Render a run of all LTR or all RTL text, with shaping. This does not run 1627 * bidi on the provided text, but renders it as a uniform right-to-left or 1628 * left-to-right run, as indicated by dir. Alignment of the text is as 1629 * determined by the Paint's TextAlign value. 1630 * 1631 * @param text the text to render 1632 * @param index the start of the text to render 1633 * @param count the count of chars to render 1634 * @param contextIndex the start of the context for shaping. Must be 1635 * no greater than index. 1636 * @param contextCount the number of characters in the context for shaping. 1637 * ContexIndex + contextCount must be no less than index 1638 * + count. 1639 * @param x the x position at which to draw the text 1640 * @param y the y position at which to draw the text 1641 * @param isRtl whether the run is in RTL direction 1642 * @param paint the paint 1643 * @hide 1644 */ 1645 public void drawTextRun(@NonNull char[] text, int index, int count, int contextIndex, 1646 int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint) { 1647 1648 if (text == null) { 1649 throw new NullPointerException("text is null"); 1650 } 1651 if (paint == null) { 1652 throw new NullPointerException("paint is null"); 1653 } 1654 if ((index | count | text.length - index - count) < 0) { 1655 throw new IndexOutOfBoundsException(); 1656 } 1657 1658 native_drawTextRun(mNativeCanvasWrapper, text, index, count, 1659 contextIndex, contextCount, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface); 1660 } 1661 1662 /** 1663 * Render a run of all LTR or all RTL text, with shaping. This does not run 1664 * bidi on the provided text, but renders it as a uniform right-to-left or 1665 * left-to-right run, as indicated by dir. Alignment of the text is as 1666 * determined by the Paint's TextAlign value. 1667 * 1668 * @param text the text to render 1669 * @param start the start of the text to render. Data before this position 1670 * can be used for shaping context. 1671 * @param end the end of the text to render. Data at or after this 1672 * position can be used for shaping context. 1673 * @param x the x position at which to draw the text 1674 * @param y the y position at which to draw the text 1675 * @param isRtl whether the run is in RTL direction 1676 * @param paint the paint 1677 * @hide 1678 */ 1679 public void drawTextRun(@NonNull CharSequence text, int start, int end, int contextStart, 1680 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 1681 1682 if (text == null) { 1683 throw new NullPointerException("text is null"); 1684 } 1685 if (paint == null) { 1686 throw new NullPointerException("paint is null"); 1687 } 1688 if ((start | end | end - start | text.length() - end) < 0) { 1689 throw new IndexOutOfBoundsException(); 1690 } 1691 1692 if (text instanceof String || text instanceof SpannedString || 1693 text instanceof SpannableString) { 1694 native_drawTextRun(mNativeCanvasWrapper, text.toString(), start, end, 1695 contextStart, contextEnd, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface); 1696 } else if (text instanceof GraphicsOperations) { 1697 ((GraphicsOperations) text).drawTextRun(this, start, end, 1698 contextStart, contextEnd, x, y, isRtl, paint); 1699 } else { 1700 int contextLen = contextEnd - contextStart; 1701 int len = end - start; 1702 char[] buf = TemporaryBuffer.obtain(contextLen); 1703 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1704 native_drawTextRun(mNativeCanvasWrapper, buf, start - contextStart, len, 1705 0, contextLen, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface); 1706 TemporaryBuffer.recycle(buf); 1707 } 1708 } 1709 1710 /** 1711 * Draw the text in the array, with each character's origin specified by 1712 * the pos array. 1713 * 1714 * This method does not support glyph composition and decomposition and 1715 * should therefore not be used to render complex scripts. 1716 * 1717 * @param text The text to be drawn 1718 * @param index The index of the first character to draw 1719 * @param count The number of characters to draw, starting from index. 1720 * @param pos Array of [x,y] positions, used to position each 1721 * character 1722 * @param paint The paint used for the text (e.g. color, size, style) 1723 */ 1724 @Deprecated 1725 public void drawPosText(@NonNull char[] text, int index, int count, @NonNull float[] pos, 1726 @NonNull Paint paint) { 1727 if (index < 0 || index + count > text.length || count*2 > pos.length) { 1728 throw new IndexOutOfBoundsException(); 1729 } 1730 native_drawPosText(mNativeCanvasWrapper, text, index, count, pos, 1731 paint.mNativePaint); 1732 } 1733 1734 /** 1735 * Draw the text in the array, with each character's origin specified by 1736 * the pos array. 1737 * 1738 * This method does not support glyph composition and decomposition and 1739 * should therefore not be used to render complex scripts. 1740 * 1741 * @param text The text to be drawn 1742 * @param pos Array of [x,y] positions, used to position each character 1743 * @param paint The paint used for the text (e.g. color, size, style) 1744 */ 1745 @Deprecated 1746 public void drawPosText(@NonNull String text, @NonNull float[] pos, @NonNull Paint paint) { 1747 if (text.length()*2 > pos.length) { 1748 throw new ArrayIndexOutOfBoundsException(); 1749 } 1750 native_drawPosText(mNativeCanvasWrapper, text, pos, paint.mNativePaint); 1751 } 1752 1753 /** 1754 * Draw the text, with origin at (x,y), using the specified paint, along 1755 * the specified path. The paint's Align setting determins where along the 1756 * path to start the text. 1757 * 1758 * @param text The text to be drawn 1759 * @param path The path the text should follow for its baseline 1760 * @param hOffset The distance along the path to add to the text's 1761 * starting position 1762 * @param vOffset The distance above(-) or below(+) the path to position 1763 * the text 1764 * @param paint The paint used for the text (e.g. color, size, style) 1765 */ 1766 public void drawTextOnPath(@NonNull char[] text, int index, int count, @NonNull Path path, 1767 float hOffset, float vOffset, @NonNull Paint paint) { 1768 if (index < 0 || index + count > text.length) { 1769 throw new ArrayIndexOutOfBoundsException(); 1770 } 1771 native_drawTextOnPath(mNativeCanvasWrapper, text, index, count, 1772 path.ni(), hOffset, vOffset, 1773 paint.mBidiFlags, paint.mNativePaint); 1774 } 1775 1776 /** 1777 * Draw the text, with origin at (x,y), using the specified paint, along 1778 * the specified path. The paint's Align setting determins where along the 1779 * path to start the text. 1780 * 1781 * @param text The text to be drawn 1782 * @param path The path the text should follow for its baseline 1783 * @param hOffset The distance along the path to add to the text's 1784 * starting position 1785 * @param vOffset The distance above(-) or below(+) the path to position 1786 * the text 1787 * @param paint The paint used for the text (e.g. color, size, style) 1788 */ 1789 public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset, 1790 float vOffset, @NonNull Paint paint) { 1791 if (text.length() > 0) { 1792 native_drawTextOnPath(mNativeCanvasWrapper, text, path.ni(), hOffset, vOffset, 1793 paint.mBidiFlags, paint.mNativePaint); 1794 } 1795 } 1796 1797 /** 1798 * Save the canvas state, draw the picture, and restore the canvas state. 1799 * This differs from picture.draw(canvas), which does not perform any 1800 * save/restore. 1801 * 1802 * <p> 1803 * <strong>Note:</strong> This forces the picture to internally call 1804 * {@link Picture#endRecording} in order to prepare for playback. 1805 * 1806 * @param picture The picture to be drawn 1807 */ 1808 public void drawPicture(@NonNull Picture picture) { 1809 picture.endRecording(); 1810 int restoreCount = save(); 1811 picture.draw(this); 1812 restoreToCount(restoreCount); 1813 } 1814 1815 /** 1816 * Draw the picture, stretched to fit into the dst rectangle. 1817 */ 1818 public void drawPicture(@NonNull Picture picture, @NonNull RectF dst) { 1819 save(); 1820 translate(dst.left, dst.top); 1821 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1822 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1823 } 1824 drawPicture(picture); 1825 restore(); 1826 } 1827 1828 /** 1829 * Draw the picture, stretched to fit into the dst rectangle. 1830 */ 1831 public void drawPicture(@NonNull Picture picture, @NonNull Rect dst) { 1832 save(); 1833 translate(dst.left, dst.top); 1834 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1835 scale((float) dst.width() / picture.getWidth(), 1836 (float) dst.height() / picture.getHeight()); 1837 } 1838 drawPicture(picture); 1839 restore(); 1840 } 1841 1842 /** 1843 * Releases the resources associated with this canvas. 1844 * 1845 * @hide 1846 */ 1847 public void release() { 1848 mFinalizer.dispose(); 1849 } 1850 1851 /** 1852 * Free up as much memory as possible from private caches (e.g. fonts, images) 1853 * 1854 * @hide 1855 */ 1856 public static native void freeCaches(); 1857 1858 /** 1859 * Free up text layout caches 1860 * 1861 * @hide 1862 */ 1863 public static native void freeTextLayoutCaches(); 1864 1865 private static native long initRaster(long nativeBitmapOrZero); 1866 private static native long initCanvas(long canvasHandle); 1867 private static native void native_setBitmap(long canvasHandle, 1868 long bitmapHandle, 1869 boolean copyState); 1870 private static native boolean native_isOpaque(long canvasHandle); 1871 private static native int native_getWidth(long canvasHandle); 1872 private static native int native_getHeight(long canvasHandle); 1873 1874 private static native int native_save(long canvasHandle, int saveFlags); 1875 private static native int native_saveLayer(long nativeCanvas, float l, 1876 float t, float r, float b, 1877 long nativePaint, 1878 int layerFlags); 1879 private static native int native_saveLayerAlpha(long nativeCanvas, float l, 1880 float t, float r, float b, 1881 int alpha, int layerFlags); 1882 private static native void native_restore(long canvasHandle); 1883 private static native void native_restoreToCount(long canvasHandle, 1884 int saveCount); 1885 private static native int native_getSaveCount(long canvasHandle); 1886 1887 private static native void native_translate(long canvasHandle, 1888 float dx, float dy); 1889 private static native void native_scale(long canvasHandle, 1890 float sx, float sy); 1891 private static native void native_rotate(long canvasHandle, float degrees); 1892 private static native void native_skew(long canvasHandle, 1893 float sx, float sy); 1894 private static native void native_concat(long nativeCanvas, 1895 long nativeMatrix); 1896 private static native void native_setMatrix(long nativeCanvas, 1897 long nativeMatrix); 1898 private static native boolean native_clipRect(long nativeCanvas, 1899 float left, float top, 1900 float right, float bottom, 1901 int regionOp); 1902 private static native boolean native_clipPath(long nativeCanvas, 1903 long nativePath, 1904 int regionOp); 1905 private static native boolean native_clipRegion(long nativeCanvas, 1906 long nativeRegion, 1907 int regionOp); 1908 private static native void nativeSetDrawFilter(long nativeCanvas, 1909 long nativeFilter); 1910 private static native boolean native_getClipBounds(long nativeCanvas, 1911 Rect bounds); 1912 private static native void native_getCTM(long nativeCanvas, 1913 long nativeMatrix); 1914 private static native boolean native_quickReject(long nativeCanvas, 1915 long nativePath); 1916 private static native boolean native_quickReject(long nativeCanvas, 1917 float left, float top, 1918 float right, float bottom); 1919 private static native void native_drawRGB(long nativeCanvas, int r, int g, 1920 int b); 1921 private static native void native_drawARGB(long nativeCanvas, int a, int r, 1922 int g, int b); 1923 private static native void native_drawColor(long nativeCanvas, int color); 1924 private static native void native_drawColor(long nativeCanvas, int color, 1925 int mode); 1926 private static native void native_drawPaint(long nativeCanvas, 1927 long nativePaint); 1928 private static native void native_drawPoint(long canvasHandle, float x, float y, 1929 long paintHandle); 1930 private static native void native_drawPoints(long canvasHandle, float[] pts, 1931 int offset, int count, 1932 long paintHandle); 1933 private static native void native_drawLine(long nativeCanvas, float startX, 1934 float startY, float stopX, 1935 float stopY, long nativePaint); 1936 private static native void native_drawLines(long canvasHandle, float[] pts, 1937 int offset, int count, 1938 long paintHandle); 1939 private static native void native_drawRect(long nativeCanvas, float left, 1940 float top, float right, 1941 float bottom, 1942 long nativePaint); 1943 private static native void native_drawOval(long nativeCanvas, float left, float top, 1944 float right, float bottom, long nativePaint); 1945 private static native void native_drawCircle(long nativeCanvas, float cx, 1946 float cy, float radius, 1947 long nativePaint); 1948 private static native void native_drawArc(long nativeCanvas, float left, float top, 1949 float right, float bottom, 1950 float startAngle, float sweep, boolean useCenter, 1951 long nativePaint); 1952 private static native void native_drawRoundRect(long nativeCanvas, 1953 float left, float top, float right, float bottom, 1954 float rx, float ry, long nativePaint); 1955 private static native void native_drawPath(long nativeCanvas, 1956 long nativePath, 1957 long nativePaint); 1958 private native void native_drawBitmap(long nativeCanvas, long nativeBitmap, 1959 float left, float top, 1960 long nativePaintOrZero, 1961 int canvasDensity, 1962 int screenDensity, 1963 int bitmapDensity); 1964 private native void native_drawBitmap(long nativeCanvas, long nativeBitmap, 1965 Rect src, RectF dst, 1966 long nativePaintOrZero, 1967 int screenDensity, 1968 int bitmapDensity); 1969 private static native void native_drawBitmap(long nativeCanvas, 1970 long nativeBitmap, 1971 Rect src, Rect dst, 1972 long nativePaintOrZero, 1973 int screenDensity, 1974 int bitmapDensity); 1975 private static native void native_drawBitmap(long nativeCanvas, int[] colors, 1976 int offset, int stride, float x, 1977 float y, int width, int height, 1978 boolean hasAlpha, 1979 long nativePaintOrZero); 1980 private static native void nativeDrawBitmapMatrix(long nativeCanvas, 1981 long nativeBitmap, 1982 long nativeMatrix, 1983 long nativePaint); 1984 private static native void nativeDrawBitmapMesh(long nativeCanvas, 1985 long nativeBitmap, 1986 int meshWidth, int meshHeight, 1987 float[] verts, int vertOffset, 1988 int[] colors, int colorOffset, 1989 long nativePaint); 1990 private static native void nativeDrawVertices(long nativeCanvas, int mode, int n, 1991 float[] verts, int vertOffset, float[] texs, int texOffset, 1992 int[] colors, int colorOffset, short[] indices, 1993 int indexOffset, int indexCount, long nativePaint); 1994 1995 private static native void native_drawText(long nativeCanvas, char[] text, 1996 int index, int count, float x, 1997 float y, int flags, long nativePaint, 1998 long nativeTypeface); 1999 private static native void native_drawText(long nativeCanvas, String text, 2000 int start, int end, float x, 2001 float y, int flags, long nativePaint, 2002 long nativeTypeface); 2003 2004 private static native void native_drawTextRun(long nativeCanvas, String text, 2005 int start, int end, int contextStart, int contextEnd, 2006 float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 2007 2008 private static native void native_drawTextRun(long nativeCanvas, char[] text, 2009 int start, int count, int contextStart, int contextCount, 2010 float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 2011 2012 private static native void native_drawPosText(long nativeCanvas, 2013 char[] text, int index, 2014 int count, float[] pos, 2015 long nativePaint); 2016 private static native void native_drawPosText(long nativeCanvas, 2017 String text, float[] pos, 2018 long nativePaint); 2019 private static native void native_drawTextOnPath(long nativeCanvas, 2020 char[] text, int index, 2021 int count, long nativePath, 2022 float hOffset, 2023 float vOffset, int bidiFlags, 2024 long nativePaint); 2025 private static native void native_drawTextOnPath(long nativeCanvas, 2026 String text, long nativePath, 2027 float hOffset, 2028 float vOffset, 2029 int flags, long nativePaint); 2030 private static native void finalizer(long nativeCanvas); 2031} 2032