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