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