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