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