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