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