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