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