Canvas.java revision f9d9c065ed75f1196316a9a31f92309f602cef76
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 */ 528 public final Matrix getMatrix() { 529 Matrix m = new Matrix(); 530 //noinspection deprecation 531 getMatrix(m); 532 return m; 533 } 534 535 /** 536 * Modify the current clip with the specified rectangle. 537 * 538 * @param rect The rect to intersect with the current clip 539 * @param op How the clip is modified 540 * @return true if the resulting clip is non-empty 541 */ 542 public boolean clipRect(RectF rect, Region.Op op) { 543 return native_clipRect(mNativeCanvas, rect.left, rect.top, rect.right, rect.bottom, 544 op.nativeInt); 545 } 546 547 /** 548 * Modify the current clip with the specified rectangle, which is 549 * expressed in local coordinates. 550 * 551 * @param rect The rectangle to intersect with the current clip. 552 * @param op How the clip is modified 553 * @return true if the resulting clip is non-empty 554 */ 555 public boolean clipRect(Rect rect, Region.Op op) { 556 return native_clipRect(mNativeCanvas, rect.left, rect.top, rect.right, rect.bottom, 557 op.nativeInt); 558 } 559 560 /** 561 * Intersect the current clip with the specified rectangle, which is 562 * expressed in local coordinates. 563 * 564 * @param rect The rectangle to intersect with the current clip. 565 * @return true if the resulting clip is non-empty 566 */ 567 public native boolean clipRect(RectF rect); 568 569 /** 570 * Intersect the current clip with the specified rectangle, which is 571 * expressed in local coordinates. 572 * 573 * @param rect The rectangle to intersect with the current clip. 574 * @return true if the resulting clip is non-empty 575 */ 576 public native boolean clipRect(Rect rect); 577 578 /** 579 * Modify the current clip with the specified rectangle, which is 580 * expressed in local coordinates. 581 * 582 * @param left The left side of the rectangle to intersect with the 583 * current clip 584 * @param top The top of the rectangle to intersect with the current 585 * clip 586 * @param right The right side of the rectangle to intersect with the 587 * current clip 588 * @param bottom The bottom of the rectangle to intersect with the current 589 * clip 590 * @param op How the clip is modified 591 * @return true if the resulting clip is non-empty 592 */ 593 public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { 594 return native_clipRect(mNativeCanvas, left, top, right, bottom, op.nativeInt); 595 } 596 597 /** 598 * Intersect the current clip with the specified rectangle, which is 599 * expressed in local coordinates. 600 * 601 * @param left The left side of the rectangle to intersect with the 602 * current clip 603 * @param top The top of the rectangle to intersect with the current clip 604 * @param right The right side of the rectangle to intersect with the 605 * current clip 606 * @param bottom The bottom of the rectangle to intersect with the current 607 * clip 608 * @return true if the resulting clip is non-empty 609 */ 610 public native boolean clipRect(float left, float top, float right, float bottom); 611 612 /** 613 * Intersect the current clip with the specified rectangle, which is 614 * expressed in local coordinates. 615 * 616 * @param left The left side of the rectangle to intersect with the 617 * current clip 618 * @param top The top of the rectangle to intersect with the current clip 619 * @param right The right side of the rectangle to intersect with the 620 * current clip 621 * @param bottom The bottom of the rectangle to intersect with the current 622 * clip 623 * @return true if the resulting clip is non-empty 624 */ 625 public native boolean clipRect(int left, int top, int right, int bottom); 626 627 /** 628 * Modify the current clip with the specified path. 629 * 630 * @param path The path to operate on the current clip 631 * @param op How the clip is modified 632 * @return true if the resulting is non-empty 633 */ 634 public boolean clipPath(Path path, Region.Op op) { 635 return native_clipPath(mNativeCanvas, path.ni(), op.nativeInt); 636 } 637 638 /** 639 * Intersect the current clip with the specified path. 640 * 641 * @param path The path to intersect with the current clip 642 * @return true if the resulting is non-empty 643 */ 644 public boolean clipPath(Path path) { 645 return clipPath(path, Region.Op.INTERSECT); 646 } 647 648 /** 649 * Modify the current clip with the specified region. Note that unlike 650 * clipRect() and clipPath() which transform their arguments by the 651 * current matrix, clipRegion() assumes its argument is already in the 652 * coordinate system of the current layer's bitmap, and so not 653 * transformation is performed. 654 * 655 * @param region The region to operate on the current clip, based on op 656 * @param op How the clip is modified 657 * @return true if the resulting is non-empty 658 */ 659 public boolean clipRegion(Region region, Region.Op op) { 660 return native_clipRegion(mNativeCanvas, region.ni(), op.nativeInt); 661 } 662 663 /** 664 * Intersect the current clip with the specified region. Note that unlike 665 * clipRect() and clipPath() which transform their arguments by the 666 * current matrix, clipRegion() assumes its argument is already in the 667 * coordinate system of the current layer's bitmap, and so not 668 * transformation is performed. 669 * 670 * @param region The region to operate on the current clip, based on op 671 * @return true if the resulting is non-empty 672 */ 673 public boolean clipRegion(Region region) { 674 return clipRegion(region, Region.Op.INTERSECT); 675 } 676 677 public DrawFilter getDrawFilter() { 678 return mDrawFilter; 679 } 680 681 public void setDrawFilter(DrawFilter filter) { 682 int nativeFilter = 0; 683 if (filter != null) { 684 nativeFilter = filter.mNativeInt; 685 } 686 mDrawFilter = filter; 687 nativeSetDrawFilter(mNativeCanvas, nativeFilter); 688 } 689 690 public enum EdgeType { 691 BW(0), //!< treat edges by just rounding to nearest pixel boundary 692 AA(1); //!< treat edges by rounding-out, since they may be antialiased 693 694 EdgeType(int nativeInt) { 695 this.nativeInt = nativeInt; 696 } 697 698 /** 699 * @hide 700 */ 701 public final int nativeInt; 702 } 703 704 /** 705 * Return true if the specified rectangle, after being transformed by the 706 * current matrix, would lie completely outside of the current clip. Call 707 * this to check if an area you intend to draw into is clipped out (and 708 * therefore you can skip making the draw calls). 709 * 710 * @param rect the rect to compare with the current clip 711 * @param type specifies how to treat the edges (BW or antialiased) 712 * @return true if the rect (transformed by the canvas' matrix) 713 * does not intersect with the canvas' clip 714 */ 715 public boolean quickReject(RectF rect, EdgeType type) { 716 return native_quickReject(mNativeCanvas, rect, type.nativeInt); 717 } 718 719 /** 720 * Return true if the specified path, after being transformed by the 721 * current matrix, would lie completely outside of the current clip. Call 722 * this to check if an area you intend to draw into is clipped out (and 723 * therefore you can skip making the draw calls). Note: for speed it may 724 * return false even if the path itself might not intersect the clip 725 * (i.e. the bounds of the path intersects, but the path does not). 726 * 727 * @param path The path to compare with the current clip 728 * @param type true if the path should be considered antialiased, 729 * since that means it may 730 * affect a larger area (more pixels) than 731 * non-antialiased. 732 * @return true if the path (transformed by the canvas' matrix) 733 * does not intersect with the canvas' clip 734 */ 735 public boolean quickReject(Path path, EdgeType type) { 736 return native_quickReject(mNativeCanvas, path.ni(), type.nativeInt); 737 } 738 739 /** 740 * Return true if the specified rectangle, after being transformed by the 741 * current matrix, would lie completely outside of the current clip. Call 742 * this to check if an area you intend to draw into is clipped out (and 743 * therefore you can skip making the draw calls). 744 * 745 * @param left The left side of the rectangle to compare with the 746 * current clip 747 * @param top The top of the rectangle to compare with the current 748 * clip 749 * @param right The right side of the rectangle to compare with the 750 * current clip 751 * @param bottom The bottom of the rectangle to compare with the 752 * current clip 753 * @param type true if the rect should be considered antialiased, 754 * since that means it may affect a larger area (more 755 * pixels) than non-antialiased. 756 * @return true if the rect (transformed by the canvas' matrix) 757 * does not intersect with the canvas' clip 758 */ 759 public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { 760 return native_quickReject(mNativeCanvas, left, top, right, bottom, 761 type.nativeInt); 762 } 763 764 /** 765 * Retrieve the clip bounds, returning true if they are non-empty. 766 * 767 * @param bounds Return the clip bounds here. If it is null, ignore it but 768 * still return true if the current clip is non-empty. 769 * @return true if the current clip is non-empty. 770 */ 771 public boolean getClipBounds(Rect bounds) { 772 return native_getClipBounds(mNativeCanvas, bounds); 773 } 774 775 /** 776 * Retrieve the clip bounds. 777 * 778 * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty. 779 */ 780 public final Rect getClipBounds() { 781 Rect r = new Rect(); 782 getClipBounds(r); 783 return r; 784 } 785 786 /** 787 * Fill the entire canvas' bitmap (restricted to the current clip) with the 788 * specified RGB color, using srcover porterduff mode. 789 * 790 * @param r red component (0..255) of the color to draw onto the canvas 791 * @param g green component (0..255) of the color to draw onto the canvas 792 * @param b blue component (0..255) of the color to draw onto the canvas 793 */ 794 public void drawRGB(int r, int g, int b) { 795 native_drawRGB(mNativeCanvas, r, g, b); 796 } 797 798 /** 799 * Fill the entire canvas' bitmap (restricted to the current clip) with the 800 * specified ARGB color, using srcover porterduff mode. 801 * 802 * @param a alpha component (0..255) of the color to draw onto the canvas 803 * @param r red component (0..255) of the color to draw onto the canvas 804 * @param g green component (0..255) of the color to draw onto the canvas 805 * @param b blue component (0..255) of the color to draw onto the canvas 806 */ 807 public void drawARGB(int a, int r, int g, int b) { 808 native_drawARGB(mNativeCanvas, a, r, g, b); 809 } 810 811 /** 812 * Fill the entire canvas' bitmap (restricted to the current clip) with the 813 * specified color, using srcover porterduff mode. 814 * 815 * @param color the color to draw onto the canvas 816 */ 817 public void drawColor(int color) { 818 native_drawColor(mNativeCanvas, color); 819 } 820 821 /** 822 * Fill the entire canvas' bitmap (restricted to the current clip) with the 823 * specified color and porter-duff xfermode. 824 * 825 * @param color the color to draw with 826 * @param mode the porter-duff mode to apply to the color 827 */ 828 public void drawColor(int color, PorterDuff.Mode mode) { 829 native_drawColor(mNativeCanvas, color, mode.nativeInt); 830 } 831 832 /** 833 * Fill the entire canvas' bitmap (restricted to the current clip) with 834 * the specified paint. This is equivalent (but faster) to drawing an 835 * infinitely large rectangle with the specified paint. 836 * 837 * @param paint The paint used to draw onto the canvas 838 */ 839 public void drawPaint(Paint paint) { 840 native_drawPaint(mNativeCanvas, paint.mNativePaint); 841 } 842 843 /** 844 * Draw a series of points. Each point is centered at the coordinate 845 * specified by pts[], and its diameter is specified by the paint's stroke 846 * width (as transformed by the canvas' CTM), with special treatment for 847 * a stroke width of 0, which always draws exactly 1 pixel (or at most 4 848 * if antialiasing is enabled). The shape of the point is controlled by 849 * the paint's Cap type. The shape is a square, unless the cap type is 850 * Round, in which case the shape is a circle. 851 * 852 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 853 * @param offset Number of values to skip before starting to draw. 854 * @param count The number of values to process, after skipping offset 855 * of them. Since one point uses two values, the number of 856 * "points" that are drawn is really (count >> 1). 857 * @param paint The paint used to draw the points 858 */ 859 public native void drawPoints(float[] pts, int offset, int count, Paint paint); 860 861 /** 862 * Helper for drawPoints() that assumes you want to draw the entire array 863 */ 864 public void drawPoints(float[] pts, Paint paint) { 865 drawPoints(pts, 0, pts.length, paint); 866 } 867 868 /** 869 * Helper for drawPoints() for drawing a single point. 870 */ 871 public native void drawPoint(float x, float y, Paint paint); 872 873 /** 874 * Draw a line segment with the specified start and stop x,y coordinates, 875 * using the specified paint. NOTE: since a line is always "framed", the 876 * Style is ignored in the paint. 877 * 878 * @param startX The x-coordinate of the start point of the line 879 * @param startY The y-coordinate of the start point of the line 880 * @param paint The paint used to draw the line 881 */ 882 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 883 native_drawLine(mNativeCanvas, startX, startY, stopX, stopY, paint.mNativePaint); 884 } 885 886 /** 887 * Draw a series of lines. Each line is taken from 4 consecutive values 888 * in the pts array. Thus to draw 1 line, the array must contain at least 4 889 * values. This is logically the same as drawing the array as follows: 890 * drawLine(pts[0], pts[1], pts[2], pts[3]) followed by 891 * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on. 892 * 893 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 894 * @param offset Number of values in the array to skip before drawing. 895 * @param count The number of values in the array to process, after 896 * skipping "offset" of them. Since each line uses 4 values, 897 * the number of "lines" that are drawn is really 898 * (count >> 2). 899 * @param paint The paint used to draw the points 900 */ 901 public native void drawLines(float[] pts, int offset, int count, Paint paint); 902 903 public void drawLines(float[] pts, Paint paint) { 904 drawLines(pts, 0, pts.length, paint); 905 } 906 907 /** 908 * Draw the specified Rect using the specified paint. The rectangle will 909 * be filled or framed based on the Style in the paint. 910 * 911 * @param rect The rect to be drawn 912 * @param paint The paint used to draw the rect 913 */ 914 public void drawRect(RectF rect, Paint paint) { 915 native_drawRect(mNativeCanvas, rect, paint.mNativePaint); 916 } 917 918 /** 919 * Draw the specified Rect using the specified Paint. The rectangle 920 * will be filled or framed based on the Style in the paint. 921 * 922 * @param r The rectangle to be drawn. 923 * @param paint The paint used to draw the rectangle 924 */ 925 public void drawRect(Rect r, Paint paint) { 926 drawRect(r.left, r.top, r.right, r.bottom, paint); 927 } 928 929 930 /** 931 * Draw the specified Rect using the specified paint. The rectangle will 932 * be filled or framed based on the Style in the paint. 933 * 934 * @param left The left side of the rectangle to be drawn 935 * @param top The top side of the rectangle to be drawn 936 * @param right The right side of the rectangle to be drawn 937 * @param bottom The bottom side of the rectangle to be drawn 938 * @param paint The paint used to draw the rect 939 */ 940 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 941 native_drawRect(mNativeCanvas, left, top, right, bottom, paint.mNativePaint); 942 } 943 944 /** 945 * Draw the specified oval using the specified paint. The oval will be 946 * filled or framed based on the Style in the paint. 947 * 948 * @param oval The rectangle bounds of the oval to be drawn 949 */ 950 public void drawOval(RectF oval, Paint paint) { 951 if (oval == null) { 952 throw new NullPointerException(); 953 } 954 native_drawOval(mNativeCanvas, oval, paint.mNativePaint); 955 } 956 957 /** 958 * Draw the specified circle using the specified paint. If radius is <= 0, 959 * then nothing will be drawn. The circle will be filled or framed based 960 * on the Style in the paint. 961 * 962 * @param cx The x-coordinate of the center of the cirle to be drawn 963 * @param cy The y-coordinate of the center of the cirle to be drawn 964 * @param radius The radius of the cirle to be drawn 965 * @param paint The paint used to draw the circle 966 */ 967 public void drawCircle(float cx, float cy, float radius, Paint paint) { 968 native_drawCircle(mNativeCanvas, cx, cy, radius, paint.mNativePaint); 969 } 970 971 /** 972 * <p>Draw the specified arc, which will be scaled to fit inside the 973 * specified oval.</p> 974 * 975 * <p>If the start angle is negative or >= 360, the start angle is treated 976 * as start angle modulo 360.</p> 977 * 978 * <p>If the sweep angle is >= 360, then the oval is drawn 979 * completely. Note that this differs slightly from SkPath::arcTo, which 980 * treats the sweep angle modulo 360. If the sweep angle is negative, 981 * the sweep angle is treated as sweep angle modulo 360</p> 982 * 983 * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the 984 * geometric angle of 0 degrees (3 o'clock on a watch.)</p> 985 * 986 * @param oval The bounds of oval used to define the shape and size 987 * of the arc 988 * @param startAngle Starting angle (in degrees) where the arc begins 989 * @param sweepAngle Sweep angle (in degrees) measured clockwise 990 * @param useCenter If true, include the center of the oval in the arc, and 991 close it if it is being stroked. This will draw a wedge 992 * @param paint The paint used to draw the arc 993 */ 994 public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 995 Paint paint) { 996 if (oval == null) { 997 throw new NullPointerException(); 998 } 999 native_drawArc(mNativeCanvas, oval, startAngle, sweepAngle, 1000 useCenter, paint.mNativePaint); 1001 } 1002 1003 /** 1004 * Draw the specified round-rect using the specified paint. The roundrect 1005 * will be filled or framed based on the Style in the paint. 1006 * 1007 * @param rect The rectangular bounds of the roundRect to be drawn 1008 * @param rx The x-radius of the oval used to round the corners 1009 * @param ry The y-radius of the oval used to round the corners 1010 * @param paint The paint used to draw the roundRect 1011 */ 1012 public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { 1013 if (rect == null) { 1014 throw new NullPointerException(); 1015 } 1016 native_drawRoundRect(mNativeCanvas, rect, rx, ry, 1017 paint.mNativePaint); 1018 } 1019 1020 /** 1021 * Draw the specified path using the specified paint. The path will be 1022 * filled or framed based on the Style in the paint. 1023 * 1024 * @param path The path to be drawn 1025 * @param paint The paint used to draw the path 1026 */ 1027 public void drawPath(Path path, Paint paint) { 1028 native_drawPath(mNativeCanvas, path.ni(), paint.mNativePaint); 1029 } 1030 1031 private static void throwIfRecycled(Bitmap bitmap) { 1032 if (bitmap.isRecycled()) { 1033 throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap); 1034 } 1035 } 1036 1037 /** 1038 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1039 * 1040 * Note: Only supported by hardware accelerated canvas at the moment. 1041 * 1042 * @param bitmap The bitmap to draw as an N-patch 1043 * @param chunks The patches information (matches the native struct Res_png_9patch) 1044 * @param dst The destination rectangle. 1045 * @param paint The paint to draw the bitmap with. may be null 1046 * 1047 * @hide 1048 */ 1049 public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) { 1050 } 1051 1052 /** 1053 * Draw the specified bitmap, with its top/left corner at (x,y), using 1054 * the specified paint, transformed by the current matrix. 1055 * 1056 * <p>Note: if the paint contains a maskfilter that generates a mask which 1057 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1058 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1059 * Thus the color outside of the original width/height will be the edge 1060 * color replicated. 1061 * 1062 * <p>If the bitmap and canvas have different densities, this function 1063 * will take care of automatically scaling the bitmap to draw at the 1064 * same density as the canvas. 1065 * 1066 * @param bitmap The bitmap to be drawn 1067 * @param left The position of the left side of the bitmap being drawn 1068 * @param top The position of the top side of the bitmap being drawn 1069 * @param paint The paint used to draw the bitmap (may be null) 1070 */ 1071 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 1072 throwIfRecycled(bitmap); 1073 native_drawBitmap(mNativeCanvas, bitmap.ni(), left, top, 1074 paint != null ? paint.mNativePaint : 0, mDensity, mScreenDensity, bitmap.mDensity); 1075 } 1076 1077 /** 1078 * Draw the specified bitmap, scaling/translating automatically to fill 1079 * the destination rectangle. If the source rectangle is not null, it 1080 * specifies the subset of the bitmap to draw. 1081 * 1082 * <p>Note: if the paint contains a maskfilter that generates a mask which 1083 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1084 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1085 * Thus the color outside of the original width/height will be the edge 1086 * color replicated. 1087 * 1088 * <p>This function <em>ignores the density associated with the bitmap</em>. 1089 * This is because the source and destination rectangle coordinate 1090 * spaces are in their respective densities, so must already have the 1091 * appropriate scaling factor applied. 1092 * 1093 * @param bitmap The bitmap to be drawn 1094 * @param src May be null. The subset of the bitmap to be drawn 1095 * @param dst The rectangle that the bitmap will be scaled/translated 1096 * to fit into 1097 * @param paint May be null. The paint used to draw the bitmap 1098 */ 1099 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 1100 if (dst == null) { 1101 throw new NullPointerException(); 1102 } 1103 throwIfRecycled(bitmap); 1104 native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst, 1105 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1106 } 1107 1108 /** 1109 * Draw the specified bitmap, scaling/translating automatically to fill 1110 * the destination rectangle. If the source rectangle is not null, it 1111 * specifies the subset of the bitmap to draw. 1112 * 1113 * <p>Note: if the paint contains a maskfilter that generates a mask which 1114 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1115 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1116 * Thus the color outside of the original width/height will be the edge 1117 * color replicated. 1118 * 1119 * <p>This function <em>ignores the density associated with the bitmap</em>. 1120 * This is because the source and destination rectangle coordinate 1121 * spaces are in their respective densities, so must already have the 1122 * appropriate scaling factor applied. 1123 * 1124 * @param bitmap The bitmap to be drawn 1125 * @param src May be null. The subset of the bitmap to be drawn 1126 * @param dst The rectangle that the bitmap will be scaled/translated 1127 * to fit into 1128 * @param paint May be null. The paint used to draw the bitmap 1129 */ 1130 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 1131 if (dst == null) { 1132 throw new NullPointerException(); 1133 } 1134 throwIfRecycled(bitmap); 1135 native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst, 1136 paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity); 1137 } 1138 1139 /** 1140 * Treat the specified array of colors as a bitmap, and draw it. This gives 1141 * the same result as first creating a bitmap from the array, and then 1142 * drawing it, but this method avoids explicitly creating a bitmap object 1143 * which can be more efficient if the colors are changing often. 1144 * 1145 * @param colors Array of colors representing the pixels of the bitmap 1146 * @param offset Offset into the array of colors for the first pixel 1147 * @param stride The number of colors in the array between rows (must be 1148 * >= width or <= -width). 1149 * @param x The X coordinate for where to draw the bitmap 1150 * @param y The Y coordinate for where to draw the bitmap 1151 * @param width The width of the bitmap 1152 * @param height The height of the bitmap 1153 * @param hasAlpha True if the alpha channel of the colors contains valid 1154 * values. If false, the alpha byte is ignored (assumed to 1155 * be 0xFF for every pixel). 1156 * @param paint May be null. The paint used to draw the bitmap 1157 */ 1158 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 1159 int width, int height, boolean hasAlpha, Paint paint) { 1160 // check for valid input 1161 if (width < 0) { 1162 throw new IllegalArgumentException("width must be >= 0"); 1163 } 1164 if (height < 0) { 1165 throw new IllegalArgumentException("height must be >= 0"); 1166 } 1167 if (Math.abs(stride) < width) { 1168 throw new IllegalArgumentException("abs(stride) must be >= width"); 1169 } 1170 int lastScanline = offset + (height - 1) * stride; 1171 int length = colors.length; 1172 if (offset < 0 || (offset + width > length) || lastScanline < 0 1173 || (lastScanline + width > length)) { 1174 throw new ArrayIndexOutOfBoundsException(); 1175 } 1176 // quick escape if there's nothing to draw 1177 if (width == 0 || height == 0) { 1178 return; 1179 } 1180 // punch down to native for the actual draw 1181 native_drawBitmap(mNativeCanvas, colors, offset, stride, x, y, width, height, hasAlpha, 1182 paint != null ? paint.mNativePaint : 0); 1183 } 1184 1185 /** Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1186 */ 1187 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 1188 int width, int height, boolean hasAlpha, Paint paint) { 1189 // call through to the common float version 1190 drawBitmap(colors, offset, stride, (float)x, (float)y, width, height, 1191 hasAlpha, paint); 1192 } 1193 1194 /** 1195 * Draw the bitmap using the specified matrix. 1196 * 1197 * @param bitmap The bitmap to draw 1198 * @param matrix The matrix used to transform the bitmap when it is drawn 1199 * @param paint May be null. The paint used to draw the bitmap 1200 */ 1201 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 1202 nativeDrawBitmapMatrix(mNativeCanvas, bitmap.ni(), matrix.ni(), 1203 paint != null ? paint.mNativePaint : 0); 1204 } 1205 1206 /** 1207 * @hide 1208 */ 1209 protected static void checkRange(int length, int offset, int count) { 1210 if ((offset | count) < 0 || offset + count > length) { 1211 throw new ArrayIndexOutOfBoundsException(); 1212 } 1213 } 1214 1215 /** 1216 * Draw the bitmap through the mesh, where mesh vertices are evenly 1217 * distributed across the bitmap. There are meshWidth+1 vertices across, and 1218 * meshHeight+1 vertices down. The verts array is accessed in row-major 1219 * order, so that the first meshWidth+1 vertices are distributed across the 1220 * top of the bitmap from left to right. A more general version of this 1221 * methid is drawVertices(). 1222 * 1223 * @param bitmap The bitmap to draw using the mesh 1224 * @param meshWidth The number of columns in the mesh. Nothing is drawn if 1225 * this is 0 1226 * @param meshHeight The number of rows in the mesh. Nothing is drawn if 1227 * this is 0 1228 * @param verts Array of x,y pairs, specifying where the mesh should be 1229 * drawn. There must be at least 1230 * (meshWidth+1) * (meshHeight+1) * 2 + meshOffset values 1231 * in the array 1232 * @param vertOffset Number of verts elements to skip before drawing 1233 * @param colors May be null. Specifies a color at each vertex, which is 1234 * interpolated across the cell, and whose values are 1235 * multiplied by the corresponding bitmap colors. If not null, 1236 * there must be at least (meshWidth+1) * (meshHeight+1) + 1237 * colorOffset values in the array. 1238 * @param colorOffset Number of color elements to skip before drawing 1239 * @param paint May be null. The paint used to draw the bitmap 1240 */ 1241 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, 1242 float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) { 1243 if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) { 1244 throw new ArrayIndexOutOfBoundsException(); 1245 } 1246 if (meshWidth == 0 || meshHeight == 0) { 1247 return; 1248 } 1249 int count = (meshWidth + 1) * (meshHeight + 1); 1250 // we mul by 2 since we need two floats per vertex 1251 checkRange(verts.length, vertOffset, count * 2); 1252 if (colors != null) { 1253 // no mul by 2, since we need only 1 color per vertex 1254 checkRange(colors.length, colorOffset, count); 1255 } 1256 nativeDrawBitmapMesh(mNativeCanvas, bitmap.ni(), meshWidth, meshHeight, 1257 verts, vertOffset, colors, colorOffset, 1258 paint != null ? paint.mNativePaint : 0); 1259 } 1260 1261 public enum VertexMode { 1262 TRIANGLES(0), 1263 TRIANGLE_STRIP(1), 1264 TRIANGLE_FAN(2); 1265 1266 VertexMode(int nativeInt) { 1267 this.nativeInt = nativeInt; 1268 } 1269 1270 /** 1271 * @hide 1272 */ 1273 public final int nativeInt; 1274 } 1275 1276 /** 1277 * Draw the array of vertices, interpreted as triangles (based on mode). The 1278 * verts array is required, and specifies the x,y pairs for each vertex. If 1279 * texs is non-null, then it is used to specify the coordinate in shader 1280 * coordinates to use at each vertex (the paint must have a shader in this 1281 * case). If there is no texs array, but there is a color array, then each 1282 * color is interpolated across its corresponding triangle in a gradient. If 1283 * both texs and colors arrays are present, then they behave as before, but 1284 * the resulting color at each pixels is the result of multiplying the 1285 * colors from the shader and the color-gradient together. The indices array 1286 * is optional, but if it is present, then it is used to specify the index 1287 * of each triangle, rather than just walking through the arrays in order. 1288 * 1289 * @param mode How to interpret the array of vertices 1290 * @param vertexCount The number of values in the vertices array (and 1291 * corresponding texs and colors arrays if non-null). Each logical 1292 * vertex is two values (x, y), vertexCount must be a multiple of 2. 1293 * @param verts Array of vertices for the mesh 1294 * @param vertOffset Number of values in the verts to skip before drawing. 1295 * @param texs May be null. If not null, specifies the coordinates to sample 1296 * into the current shader (e.g. bitmap tile or gradient) 1297 * @param texOffset Number of values in texs to skip before drawing. 1298 * @param colors May be null. If not null, specifies a color for each 1299 * vertex, to be interpolated across the triangle. 1300 * @param colorOffset Number of values in colors to skip before drawing. 1301 * @param indices If not null, array of indices to reference into the 1302 * vertex (texs, colors) array. 1303 * @param indexCount number of entries in the indices array (if not null). 1304 * @param paint Specifies the shader to use if the texs array is non-null. 1305 */ 1306 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 1307 float[] texs, int texOffset, int[] colors, int colorOffset, 1308 short[] indices, int indexOffset, int indexCount, Paint paint) { 1309 checkRange(verts.length, vertOffset, vertexCount); 1310 if (texs != null) { 1311 checkRange(texs.length, texOffset, vertexCount); 1312 } 1313 if (colors != null) { 1314 checkRange(colors.length, colorOffset, vertexCount / 2); 1315 } 1316 if (indices != null) { 1317 checkRange(indices.length, indexOffset, indexCount); 1318 } 1319 nativeDrawVertices(mNativeCanvas, mode.nativeInt, vertexCount, verts, 1320 vertOffset, texs, texOffset, colors, colorOffset, 1321 indices, indexOffset, indexCount, paint.mNativePaint); 1322 } 1323 1324 /** 1325 * Draw the text, with origin at (x,y), using the specified paint. The 1326 * origin is interpreted based on the Align setting in the paint. 1327 * 1328 * @param text The text to be drawn 1329 * @param x The x-coordinate of the origin of the text being drawn 1330 * @param y The y-coordinate of the origin of the text being drawn 1331 * @param paint The paint used for the text (e.g. color, size, style) 1332 */ 1333 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 1334 if ((index | count | (index + count) | 1335 (text.length - index - count)) < 0) { 1336 throw new IndexOutOfBoundsException(); 1337 } 1338 native_drawText(mNativeCanvas, text, index, count, x, y, paint.mBidiFlags, 1339 paint.mNativePaint); 1340 } 1341 1342 /** 1343 * Draw the text, with origin at (x,y), using the specified paint. The 1344 * origin is interpreted based on the Align setting in the paint. 1345 * 1346 * @param text The text to be drawn 1347 * @param x The x-coordinate of the origin of the text being drawn 1348 * @param y The y-coordinate of the origin of the text being drawn 1349 * @param paint The paint used for the text (e.g. color, size, style) 1350 */ 1351 public void drawText(String text, float x, float y, Paint paint) { 1352 native_drawText(mNativeCanvas, text, 0, text.length(), x, y, paint.mBidiFlags, 1353 paint.mNativePaint); 1354 } 1355 1356 /** 1357 * Draw the text, with origin at (x,y), using the specified paint. 1358 * The origin is interpreted based on the Align setting in the paint. 1359 * 1360 * @param text The text to be drawn 1361 * @param start The index of the first character in text to draw 1362 * @param end (end - 1) is the index of the last character in text to draw 1363 * @param x The x-coordinate of the origin of the text being drawn 1364 * @param y The y-coordinate of the origin of the text being drawn 1365 * @param paint The paint used for the text (e.g. color, size, style) 1366 */ 1367 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 1368 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1369 throw new IndexOutOfBoundsException(); 1370 } 1371 native_drawText(mNativeCanvas, text, start, end, x, y, paint.mBidiFlags, 1372 paint.mNativePaint); 1373 } 1374 1375 /** 1376 * Draw the specified range of text, specified by start/end, with its 1377 * origin at (x,y), in the specified Paint. The origin is interpreted 1378 * based on the Align setting in the Paint. 1379 * 1380 * @param text The text to be drawn 1381 * @param start The index of the first character in text to draw 1382 * @param end (end - 1) is the index of the last character in text 1383 * to draw 1384 * @param x The x-coordinate of origin for where to draw the text 1385 * @param y The y-coordinate of origin for where to draw the text 1386 * @param paint The paint used for the text (e.g. color, size, style) 1387 */ 1388 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 1389 if (text instanceof String || text instanceof SpannedString || 1390 text instanceof SpannableString) { 1391 native_drawText(mNativeCanvas, text.toString(), start, end, x, y, 1392 paint.mBidiFlags, paint.mNativePaint); 1393 } else if (text instanceof GraphicsOperations) { 1394 ((GraphicsOperations) text).drawText(this, start, end, x, y, 1395 paint); 1396 } else { 1397 char[] buf = TemporaryBuffer.obtain(end - start); 1398 TextUtils.getChars(text, start, end, buf, 0); 1399 native_drawText(mNativeCanvas, buf, 0, end - start, x, y, 1400 paint.mBidiFlags, paint.mNativePaint); 1401 TemporaryBuffer.recycle(buf); 1402 } 1403 } 1404 1405 /** 1406 * Render a run of all LTR or all RTL text, with shaping. This does not run 1407 * bidi on the provided text, but renders it as a uniform right-to-left or 1408 * left-to-right run, as indicated by dir. Alignment of the text is as 1409 * determined by the Paint's TextAlign value. 1410 * 1411 * @param text the text to render 1412 * @param index the start of the text to render 1413 * @param count the count of chars to render 1414 * @param contextIndex the start of the context for shaping. Must be 1415 * no greater than index. 1416 * @param contextCount the number of characters in the context for shaping. 1417 * ContexIndex + contextCount must be no less than index 1418 * + count. 1419 * @param x the x position at which to draw the text 1420 * @param y the y position at which to draw the text 1421 * @param dir the run direction, either {@link #DIRECTION_LTR} or 1422 * {@link #DIRECTION_RTL}. 1423 * @param paint the paint 1424 * @hide 1425 */ 1426 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 1427 float x, float y, int dir, Paint paint) { 1428 1429 if (text == null) { 1430 throw new NullPointerException("text is null"); 1431 } 1432 if (paint == null) { 1433 throw new NullPointerException("paint is null"); 1434 } 1435 if ((index | count | text.length - index - count) < 0) { 1436 throw new IndexOutOfBoundsException(); 1437 } 1438 if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) { 1439 throw new IllegalArgumentException("unknown dir: " + dir); 1440 } 1441 1442 native_drawTextRun(mNativeCanvas, text, index, count, 1443 contextIndex, contextCount, x, y, dir, paint.mNativePaint); 1444 } 1445 1446 /** 1447 * Render a run of all LTR or all RTL text, with shaping. This does not run 1448 * bidi on the provided text, but renders it as a uniform right-to-left or 1449 * left-to-right run, as indicated by dir. Alignment of the text is as 1450 * determined by the Paint's TextAlign value. 1451 * 1452 * @param text the text to render 1453 * @param start the start of the text to render. Data before this position 1454 * can be used for shaping context. 1455 * @param end the end of the text to render. Data at or after this 1456 * position can be used for shaping context. 1457 * @param x the x position at which to draw the text 1458 * @param y the y position at which to draw the text 1459 * @param dir the run direction, either 0 for LTR or 1 for RTL. 1460 * @param paint the paint 1461 * @hide 1462 */ 1463 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 1464 float x, float y, int dir, Paint paint) { 1465 1466 if (text == null) { 1467 throw new NullPointerException("text is null"); 1468 } 1469 if (paint == null) { 1470 throw new NullPointerException("paint is null"); 1471 } 1472 if ((start | end | end - start | text.length() - end) < 0) { 1473 throw new IndexOutOfBoundsException(); 1474 } 1475 1476 int flags = dir == 0 ? 0 : 1; 1477 1478 if (text instanceof String || text instanceof SpannedString || 1479 text instanceof SpannableString) { 1480 native_drawTextRun(mNativeCanvas, text.toString(), start, end, 1481 contextStart, contextEnd, x, y, flags, paint.mNativePaint); 1482 } else if (text instanceof GraphicsOperations) { 1483 ((GraphicsOperations) text).drawTextRun(this, start, end, 1484 contextStart, contextEnd, x, y, flags, paint); 1485 } else { 1486 int contextLen = contextEnd - contextStart; 1487 int len = end - start; 1488 char[] buf = TemporaryBuffer.obtain(contextLen); 1489 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1490 native_drawTextRun(mNativeCanvas, buf, start - contextStart, len, 1491 0, contextLen, x, y, flags, paint.mNativePaint); 1492 TemporaryBuffer.recycle(buf); 1493 } 1494 } 1495 1496 /** 1497 * Draw the text in the array, with each character's origin specified by 1498 * the pos array. 1499 * 1500 * This method does not support glyph composition and decomposition and 1501 * should therefore not be used to render complex scripts. 1502 * 1503 * @param text The text to be drawn 1504 * @param index The index of the first character to draw 1505 * @param count The number of characters to draw, starting from index. 1506 * @param pos Array of [x,y] positions, used to position each 1507 * character 1508 * @param paint The paint used for the text (e.g. color, size, style) 1509 */ 1510 @Deprecated 1511 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 1512 if (index < 0 || index + count > text.length || count*2 > pos.length) { 1513 throw new IndexOutOfBoundsException(); 1514 } 1515 native_drawPosText(mNativeCanvas, text, index, count, pos, 1516 paint.mNativePaint); 1517 } 1518 1519 /** 1520 * Draw the text in the array, with each character's origin specified by 1521 * the pos array. 1522 * 1523 * This method does not support glyph composition and decomposition and 1524 * should therefore not be used to render complex scripts. 1525 * 1526 * @param text The text to be drawn 1527 * @param pos Array of [x,y] positions, used to position each character 1528 * @param paint The paint used for the text (e.g. color, size, style) 1529 */ 1530 @Deprecated 1531 public void drawPosText(String text, float[] pos, Paint paint) { 1532 if (text.length()*2 > pos.length) { 1533 throw new ArrayIndexOutOfBoundsException(); 1534 } 1535 native_drawPosText(mNativeCanvas, text, pos, paint.mNativePaint); 1536 } 1537 1538 /** 1539 * Draw the text, with origin at (x,y), using the specified paint, along 1540 * the specified path. The paint's Align setting determins where along the 1541 * path to start the text. 1542 * 1543 * @param text The text to be drawn 1544 * @param path The path the text should follow for its baseline 1545 * @param hOffset The distance along the path to add to the text's 1546 * starting position 1547 * @param vOffset The distance above(-) or below(+) the path to position 1548 * the text 1549 * @param paint The paint used for the text (e.g. color, size, style) 1550 */ 1551 public void drawTextOnPath(char[] text, int index, int count, Path path, 1552 float hOffset, float vOffset, Paint paint) { 1553 if (index < 0 || index + count > text.length) { 1554 throw new ArrayIndexOutOfBoundsException(); 1555 } 1556 native_drawTextOnPath(mNativeCanvas, text, index, count, 1557 path.ni(), hOffset, vOffset, 1558 paint.mBidiFlags, paint.mNativePaint); 1559 } 1560 1561 /** 1562 * Draw the text, with origin at (x,y), using the specified paint, along 1563 * the specified path. The paint's Align setting determins where along the 1564 * path to start the text. 1565 * 1566 * @param text The text to be drawn 1567 * @param path The path the text should follow for its baseline 1568 * @param hOffset The distance along the path to add to the text's 1569 * starting position 1570 * @param vOffset The distance above(-) or below(+) the path to position 1571 * the text 1572 * @param paint The paint used for the text (e.g. color, size, style) 1573 */ 1574 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 1575 if (text.length() > 0) { 1576 native_drawTextOnPath(mNativeCanvas, text, path.ni(), hOffset, vOffset, 1577 paint.mBidiFlags, paint.mNativePaint); 1578 } 1579 } 1580 1581 /** 1582 * Save the canvas state, draw the picture, and restore the canvas state. 1583 * This differs from picture.draw(canvas), which does not perform any 1584 * save/restore. 1585 * 1586 * @param picture The picture to be drawn 1587 */ 1588 public void drawPicture(Picture picture) { 1589 picture.endRecording(); 1590 native_drawPicture(mNativeCanvas, picture.ni()); 1591 } 1592 1593 /** 1594 * Draw the picture, stretched to fit into the dst rectangle. 1595 */ 1596 public void drawPicture(Picture picture, RectF dst) { 1597 save(); 1598 translate(dst.left, dst.top); 1599 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1600 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1601 } 1602 drawPicture(picture); 1603 restore(); 1604 } 1605 1606 /** 1607 * Draw the picture, stretched to fit into the dst rectangle. 1608 */ 1609 public void drawPicture(Picture picture, Rect dst) { 1610 save(); 1611 translate(dst.left, dst.top); 1612 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1613 scale((float) dst.width() / picture.getWidth(), 1614 (float) dst.height() / picture.getHeight()); 1615 } 1616 drawPicture(picture); 1617 restore(); 1618 } 1619 1620 /** 1621 * Free up as much memory as possible from private caches (e.g. fonts, images) 1622 * 1623 * @hide 1624 */ 1625 public static native void freeCaches(); 1626 1627 private static native int initRaster(int nativeBitmapOrZero); 1628 private static native void native_setBitmap(int nativeCanvas, int bitmap); 1629 private static native int native_saveLayer(int nativeCanvas, RectF bounds, 1630 int paint, int layerFlags); 1631 private static native int native_saveLayer(int nativeCanvas, float l, 1632 float t, float r, float b, 1633 int paint, int layerFlags); 1634 private static native int native_saveLayerAlpha(int nativeCanvas, 1635 RectF bounds, int alpha, 1636 int layerFlags); 1637 private static native int native_saveLayerAlpha(int nativeCanvas, float l, 1638 float t, float r, float b, 1639 int alpha, int layerFlags); 1640 1641 private static native void native_concat(int nCanvas, int nMatrix); 1642 private static native void native_setMatrix(int nCanvas, int nMatrix); 1643 private static native boolean native_clipRect(int nCanvas, 1644 float left, float top, 1645 float right, float bottom, 1646 int regionOp); 1647 private static native boolean native_clipPath(int nativeCanvas, 1648 int nativePath, 1649 int regionOp); 1650 private static native boolean native_clipRegion(int nativeCanvas, 1651 int nativeRegion, 1652 int regionOp); 1653 private static native void nativeSetDrawFilter(int nativeCanvas, 1654 int nativeFilter); 1655 private static native boolean native_getClipBounds(int nativeCanvas, 1656 Rect bounds); 1657 private static native void native_getCTM(int canvas, int matrix); 1658 private static native boolean native_quickReject(int nativeCanvas, 1659 RectF rect, 1660 int native_edgeType); 1661 private static native boolean native_quickReject(int nativeCanvas, 1662 int path, 1663 int native_edgeType); 1664 private static native boolean native_quickReject(int nativeCanvas, 1665 float left, float top, 1666 float right, float bottom, 1667 int native_edgeType); 1668 private static native void native_drawRGB(int nativeCanvas, int r, int g, 1669 int b); 1670 private static native void native_drawARGB(int nativeCanvas, int a, int r, 1671 int g, int b); 1672 private static native void native_drawColor(int nativeCanvas, int color); 1673 private static native void native_drawColor(int nativeCanvas, int color, 1674 int mode); 1675 private static native void native_drawPaint(int nativeCanvas, int paint); 1676 private static native void native_drawLine(int nativeCanvas, float startX, 1677 float startY, float stopX, 1678 float stopY, int paint); 1679 private static native void native_drawRect(int nativeCanvas, RectF rect, 1680 int paint); 1681 private static native void native_drawRect(int nativeCanvas, float left, 1682 float top, float right, 1683 float bottom, int paint); 1684 private static native void native_drawOval(int nativeCanvas, RectF oval, 1685 int paint); 1686 private static native void native_drawCircle(int nativeCanvas, float cx, 1687 float cy, float radius, 1688 int paint); 1689 private static native void native_drawArc(int nativeCanvas, RectF oval, 1690 float startAngle, float sweep, 1691 boolean useCenter, int paint); 1692 private static native void native_drawRoundRect(int nativeCanvas, 1693 RectF rect, float rx, 1694 float ry, int paint); 1695 private static native void native_drawPath(int nativeCanvas, int path, 1696 int paint); 1697 private native void native_drawBitmap(int nativeCanvas, int bitmap, 1698 float left, float top, 1699 int nativePaintOrZero, 1700 int canvasDensity, 1701 int screenDensity, 1702 int bitmapDensity); 1703 private native void native_drawBitmap(int nativeCanvas, int bitmap, 1704 Rect src, RectF dst, 1705 int nativePaintOrZero, 1706 int screenDensity, 1707 int bitmapDensity); 1708 private static native void native_drawBitmap(int nativeCanvas, int bitmap, 1709 Rect src, Rect dst, 1710 int nativePaintOrZero, 1711 int screenDensity, 1712 int bitmapDensity); 1713 private static native void native_drawBitmap(int nativeCanvas, int[] colors, 1714 int offset, int stride, float x, 1715 float y, int width, int height, 1716 boolean hasAlpha, 1717 int nativePaintOrZero); 1718 private static native void nativeDrawBitmapMatrix(int nCanvas, int nBitmap, 1719 int nMatrix, int nPaint); 1720 private static native void nativeDrawBitmapMesh(int nCanvas, int nBitmap, 1721 int meshWidth, int meshHeight, 1722 float[] verts, int vertOffset, 1723 int[] colors, int colorOffset, int nPaint); 1724 private static native void nativeDrawVertices(int nCanvas, int mode, int n, 1725 float[] verts, int vertOffset, float[] texs, int texOffset, 1726 int[] colors, int colorOffset, short[] indices, 1727 int indexOffset, int indexCount, int nPaint); 1728 1729 private static native void native_drawText(int nativeCanvas, char[] text, 1730 int index, int count, float x, 1731 float y, int flags, int paint); 1732 private static native void native_drawText(int nativeCanvas, String text, 1733 int start, int end, float x, 1734 float y, int flags, int paint); 1735 1736 private static native void native_drawTextRun(int nativeCanvas, String text, 1737 int start, int end, int contextStart, int contextEnd, 1738 float x, float y, int flags, int paint); 1739 1740 private static native void native_drawTextRun(int nativeCanvas, char[] text, 1741 int start, int count, int contextStart, int contextCount, 1742 float x, float y, int flags, int paint); 1743 1744 private static native void native_drawPosText(int nativeCanvas, 1745 char[] text, int index, 1746 int count, float[] pos, 1747 int paint); 1748 private static native void native_drawPosText(int nativeCanvas, 1749 String text, float[] pos, 1750 int paint); 1751 private static native void native_drawTextOnPath(int nativeCanvas, 1752 char[] text, int index, 1753 int count, int path, 1754 float hOffset, 1755 float vOffset, int bidiFlags, 1756 int paint); 1757 private static native void native_drawTextOnPath(int nativeCanvas, 1758 String text, int path, 1759 float hOffset, 1760 float vOffset, 1761 int flags, int paint); 1762 private static native void native_drawPicture(int nativeCanvas, 1763 int nativePicture); 1764 private static native void finalizer(int nativeCanvas); 1765} 1766