Canvas.java revision 3e7dbf5373732459b74fd698a6912d6115ed2870
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 BW(0), //!< treat edges by just rounding to nearest pixel boundary 687 AA(1); //!< treat edges by rounding-out, since they may be antialiased 688 689 EdgeType(int nativeInt) { 690 this.nativeInt = nativeInt; 691 } 692 693 /** 694 * @hide 695 */ 696 public final int nativeInt; 697 } 698 699 /** 700 * Return true if the specified rectangle, after being transformed by the 701 * current matrix, would lie completely outside of the current clip. Call 702 * this to check if an area you intend to draw into is clipped out (and 703 * therefore you can skip making the draw calls). 704 * 705 * @param rect the rect to compare with the current clip 706 * @param type specifies how to treat the edges (BW or antialiased) 707 * @return true if the rect (transformed by the canvas' matrix) 708 * does not intersect with the canvas' clip 709 */ 710 public boolean quickReject(RectF rect, EdgeType type) { 711 return native_quickReject(mNativeCanvas, rect); 712 } 713 714 /** 715 * Return true if the specified path, after being transformed by the 716 * current matrix, would lie completely outside of the current clip. Call 717 * this to check if an area you intend to draw into is clipped out (and 718 * therefore you can skip making the draw calls). Note: for speed it may 719 * return false even if the path itself might not intersect the clip 720 * (i.e. the bounds of the path intersects, but the path does not). 721 * 722 * @param path The path to compare with the current clip 723 * @param type true if the path should be considered antialiased, 724 * since that means it may 725 * affect a larger area (more pixels) than 726 * non-antialiased. 727 * @return true if the path (transformed by the canvas' matrix) 728 * does not intersect with the canvas' clip 729 */ 730 public boolean quickReject(Path path, EdgeType type) { 731 return native_quickReject(mNativeCanvas, path.ni()); 732 } 733 734 /** 735 * Return true if the specified rectangle, after being transformed by the 736 * current matrix, would lie completely outside of the current clip. Call 737 * this to check if an area you intend to draw into is clipped out (and 738 * therefore you can skip making the draw calls). 739 * 740 * @param left The left side of the rectangle to compare with the 741 * current clip 742 * @param top The top of the rectangle to compare with the current 743 * clip 744 * @param right The right side of the rectangle to compare with the 745 * current clip 746 * @param bottom The bottom of the rectangle to compare with the 747 * current clip 748 * @param type true if the rect should be considered antialiased, 749 * since that means it may affect a larger area (more 750 * pixels) than non-antialiased. 751 * @return true if the rect (transformed by the canvas' matrix) 752 * does not intersect with the canvas' clip 753 */ 754 public boolean quickReject(float left, float top, float right, float bottom, 755 EdgeType type) { 756 return native_quickReject(mNativeCanvas, left, top, right, bottom); 757 } 758 759 /** 760 * Retrieve the clip bounds, returning true if they are non-empty. 761 * 762 * @param bounds Return the clip bounds here. If it is null, ignore it but 763 * still return true if the current clip is non-empty. 764 * @return true if the current clip is non-empty. 765 */ 766 public boolean getClipBounds(Rect bounds) { 767 return native_getClipBounds(mNativeCanvas, bounds); 768 } 769 770 /** 771 * Retrieve the clip bounds. 772 * 773 * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty. 774 */ 775 public final Rect getClipBounds() { 776 Rect r = new Rect(); 777 getClipBounds(r); 778 return r; 779 } 780 781 /** 782 * Fill the entire canvas' bitmap (restricted to the current clip) with the 783 * specified RGB color, using srcover porterduff mode. 784 * 785 * @param r red component (0..255) of the color to draw onto the canvas 786 * @param g green component (0..255) of the color to draw onto the canvas 787 * @param b blue component (0..255) of the color to draw onto the canvas 788 */ 789 public void drawRGB(int r, int g, int b) { 790 native_drawRGB(mNativeCanvas, r, g, b); 791 } 792 793 /** 794 * Fill the entire canvas' bitmap (restricted to the current clip) with the 795 * specified ARGB color, using srcover porterduff mode. 796 * 797 * @param a alpha component (0..255) of the color to draw onto the canvas 798 * @param r red component (0..255) of the color to draw onto the canvas 799 * @param g green component (0..255) of the color to draw onto the canvas 800 * @param b blue component (0..255) of the color to draw onto the canvas 801 */ 802 public void drawARGB(int a, int r, int g, int b) { 803 native_drawARGB(mNativeCanvas, a, r, g, b); 804 } 805 806 /** 807 * Fill the entire canvas' bitmap (restricted to the current clip) with the 808 * specified color, using srcover porterduff mode. 809 * 810 * @param color the color to draw onto the canvas 811 */ 812 public void drawColor(int color) { 813 native_drawColor(mNativeCanvas, color); 814 } 815 816 /** 817 * Fill the entire canvas' bitmap (restricted to the current clip) with the 818 * specified color and porter-duff xfermode. 819 * 820 * @param color the color to draw with 821 * @param mode the porter-duff mode to apply to the color 822 */ 823 public void drawColor(int color, PorterDuff.Mode mode) { 824 native_drawColor(mNativeCanvas, color, mode.nativeInt); 825 } 826 827 /** 828 * Fill the entire canvas' bitmap (restricted to the current clip) with 829 * the specified paint. This is equivalent (but faster) to drawing an 830 * infinitely large rectangle with the specified paint. 831 * 832 * @param paint The paint used to draw onto the canvas 833 */ 834 public void drawPaint(Paint paint) { 835 native_drawPaint(mNativeCanvas, paint.mNativePaint); 836 } 837 838 /** 839 * Draw a series of points. Each point is centered at the coordinate 840 * specified by pts[], and its diameter is specified by the paint's stroke 841 * width (as transformed by the canvas' CTM), with special treatment for 842 * a stroke width of 0, which always draws exactly 1 pixel (or at most 4 843 * if antialiasing is enabled). The shape of the point is controlled by 844 * the paint's Cap type. The shape is a square, unless the cap type is 845 * Round, in which case the shape is a circle. 846 * 847 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 848 * @param offset Number of values to skip before starting to draw. 849 * @param count The number of values to process, after skipping offset 850 * of them. Since one point uses two values, the number of 851 * "points" that are drawn is really (count >> 1). 852 * @param paint The paint used to draw the points 853 */ 854 public native void drawPoints(float[] pts, int offset, int count, Paint paint); 855 856 /** 857 * Helper for drawPoints() that assumes you want to draw the entire array 858 */ 859 public void drawPoints(float[] pts, Paint paint) { 860 drawPoints(pts, 0, pts.length, paint); 861 } 862 863 /** 864 * Helper for drawPoints() for drawing a single point. 865 */ 866 public native void drawPoint(float x, float y, Paint paint); 867 868 /** 869 * Draw a line segment with the specified start and stop x,y coordinates, 870 * using the specified paint. 871 * 872 * <p>Note that since a line is always "framed", the Style is ignored in the paint.</p> 873 * 874 * <p>Degenerate lines (length is 0) will not be drawn.</p> 875 * 876 * @param startX The x-coordinate of the start point of the line 877 * @param startY The y-coordinate of the start point of the line 878 * @param paint The paint used to draw the line 879 */ 880 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 881 native_drawLine(mNativeCanvas, startX, startY, stopX, stopY, paint.mNativePaint); 882 } 883 884 /** 885 * Draw a series of lines. Each line is taken from 4 consecutive values 886 * in the pts array. Thus to draw 1 line, the array must contain at least 4 887 * values. This is logically the same as drawing the array as follows: 888 * drawLine(pts[0], pts[1], pts[2], pts[3]) followed by 889 * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on. 890 * 891 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 892 * @param offset Number of values in the array to skip before drawing. 893 * @param count The number of values in the array to process, after 894 * skipping "offset" of them. Since each line uses 4 values, 895 * the number of "lines" that are drawn is really 896 * (count >> 2). 897 * @param paint The paint used to draw the points 898 */ 899 public native void drawLines(float[] pts, int offset, int count, Paint paint); 900 901 public void drawLines(float[] pts, Paint paint) { 902 drawLines(pts, 0, pts.length, paint); 903 } 904 905 /** 906 * Draw the specified Rect using the specified paint. The rectangle will 907 * be filled or framed based on the Style in the paint. 908 * 909 * @param rect The rect to be drawn 910 * @param paint The paint used to draw the rect 911 */ 912 public void drawRect(RectF rect, Paint paint) { 913 native_drawRect(mNativeCanvas, rect, paint.mNativePaint); 914 } 915 916 /** 917 * Draw the specified Rect using the specified Paint. The rectangle 918 * will be filled or framed based on the Style in the paint. 919 * 920 * @param r The rectangle to be drawn. 921 * @param paint The paint used to draw the rectangle 922 */ 923 public void drawRect(Rect r, Paint paint) { 924 drawRect(r.left, r.top, r.right, r.bottom, paint); 925 } 926 927 928 /** 929 * Draw the specified Rect using the specified paint. The rectangle will 930 * be filled or framed based on the Style in the paint. 931 * 932 * @param left The left side of the rectangle to be drawn 933 * @param top The top side of the rectangle to be drawn 934 * @param right The right side of the rectangle to be drawn 935 * @param bottom The bottom side of the rectangle to be drawn 936 * @param paint The paint used to draw the rect 937 */ 938 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 939 native_drawRect(mNativeCanvas, left, top, right, bottom, paint.mNativePaint); 940 } 941 942 /** 943 * Draw the specified oval using the specified paint. The oval will be 944 * filled or framed based on the Style in the paint. 945 * 946 * @param oval The rectangle bounds of the oval to be drawn 947 */ 948 public void drawOval(RectF oval, Paint paint) { 949 if (oval == null) { 950 throw new NullPointerException(); 951 } 952 native_drawOval(mNativeCanvas, oval, paint.mNativePaint); 953 } 954 955 /** 956 * Draw the specified circle using the specified paint. If radius is <= 0, 957 * then nothing will be drawn. The circle will be filled or framed based 958 * on the Style in the paint. 959 * 960 * @param cx The x-coordinate of the center of the cirle to be drawn 961 * @param cy The y-coordinate of the center of the cirle to be drawn 962 * @param radius The radius of the cirle to be drawn 963 * @param paint The paint used to draw the circle 964 */ 965 public void drawCircle(float cx, float cy, float radius, Paint paint) { 966 native_drawCircle(mNativeCanvas, cx, cy, radius, paint.mNativePaint); 967 } 968 969 /** 970 * <p>Draw the specified arc, which will be scaled to fit inside the 971 * specified oval.</p> 972 * 973 * <p>If the start angle is negative or >= 360, the start angle is treated 974 * as start angle modulo 360.</p> 975 * 976 * <p>If the sweep angle is >= 360, then the oval is drawn 977 * completely. Note that this differs slightly from SkPath::arcTo, which 978 * treats the sweep angle modulo 360. If the sweep angle is negative, 979 * the sweep angle is treated as sweep angle modulo 360</p> 980 * 981 * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the 982 * geometric angle of 0 degrees (3 o'clock on a watch.)</p> 983 * 984 * @param oval The bounds of oval used to define the shape and size 985 * of the arc 986 * @param startAngle Starting angle (in degrees) where the arc begins 987 * @param sweepAngle Sweep angle (in degrees) measured clockwise 988 * @param useCenter If true, include the center of the oval in the arc, and 989 close it if it is being stroked. This will draw a wedge 990 * @param paint The paint used to draw the arc 991 */ 992 public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 993 Paint paint) { 994 if (oval == null) { 995 throw new NullPointerException(); 996 } 997 native_drawArc(mNativeCanvas, oval, startAngle, sweepAngle, 998 useCenter, paint.mNativePaint); 999 } 1000 1001 /** 1002 * Draw the specified round-rect using the specified paint. The roundrect 1003 * will be filled or framed based on the Style in the paint. 1004 * 1005 * @param rect The rectangular bounds of the roundRect to be drawn 1006 * @param rx The x-radius of the oval used to round the corners 1007 * @param ry The y-radius of the oval used to round the corners 1008 * @param paint The paint used to draw the roundRect 1009 */ 1010 public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { 1011 if (rect == null) { 1012 throw new NullPointerException(); 1013 } 1014 native_drawRoundRect(mNativeCanvas, rect, rx, ry, 1015 paint.mNativePaint); 1016 } 1017 1018 /** 1019 * Draw the specified path using the specified paint. The path will be 1020 * filled or framed based on the Style in the paint. 1021 * 1022 * @param path The path to be drawn 1023 * @param paint The paint used to draw the path 1024 */ 1025 public void drawPath(Path path, Paint paint) { 1026 native_drawPath(mNativeCanvas, path.ni(), paint.mNativePaint); 1027 } 1028 1029 private static void throwIfRecycled(Bitmap bitmap) { 1030 if (bitmap.isRecycled()) { 1031 throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap); 1032 } 1033 } 1034 1035 /** 1036 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1037 * 1038 * Note: Only supported by hardware accelerated canvas at the moment. 1039 * 1040 * @param bitmap The bitmap to draw as an N-patch 1041 * @param chunks The patches information (matches the native struct Res_png_9patch) 1042 * @param dst The destination rectangle. 1043 * @param paint The paint to draw the bitmap with. may be null 1044 * 1045 * @hide 1046 */ 1047 public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) { 1048 } 1049 1050 /** 1051 * Draw the specified bitmap, with its top/left corner at (x,y), using 1052 * the specified paint, transformed by the current matrix. 1053 * 1054 * <p>Note: if the paint contains a maskfilter that generates a mask which 1055 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1056 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1057 * Thus the color outside of the original width/height will be the edge 1058 * color replicated. 1059 * 1060 * <p>If the bitmap and canvas have different densities, this function 1061 * will take care of automatically scaling the bitmap to draw at the 1062 * same density as the canvas. 1063 * 1064 * @param bitmap The bitmap to be drawn 1065 * @param left The position of the left side of the bitmap being drawn 1066 * @param top The position of the top side of the bitmap being drawn 1067 * @param paint The paint used to draw the bitmap (may be null) 1068 */ 1069 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 1070 throwIfRecycled(bitmap); 1071 native_drawBitmap(mNativeCanvas, bitmap.ni(), left, top, 1072 paint != null ? paint.mNativePaint : 0, mDensity, mScreenDensity, bitmap.mDensity); 1073 } 1074 1075 /** 1076 * Draw the specified bitmap, scaling/translating automatically to fill 1077 * the destination rectangle. If the source rectangle is not null, it 1078 * specifies the subset of the bitmap to draw. 1079 * 1080 * <p>Note: if the paint contains a maskfilter that generates a mask which 1081 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1082 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1083 * Thus the color outside of the original width/height will be the edge 1084 * color replicated. 1085 * 1086 * <p>This function <em>ignores the density associated with the bitmap</em>. 1087 * This is because the source and destination rectangle coordinate 1088 * spaces are in their respective densities, so must already have the 1089 * appropriate scaling factor applied. 1090 * 1091 * @param bitmap The bitmap to be drawn 1092 * @param src May be null. The subset of the bitmap to be drawn 1093 * @param dst The rectangle that the bitmap will be scaled/translated 1094 * to fit into 1095 * @param paint May be null. The paint used to draw the bitmap 1096 */ 1097 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 1098 if (dst == null) { 1099 throw new NullPointerException(); 1100 } 1101 throwIfRecycled(bitmap); 1102 native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst, 1103 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1104 } 1105 1106 /** 1107 * Draw the specified bitmap, scaling/translating automatically to fill 1108 * the destination rectangle. If the source rectangle is not null, it 1109 * specifies the subset of the bitmap to draw. 1110 * 1111 * <p>Note: if the paint contains a maskfilter that generates a mask which 1112 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1113 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1114 * Thus the color outside of the original width/height will be the edge 1115 * color replicated. 1116 * 1117 * <p>This function <em>ignores the density associated with the bitmap</em>. 1118 * This is because the source and destination rectangle coordinate 1119 * spaces are in their respective densities, so must already have the 1120 * appropriate scaling factor applied. 1121 * 1122 * @param bitmap The bitmap to be drawn 1123 * @param src May be null. The subset of the bitmap to be drawn 1124 * @param dst The rectangle that the bitmap will be scaled/translated 1125 * to fit into 1126 * @param paint May be null. The paint used to draw the bitmap 1127 */ 1128 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 1129 if (dst == null) { 1130 throw new NullPointerException(); 1131 } 1132 throwIfRecycled(bitmap); 1133 native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst, 1134 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1135 } 1136 1137 /** 1138 * Treat the specified array of colors as a bitmap, and draw it. This gives 1139 * the same result as first creating a bitmap from the array, and then 1140 * drawing it, but this method avoids explicitly creating a bitmap object 1141 * which can be more efficient if the colors are changing often. 1142 * 1143 * @param colors Array of colors representing the pixels of the bitmap 1144 * @param offset Offset into the array of colors for the first pixel 1145 * @param stride The number of colors in the array between rows (must be 1146 * >= width or <= -width). 1147 * @param x The X coordinate for where to draw the bitmap 1148 * @param y The Y coordinate for where to draw the bitmap 1149 * @param width The width of the bitmap 1150 * @param height The height of the bitmap 1151 * @param hasAlpha True if the alpha channel of the colors contains valid 1152 * values. If false, the alpha byte is ignored (assumed to 1153 * be 0xFF for every pixel). 1154 * @param paint May be null. The paint used to draw the bitmap 1155 */ 1156 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 1157 int width, int height, boolean hasAlpha, Paint paint) { 1158 // check for valid input 1159 if (width < 0) { 1160 throw new IllegalArgumentException("width must be >= 0"); 1161 } 1162 if (height < 0) { 1163 throw new IllegalArgumentException("height must be >= 0"); 1164 } 1165 if (Math.abs(stride) < width) { 1166 throw new IllegalArgumentException("abs(stride) must be >= width"); 1167 } 1168 int lastScanline = offset + (height - 1) * stride; 1169 int length = colors.length; 1170 if (offset < 0 || (offset + width > length) || lastScanline < 0 1171 || (lastScanline + width > length)) { 1172 throw new ArrayIndexOutOfBoundsException(); 1173 } 1174 // quick escape if there's nothing to draw 1175 if (width == 0 || height == 0) { 1176 return; 1177 } 1178 // punch down to native for the actual draw 1179 native_drawBitmap(mNativeCanvas, colors, offset, stride, x, y, width, height, hasAlpha, 1180 paint != null ? paint.mNativePaint : 0); 1181 } 1182 1183 /** Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1184 */ 1185 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 1186 int width, int height, boolean hasAlpha, Paint paint) { 1187 // call through to the common float version 1188 drawBitmap(colors, offset, stride, (float)x, (float)y, width, height, 1189 hasAlpha, paint); 1190 } 1191 1192 /** 1193 * Draw the bitmap using the specified matrix. 1194 * 1195 * @param bitmap The bitmap to draw 1196 * @param matrix The matrix used to transform the bitmap when it is drawn 1197 * @param paint May be null. The paint used to draw the bitmap 1198 */ 1199 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 1200 nativeDrawBitmapMatrix(mNativeCanvas, bitmap.ni(), matrix.ni(), 1201 paint != null ? paint.mNativePaint : 0); 1202 } 1203 1204 /** 1205 * @hide 1206 */ 1207 protected static void checkRange(int length, int offset, int count) { 1208 if ((offset | count) < 0 || offset + count > length) { 1209 throw new ArrayIndexOutOfBoundsException(); 1210 } 1211 } 1212 1213 /** 1214 * Draw the bitmap through the mesh, where mesh vertices are evenly 1215 * distributed across the bitmap. There are meshWidth+1 vertices across, and 1216 * meshHeight+1 vertices down. The verts array is accessed in row-major 1217 * order, so that the first meshWidth+1 vertices are distributed across the 1218 * top of the bitmap from left to right. A more general version of this 1219 * method is drawVertices(). 1220 * 1221 * @param bitmap The bitmap to draw using the mesh 1222 * @param meshWidth The number of columns in the mesh. Nothing is drawn if 1223 * this is 0 1224 * @param meshHeight The number of rows in the mesh. Nothing is drawn if 1225 * this is 0 1226 * @param verts Array of x,y pairs, specifying where the mesh should be 1227 * drawn. There must be at least 1228 * (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values 1229 * in the array 1230 * @param vertOffset Number of verts elements to skip before drawing 1231 * @param colors May be null. Specifies a color at each vertex, which is 1232 * interpolated across the cell, and whose values are 1233 * multiplied by the corresponding bitmap colors. If not null, 1234 * there must be at least (meshWidth+1) * (meshHeight+1) + 1235 * colorOffset values in the array. 1236 * @param colorOffset Number of color elements to skip before drawing 1237 * @param paint May be null. The paint used to draw the bitmap 1238 */ 1239 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, 1240 float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) { 1241 if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) { 1242 throw new ArrayIndexOutOfBoundsException(); 1243 } 1244 if (meshWidth == 0 || meshHeight == 0) { 1245 return; 1246 } 1247 int count = (meshWidth + 1) * (meshHeight + 1); 1248 // we mul by 2 since we need two floats per vertex 1249 checkRange(verts.length, vertOffset, count * 2); 1250 if (colors != null) { 1251 // no mul by 2, since we need only 1 color per vertex 1252 checkRange(colors.length, colorOffset, count); 1253 } 1254 nativeDrawBitmapMesh(mNativeCanvas, bitmap.ni(), meshWidth, meshHeight, 1255 verts, vertOffset, colors, colorOffset, 1256 paint != null ? paint.mNativePaint : 0); 1257 } 1258 1259 public enum VertexMode { 1260 TRIANGLES(0), 1261 TRIANGLE_STRIP(1), 1262 TRIANGLE_FAN(2); 1263 1264 VertexMode(int nativeInt) { 1265 this.nativeInt = nativeInt; 1266 } 1267 1268 /** 1269 * @hide 1270 */ 1271 public final int nativeInt; 1272 } 1273 1274 /** 1275 * Draw the array of vertices, interpreted as triangles (based on mode). The 1276 * verts array is required, and specifies the x,y pairs for each vertex. If 1277 * texs is non-null, then it is used to specify the coordinate in shader 1278 * coordinates to use at each vertex (the paint must have a shader in this 1279 * case). If there is no texs array, but there is a color array, then each 1280 * color is interpolated across its corresponding triangle in a gradient. If 1281 * both texs and colors arrays are present, then they behave as before, but 1282 * the resulting color at each pixels is the result of multiplying the 1283 * colors from the shader and the color-gradient together. The indices array 1284 * is optional, but if it is present, then it is used to specify the index 1285 * of each triangle, rather than just walking through the arrays in order. 1286 * 1287 * @param mode How to interpret the array of vertices 1288 * @param vertexCount The number of values in the vertices array (and 1289 * corresponding texs and colors arrays if non-null). Each logical 1290 * vertex is two values (x, y), vertexCount must be a multiple of 2. 1291 * @param verts Array of vertices for the mesh 1292 * @param vertOffset Number of values in the verts to skip before drawing. 1293 * @param texs May be null. If not null, specifies the coordinates to sample 1294 * into the current shader (e.g. bitmap tile or gradient) 1295 * @param texOffset Number of values in texs to skip before drawing. 1296 * @param colors May be null. If not null, specifies a color for each 1297 * vertex, to be interpolated across the triangle. 1298 * @param colorOffset Number of values in colors to skip before drawing. 1299 * @param indices If not null, array of indices to reference into the 1300 * vertex (texs, colors) array. 1301 * @param indexCount number of entries in the indices array (if not null). 1302 * @param paint Specifies the shader to use if the texs array is non-null. 1303 */ 1304 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 1305 float[] texs, int texOffset, int[] colors, int colorOffset, 1306 short[] indices, int indexOffset, int indexCount, Paint paint) { 1307 checkRange(verts.length, vertOffset, vertexCount); 1308 if (texs != null) { 1309 checkRange(texs.length, texOffset, vertexCount); 1310 } 1311 if (colors != null) { 1312 checkRange(colors.length, colorOffset, vertexCount / 2); 1313 } 1314 if (indices != null) { 1315 checkRange(indices.length, indexOffset, indexCount); 1316 } 1317 nativeDrawVertices(mNativeCanvas, mode.nativeInt, vertexCount, verts, 1318 vertOffset, texs, texOffset, colors, colorOffset, 1319 indices, indexOffset, indexCount, paint.mNativePaint); 1320 } 1321 1322 /** 1323 * Draw the text, with origin at (x,y), using the specified paint. The 1324 * origin is interpreted based on the Align setting in the paint. 1325 * 1326 * @param text The text to be drawn 1327 * @param x The x-coordinate of the origin of the text being drawn 1328 * @param y The y-coordinate of the origin of the text being drawn 1329 * @param paint The paint used for the text (e.g. color, size, style) 1330 */ 1331 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 1332 if ((index | count | (index + count) | 1333 (text.length - index - count)) < 0) { 1334 throw new IndexOutOfBoundsException(); 1335 } 1336 native_drawText(mNativeCanvas, text, index, count, x, y, paint.mNativePaint); 1337 } 1338 1339 /** 1340 * Draw the text, with origin at (x,y), using the specified paint. The 1341 * origin is interpreted based on the Align setting in the paint. 1342 * 1343 * @param text The text to be drawn 1344 * @param x The x-coordinate of the origin of the text being drawn 1345 * @param y The y-coordinate of the origin of the text being drawn 1346 * @param paint The paint used for the text (e.g. color, size, style) 1347 */ 1348 public void drawText(String text, float x, float y, Paint paint) { 1349 native_drawText(mNativeCanvas, text, 0, text.length(), x, y, paint.mNativePaint); 1350 } 1351 1352 /** 1353 * Draw the text, with origin at (x,y), using the specified paint. 1354 * The origin is interpreted based on the Align setting in the paint. 1355 * 1356 * @param text The text to be drawn 1357 * @param start The index of the first character in text to draw 1358 * @param end (end - 1) is the index of the last character in text to draw 1359 * @param x The x-coordinate of the origin of the text being drawn 1360 * @param y The y-coordinate of the origin of the text being drawn 1361 * @param paint The paint used for the text (e.g. color, size, style) 1362 */ 1363 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 1364 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1365 throw new IndexOutOfBoundsException(); 1366 } 1367 native_drawText(mNativeCanvas, text, start, end, x, y, paint.mNativePaint); 1368 } 1369 1370 /** 1371 * Draw the specified range of text, specified by start/end, with its 1372 * origin at (x,y), in the specified Paint. The origin is interpreted 1373 * based on the Align setting in the Paint. 1374 * 1375 * @param text The text to be drawn 1376 * @param start The index of the first character in text to draw 1377 * @param end (end - 1) is the index of the last character in text 1378 * to draw 1379 * @param x The x-coordinate of origin for where to draw the text 1380 * @param y The y-coordinate of origin for where to draw the text 1381 * @param paint The paint used for the text (e.g. color, size, style) 1382 */ 1383 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 1384 if (text instanceof String || text instanceof SpannedString || 1385 text instanceof SpannableString) { 1386 native_drawText(mNativeCanvas, text.toString(), start, end, x, y, paint.mNativePaint); 1387 } else if (text instanceof GraphicsOperations) { 1388 ((GraphicsOperations) text).drawText(this, start, end, x, y, 1389 paint); 1390 } else { 1391 char[] buf = TemporaryBuffer.obtain(end - start); 1392 TextUtils.getChars(text, start, end, buf, 0); 1393 native_drawText(mNativeCanvas, buf, 0, end - start, x, y, paint.mNativePaint); 1394 TemporaryBuffer.recycle(buf); 1395 } 1396 } 1397 1398 /** 1399 * Render a run of all LTR or all RTL text, with shaping. This does not run 1400 * bidi on the provided text, but renders it as a uniform right-to-left or 1401 * left-to-right run, as indicated by dir. Alignment of the text is as 1402 * determined by the Paint's TextAlign value. 1403 * 1404 * @param text the text to render 1405 * @param index the start of the text to render 1406 * @param count the count of chars to render 1407 * @param contextIndex the start of the context for shaping. Must be 1408 * no greater than index. 1409 * @param contextCount the number of characters in the context for shaping. 1410 * ContexIndex + contextCount must be no less than index 1411 * + count. 1412 * @param x the x position at which to draw the text 1413 * @param y the y position at which to draw the text 1414 * @param paint the paint 1415 * @hide 1416 */ 1417 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 1418 float x, float y, Paint paint) { 1419 1420 if (text == null) { 1421 throw new NullPointerException("text is null"); 1422 } 1423 if (paint == null) { 1424 throw new NullPointerException("paint is null"); 1425 } 1426 if ((index | count | text.length - index - count) < 0) { 1427 throw new IndexOutOfBoundsException(); 1428 } 1429 1430 native_drawTextRun(mNativeCanvas, text, index, count, 1431 contextIndex, contextCount, x, y, paint.mNativePaint); 1432 } 1433 1434 /** 1435 * Render a run of all LTR or all RTL text, with shaping. This does not run 1436 * bidi on the provided text, but renders it as a uniform right-to-left or 1437 * left-to-right run, as indicated by dir. Alignment of the text is as 1438 * determined by the Paint's TextAlign value. 1439 * 1440 * @param text the text to render 1441 * @param start the start of the text to render. Data before this position 1442 * can be used for shaping context. 1443 * @param end the end of the text to render. Data at or after this 1444 * position can be used for shaping context. 1445 * @param x the x position at which to draw the text 1446 * @param y the y position at which to draw the text 1447 * @param paint the paint 1448 * @hide 1449 */ 1450 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 1451 float x, float y, Paint paint) { 1452 1453 if (text == null) { 1454 throw new NullPointerException("text is null"); 1455 } 1456 if (paint == null) { 1457 throw new NullPointerException("paint is null"); 1458 } 1459 if ((start | end | end - start | text.length() - end) < 0) { 1460 throw new IndexOutOfBoundsException(); 1461 } 1462 1463 if (text instanceof String || text instanceof SpannedString || 1464 text instanceof SpannableString) { 1465 native_drawTextRun(mNativeCanvas, text.toString(), start, end, 1466 contextStart, contextEnd, x, y, paint.mNativePaint); 1467 } else if (text instanceof GraphicsOperations) { 1468 ((GraphicsOperations) text).drawTextRun(this, start, end, 1469 contextStart, contextEnd, x, y, paint); 1470 } else { 1471 int contextLen = contextEnd - contextStart; 1472 int len = end - start; 1473 char[] buf = TemporaryBuffer.obtain(contextLen); 1474 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1475 native_drawTextRun(mNativeCanvas, buf, start - contextStart, len, 1476 0, contextLen, x, y, paint.mNativePaint); 1477 TemporaryBuffer.recycle(buf); 1478 } 1479 } 1480 1481 /** 1482 * Draw the text in the array, with each character's origin specified by 1483 * the pos array. 1484 * 1485 * This method does not support glyph composition and decomposition and 1486 * should therefore not be used to render complex scripts. 1487 * 1488 * @param text The text to be drawn 1489 * @param index The index of the first character to draw 1490 * @param count The number of characters to draw, starting from index. 1491 * @param pos Array of [x,y] positions, used to position each 1492 * character 1493 * @param paint The paint used for the text (e.g. color, size, style) 1494 */ 1495 @Deprecated 1496 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 1497 if (index < 0 || index + count > text.length || count*2 > pos.length) { 1498 throw new IndexOutOfBoundsException(); 1499 } 1500 native_drawPosText(mNativeCanvas, text, index, count, pos, 1501 paint.mNativePaint); 1502 } 1503 1504 /** 1505 * Draw the text in the array, with each character's origin specified by 1506 * the pos array. 1507 * 1508 * This method does not support glyph composition and decomposition and 1509 * should therefore not be used to render complex scripts. 1510 * 1511 * @param text The text to be drawn 1512 * @param pos Array of [x,y] positions, used to position each character 1513 * @param paint The paint used for the text (e.g. color, size, style) 1514 */ 1515 @Deprecated 1516 public void drawPosText(String text, float[] pos, Paint paint) { 1517 if (text.length()*2 > pos.length) { 1518 throw new ArrayIndexOutOfBoundsException(); 1519 } 1520 native_drawPosText(mNativeCanvas, text, pos, paint.mNativePaint); 1521 } 1522 1523 /** 1524 * Draw the text, with origin at (x,y), using the specified paint, along 1525 * the specified path. The paint's Align setting determins where along the 1526 * path to start the text. 1527 * 1528 * @param text The text to be drawn 1529 * @param path The path the text should follow for its baseline 1530 * @param hOffset The distance along the path to add to the text's 1531 * starting position 1532 * @param vOffset The distance above(-) or below(+) the path to position 1533 * the text 1534 * @param paint The paint used for the text (e.g. color, size, style) 1535 */ 1536 public void drawTextOnPath(char[] text, int index, int count, Path path, 1537 float hOffset, float vOffset, Paint paint) { 1538 if (index < 0 || index + count > text.length) { 1539 throw new ArrayIndexOutOfBoundsException(); 1540 } 1541 native_drawTextOnPath(mNativeCanvas, text, index, count, 1542 path.ni(), hOffset, vOffset, paint.mNativePaint); 1543 } 1544 1545 /** 1546 * Draw the text, with origin at (x,y), using the specified paint, along 1547 * the specified path. The paint's Align setting determins where along the 1548 * path to start the text. 1549 * 1550 * @param text The text to be drawn 1551 * @param path The path the text should follow for its baseline 1552 * @param hOffset The distance along the path to add to the text's 1553 * starting position 1554 * @param vOffset The distance above(-) or below(+) the path to position 1555 * the text 1556 * @param paint The paint used for the text (e.g. color, size, style) 1557 */ 1558 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 1559 if (text.length() > 0) { 1560 native_drawTextOnPath(mNativeCanvas, text, path.ni(), hOffset, vOffset, 1561 paint.mNativePaint); 1562 } 1563 } 1564 1565 /** 1566 * Save the canvas state, draw the picture, and restore the canvas state. 1567 * This differs from picture.draw(canvas), which does not perform any 1568 * save/restore. 1569 * 1570 * <p> 1571 * <strong>Note:</strong> This forces the picture to internally call 1572 * {@link Picture#endRecording} in order to prepare for playback. 1573 * 1574 * @param picture The picture to be drawn 1575 */ 1576 public void drawPicture(Picture picture) { 1577 picture.endRecording(); 1578 native_drawPicture(mNativeCanvas, picture.ni()); 1579 } 1580 1581 /** 1582 * Draw the picture, stretched to fit into the dst rectangle. 1583 */ 1584 public void drawPicture(Picture picture, RectF dst) { 1585 save(); 1586 translate(dst.left, dst.top); 1587 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1588 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1589 } 1590 drawPicture(picture); 1591 restore(); 1592 } 1593 1594 /** 1595 * Draw the picture, stretched to fit into the dst rectangle. 1596 */ 1597 public void drawPicture(Picture picture, Rect dst) { 1598 save(); 1599 translate(dst.left, dst.top); 1600 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1601 scale((float) dst.width() / picture.getWidth(), 1602 (float) dst.height() / picture.getHeight()); 1603 } 1604 drawPicture(picture); 1605 restore(); 1606 } 1607 1608 /** 1609 * Free up as much memory as possible from private caches (e.g. fonts, images) 1610 * 1611 * @hide 1612 */ 1613 public static native void freeCaches(); 1614 1615 /** 1616 * Free up text layout caches 1617 * 1618 * @hide 1619 */ 1620 public static native void freeTextLayoutCaches(); 1621 1622 private static native int initRaster(int nativeBitmapOrZero); 1623 private static native void copyNativeCanvasState(int srcCanvas, int dstCanvas); 1624 private static native int native_saveLayer(int nativeCanvas, RectF bounds, 1625 int paint, int layerFlags); 1626 private static native int native_saveLayer(int nativeCanvas, float l, 1627 float t, float r, float b, 1628 int paint, int layerFlags); 1629 private static native int native_saveLayerAlpha(int nativeCanvas, 1630 RectF bounds, int alpha, 1631 int layerFlags); 1632 private static native int native_saveLayerAlpha(int nativeCanvas, float l, 1633 float t, float r, float b, 1634 int alpha, int layerFlags); 1635 1636 private static native void native_concat(int nCanvas, int nMatrix); 1637 private static native void native_setMatrix(int nCanvas, int nMatrix); 1638 private static native boolean native_clipRect(int nCanvas, 1639 float left, float top, 1640 float right, float bottom, 1641 int regionOp); 1642 private static native boolean native_clipPath(int nativeCanvas, 1643 int nativePath, 1644 int regionOp); 1645 private static native boolean native_clipRegion(int nativeCanvas, 1646 int nativeRegion, 1647 int regionOp); 1648 private static native void nativeSetDrawFilter(int nativeCanvas, 1649 int nativeFilter); 1650 private static native boolean native_getClipBounds(int nativeCanvas, 1651 Rect bounds); 1652 private static native void native_getCTM(int canvas, int matrix); 1653 private static native boolean native_quickReject(int nativeCanvas, 1654 RectF rect); 1655 private static native boolean native_quickReject(int nativeCanvas, 1656 int path); 1657 private static native boolean native_quickReject(int nativeCanvas, 1658 float left, float top, 1659 float right, float bottom); 1660 private static native void native_drawRGB(int nativeCanvas, int r, int g, 1661 int b); 1662 private static native void native_drawARGB(int nativeCanvas, int a, int r, 1663 int g, int b); 1664 private static native void native_drawColor(int nativeCanvas, int color); 1665 private static native void native_drawColor(int nativeCanvas, int color, 1666 int mode); 1667 private static native void native_drawPaint(int nativeCanvas, int paint); 1668 private static native void native_drawLine(int nativeCanvas, float startX, 1669 float startY, float stopX, 1670 float stopY, int paint); 1671 private static native void native_drawRect(int nativeCanvas, RectF rect, 1672 int paint); 1673 private static native void native_drawRect(int nativeCanvas, float left, 1674 float top, float right, 1675 float bottom, int paint); 1676 private static native void native_drawOval(int nativeCanvas, RectF oval, 1677 int paint); 1678 private static native void native_drawCircle(int nativeCanvas, float cx, 1679 float cy, float radius, 1680 int paint); 1681 private static native void native_drawArc(int nativeCanvas, RectF oval, 1682 float startAngle, float sweep, 1683 boolean useCenter, int paint); 1684 private static native void native_drawRoundRect(int nativeCanvas, 1685 RectF rect, float rx, 1686 float ry, int paint); 1687 private static native void native_drawPath(int nativeCanvas, int path, 1688 int paint); 1689 private native void native_drawBitmap(int nativeCanvas, int bitmap, 1690 float left, float top, 1691 int nativePaintOrZero, 1692 int canvasDensity, 1693 int screenDensity, 1694 int bitmapDensity); 1695 private native void native_drawBitmap(int nativeCanvas, int bitmap, 1696 Rect src, RectF dst, 1697 int nativePaintOrZero, 1698 int screenDensity, 1699 int bitmapDensity); 1700 private static native void native_drawBitmap(int nativeCanvas, int bitmap, 1701 Rect src, Rect dst, 1702 int nativePaintOrZero, 1703 int screenDensity, 1704 int bitmapDensity); 1705 private static native void native_drawBitmap(int nativeCanvas, int[] colors, 1706 int offset, int stride, float x, 1707 float y, int width, int height, 1708 boolean hasAlpha, 1709 int nativePaintOrZero); 1710 private static native void nativeDrawBitmapMatrix(int nCanvas, int nBitmap, 1711 int nMatrix, int nPaint); 1712 private static native void nativeDrawBitmapMesh(int nCanvas, int nBitmap, 1713 int meshWidth, int meshHeight, 1714 float[] verts, int vertOffset, 1715 int[] colors, int colorOffset, int nPaint); 1716 private static native void nativeDrawVertices(int nCanvas, int mode, int n, 1717 float[] verts, int vertOffset, float[] texs, int texOffset, 1718 int[] colors, int colorOffset, short[] indices, 1719 int indexOffset, int indexCount, int nPaint); 1720 1721 private static native void native_drawText(int nativeCanvas, char[] text, 1722 int index, int count, float x, 1723 float y, int paint); 1724 private static native void native_drawText(int nativeCanvas, String text, 1725 int start, int end, float x, 1726 float y, int paint); 1727 1728 private static native void native_drawTextRun(int nativeCanvas, String text, 1729 int start, int end, int contextStart, int contextEnd, 1730 float x, float y, int paint); 1731 1732 private static native void native_drawTextRun(int nativeCanvas, char[] text, 1733 int start, int count, int contextStart, int contextCount, 1734 float x, float y, int paint); 1735 1736 private static native void native_drawPosText(int nativeCanvas, 1737 char[] text, int index, 1738 int count, float[] pos, 1739 int paint); 1740 private static native void native_drawPosText(int nativeCanvas, 1741 String text, float[] pos, 1742 int paint); 1743 private static native void native_drawTextOnPath(int nativeCanvas, 1744 char[] text, int index, 1745 int count, int path, 1746 float hOffset, 1747 float vOffset, 1748 int paint); 1749 private static native void native_drawTextOnPath(int nativeCanvas, 1750 String text, int path, 1751 float hOffset, 1752 float vOffset, 1753 int paint); 1754 private static native void native_drawPicture(int nativeCanvas, 1755 int nativePicture); 1756 private static native void finalizer(int nativeCanvas); 1757} 1758