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