Canvas.java revision da3198a550bdd91ccdc8a09c3a6e00d3b61c0c20
15c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran/* 25c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Copyright (C) 2006 The Android Open Source Project 35c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 45c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Licensed under the Apache License, Version 2.0 (the "License"); 55c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * you may not use this file except in compliance with the License. 65c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * You may obtain a copy of the License at 75c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 85c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * http://www.apache.org/licenses/LICENSE-2.0 95c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 105c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Unless required by applicable law or agreed to in writing, software 115c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * distributed under the License is distributed on an "AS IS" BASIS, 125c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * See the License for the specific language governing permissions and 145c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * limitations under the License. 155c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran */ 165c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 175c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandranpackage android.graphics; 185c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 195c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandranimport android.annotation.IntDef; 20b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandranimport android.annotation.NonNull; 21f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandranimport android.annotation.Nullable; 22f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandranimport android.text.GraphicsOperations; 23f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandranimport android.text.SpannableString; 24f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandranimport android.text.SpannedString; 255009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandranimport android.text.TextUtils; 265c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 27ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colittiimport java.lang.annotation.Retention; 284acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandranimport java.lang.annotation.RetentionPolicy; 294753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 30a561e121c724e9163b2e256e15eef660e3a326daPaul Jensenimport javax.microedition.khronos.opengles.GL; 315c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 324acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran/** 335c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * The Canvas class holds the "draw" calls. To draw something, you need 345c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 4 basic components: A Bitmap to hold the pixels, a Canvas to host 355c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, 36f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Path, text, Bitmap), and a paint (to describe the colors and styles for the 37f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * drawing). 385009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 395009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * <div class="special reference"> 405009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * <h3>Developer Guides</h3> 415009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * <p>For more information about how to use Canvas, read the 425009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html"> 435009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Canvas and Drawables</a> developer guide.</p></div> 445009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 455009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandranpublic class Canvas { 465009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 475009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran // assigned in constructors or setBitmap, freed in finalizer 485009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran private long mNativeCanvasWrapper; 495009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 505009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** @hide */ 515009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public long getNativeCanvasWrapper() { 525009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return mNativeCanvasWrapper; 535c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran } 544acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 554acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** @hide */ 564acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public boolean isRecordingFor(Object o) { return false; } 574acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 584acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran // may be null 594acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran private Bitmap mBitmap; 605965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti 615965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti // optional field set by the caller 625965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti private DrawFilter mDrawFilter; 635965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti 645965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti /** 655965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti * @hide 665965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti */ 675965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti protected int mDensity = Bitmap.DENSITY_NONE; 685965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti 695965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti /** 7072723683be57b6d562dc96cde30cc33cc96b3e82Lorenzo Colitti * Used to determine when compatibility scaling is in effect. 7172723683be57b6d562dc96cde30cc33cc96b3e82Lorenzo Colitti * 724753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @hide 73f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 74f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran protected int mScreenDensity = Bitmap.DENSITY_NONE; 75f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 76f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran // Used by native code 77f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran @SuppressWarnings("UnusedDeclaration") 78f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private int mSurfaceFormat; 795009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 805009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 815009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Flag for drawTextRun indicating left-to-right run direction. 825009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @hide 835009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 845009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public static final int DIRECTION_LTR = 0; 854acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 86c7d804c1d22db40ac79fde46a032cd359e975f5bSreeram Ramachandran /** 87c7d804c1d22db40ac79fde46a032cd359e975f5bSreeram Ramachandran * Flag for drawTextRun indicating right-to-left run direction. 884acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @hide 89f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 90f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public static final int DIRECTION_RTL = 1; 91f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 92f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran // Maximum bitmap size as defined in Skia's native code 93f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran // (see SkCanvas.cpp, SkDraw.cpp) 94f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private static final int MAXMIMUM_BITMAP_SIZE = 32766; 95f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 96f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran // This field is used to finalize the native Canvas properly 97f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private final CanvasFinalizer mFinalizer; 98f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 99f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private static final class CanvasFinalizer { 100f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private long mNativeCanvasWrapper; 101f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 102f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public CanvasFinalizer(long nativeCanvas) { 103f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran mNativeCanvasWrapper = nativeCanvas; 104f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } 105f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 106f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran @Override 107f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran protected void finalize() throws Throwable { 108f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran try { 109f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran dispose(); 110f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } finally { 1114acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran super.finalize(); 1124acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 113a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen } 1145c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 115a48118062412f16ae712bfc8c8a539d3b6a85e47Sreeram Ramachandran public void dispose() { 116a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen if (mNativeCanvasWrapper != 0) { 1174acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran finalizer(mNativeCanvasWrapper); 1184acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran mNativeCanvasWrapper = 0; 1194acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1204acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1214acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1224acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 1234acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** 1244acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * Construct an empty raster canvas. Use setBitmap() to specify a bitmap to 1254acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * draw into. The initial target density is {@link Bitmap#DENSITY_NONE}; 1264acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * this will typically be replaced when a target bitmap is set for the 1274acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * canvas. 1284acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran */ 1294acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public Canvas() { 1304acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran if (!isHardwareAccelerated()) { 1314acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran // 0 means no native bitmap 1324acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran mNativeCanvasWrapper = initRaster(0); 1334acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper); 1344acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } else { 1354acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran mFinalizer = null; 1364acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1374acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1384acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 1394acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** 1404acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * Construct a canvas with the specified bitmap to draw into. The bitmap 1414acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * must be mutable. 1424acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * 1434acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * <p>The initial target density of the canvas is the same as the given 1444acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * bitmap's density. 1454acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * 1464acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param bitmap Specifies a mutable bitmap for the canvas to draw into. 1474acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran */ 1484acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public Canvas(@NonNull Bitmap bitmap) { 149c7d804c1d22db40ac79fde46a032cd359e975f5bSreeram Ramachandran if (!bitmap.isMutable()) { 1504acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran throw new IllegalStateException("Immutable bitmap passed to Canvas constructor"); 1514acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1524acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran throwIfCannotDraw(bitmap); 153a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen mNativeCanvasWrapper = initRaster(bitmap.ni()); 1544acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper); 155c7d804c1d22db40ac79fde46a032cd359e975f5bSreeram Ramachandran mBitmap = bitmap; 156c7d804c1d22db40ac79fde46a032cd359e975f5bSreeram Ramachandran mDensity = bitmap.mDensity; 1574acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1584acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 1594acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** @hide */ 1604acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public Canvas(long nativeCanvas) { 1614acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran if (nativeCanvas == 0) { 1624acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran throw new IllegalStateException(); 1634acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 1645c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran mNativeCanvasWrapper = nativeCanvas; 1655c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper); 1664753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti mDensity = Bitmap.getDefaultDensity(); 1674753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti } 1684753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 1694753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** 170f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Returns null. 1714753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 1724753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @deprecated This method is not supported and should not be invoked. 1734753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 1744753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @hide 1754753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti */ 1764753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti @Deprecated 1774753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti protected GL getGL() { 1784753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti return null; 1794753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti } 1804753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 1814753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** 1824753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * Indicates whether this Canvas uses hardware acceleration. 1834753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 1844753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * Note that this method does not define what type of hardware acceleration 1854753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * may or may not be used. 1864753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 1874753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @return True if drawing operations are hardware accelerated, 1884753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * false otherwise. 189f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 190f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public boolean isHardwareAccelerated() { 1914753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti return false; 1924753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti } 1934753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 1944753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** 195f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Specify a bitmap for the canvas to draw into. All canvas state such as 196f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * layers, filters, and the save/restore stack are reset with the exception 197f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * of the current matrix and clip stack. Additionally, as a side-effect 1984753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * the canvas' target density is updated to match that of the bitmap. 1991201e84ebda52d8d82e5385644e9de0923e61aa3Sreeram Ramachandran * 2004753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @param bitmap Specifies a mutable bitmap for the canvas to draw into. 2014753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @see #setDensity(int) 2024753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @see #getDensity() 203f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 2044753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti public void setBitmap(@Nullable Bitmap bitmap) { 2054753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti if (isHardwareAccelerated()) { 2064753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti throw new RuntimeException("Can't set a bitmap device on a HW accelerated canvas"); 2074753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti } 2084753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 2094753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti if (bitmap == null) { 2104753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti native_setBitmap(mNativeCanvasWrapper, 0, false); 2114753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti mDensity = Bitmap.DENSITY_NONE; 2124753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti } else { 2134753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti if (!bitmap.isMutable()) { 2148fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran throw new IllegalStateException(); 2158fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran } 2168fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran throwIfCannotDraw(bitmap); 2178fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran 2188fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran native_setBitmap(mNativeCanvasWrapper, bitmap.ni(), true); 2198fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran mDensity = bitmap.mDensity; 2208fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran } 22196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti 22296f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti mBitmap = bitmap; 223f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } 224f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 225f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 226f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * setBitmap() variant for native callers with a raw bitmap handle. 227f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 228f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran private void setNativeBitmap(long bitmapHandle) { 229f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran native_setBitmap(mNativeCanvasWrapper, bitmapHandle, false); 230f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } 231f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 2324753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** 2334753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * Set the viewport dimensions if this canvas is GL based. If it is not, 2344753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * this method is ignored and no exception is thrown. 2354753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 2364753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @param width The width of the viewport 2375009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param height The height of the viewport 2384753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * 2394753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @hide 240cf891383224e420d99996f7e63728a7cc902415eSreeram Ramachandran */ 2414753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti public void setViewport(int width, int height) {} 2424753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 2434753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** @hide */ 2444753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti public void setHighContrastText(boolean highContrastText) {} 2457619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran 2465965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti /** @hide */ 247f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public void insertReorderBarrier() {} 248cf891383224e420d99996f7e63728a7cc902415eSreeram Ramachandran 2495965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti /** @hide */ 2505965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti public void insertInorderBarrier() {} 25172723683be57b6d562dc96cde30cc33cc96b3e82Lorenzo Colitti 2525965651602fb8373b75b6ae2d59c6a4d753f2f49Lorenzo Colitti /** 2534753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * Return true if the device that the current layer draws into is opaque 2544753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * (i.e. does not support per-pixel alpha). 2555009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 2565009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return true if the device that the current layer draws into is opaque 2574753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti */ 258a10ac3214f6a582b7fdb66acc43c702731e53d81Lorenzo Colitti public boolean isOpaque() { 259f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_isOpaque(mNativeCanvasWrapper); 260a10ac3214f6a582b7fdb66acc43c702731e53d81Lorenzo Colitti } 2614753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 262f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 263f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Returns the width of the current drawing layer 264f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 265f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return the width of the current drawing layer 2665009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 2675009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int getWidth() { 268f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_getWidth(mNativeCanvasWrapper); 269f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } 270f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 271f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 272f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Returns the height of the current drawing layer 273f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 274f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return the height of the current drawing layer 275f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 2765009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int getHeight() { 277f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_getHeight(mNativeCanvasWrapper); 278f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran } 2794753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 2804753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti /** 28172723683be57b6d562dc96cde30cc33cc96b3e82Lorenzo Colitti * <p>Returns the target density of the canvas. The default density is 282f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * derived from the density of its backing bitmap, or 283f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * {@link Bitmap#DENSITY_NONE} if there is not one.</p> 284f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 28596f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti * @return Returns the current target density of the canvas, which is used 2865c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * to determine the scaling factor when drawing a bitmap into it. 2875c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 2885c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * @see #setDensity(int) 28996f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti * @see Bitmap#getDensity() 2905c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran */ 2915c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran public int getDensity() { 292ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti return mDensity; 293ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti } 294f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 295f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 296ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * <p>Specifies the density for this Canvas' backing bitmap. This modifies 297ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * the target density of the canvas itself, as well as the density of its 298f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}. 299ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * 300ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * @param density The new target density of the canvas, which is used 301ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * to determine the scaling factor when drawing a bitmap into it. Use 302ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * {@link Bitmap#DENSITY_NONE} to disable bitmap scaling. 303ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * 304f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @see #getDensity() 305f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @see Bitmap#setDensity(int) 306ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti */ 307ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti public void setDensity(int density) { 308ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti if (mBitmap != null) { 309f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran mBitmap.setDensity(density); 310ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti } 311ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti mDensity = density; 3127619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran } 313ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti 3141201e84ebda52d8d82e5385644e9de0923e61aa3Sreeram Ramachandran /** @hide */ 315ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti public void setScreenDensity(int density) { 316ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti mScreenDensity = density; 317ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti } 318ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti 319ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti /** 3205009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Returns the maximum allowed width for bitmaps drawn with this canvas. 321ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * Attempting to draw with a bitmap wider than this value will result 322ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * in an error. 323f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 324ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * @see #getMaximumBitmapHeight() 3257619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran */ 3267619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran public int getMaximumBitmapWidth() { 3277619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran return MAXMIMUM_BITMAP_SIZE; 328ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti } 329ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti 330f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 331f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Returns the maximum allowed height for bitmaps drawn with this canvas. 332ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * Attempting to draw with a bitmap taller than this value will result 333ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * in an error. 334ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti * 3354753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti * @see #getMaximumBitmapWidth() 336f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 337ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti public int getMaximumBitmapHeight() { 338ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti return MAXMIMUM_BITMAP_SIZE; 339ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti } 340ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti 341ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti // the SAVE_FLAG constants must match their native equivalents 3424753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti 343f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** @hide */ 344f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran @IntDef(flag = true, 345ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti value = { 346ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti MATRIX_SAVE_FLAG, 347f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran CLIP_SAVE_FLAG, 348f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran HAS_ALPHA_LAYER_SAVE_FLAG, 349f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran FULL_COLOR_LAYER_SAVE_FLAG, 350f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran CLIP_TO_LAYER_SAVE_FLAG, 351f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran ALL_SAVE_FLAG 352f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran }) 3535009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran @Retention(RetentionPolicy.SOURCE) 3545009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public @interface Saveflags {} 355f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 356f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** restore the current matrix when restore() is called */ 357ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti public static final int MATRIX_SAVE_FLAG = 0x01; 358ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti /** restore the current clip when restore() is called */ 359f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public static final int CLIP_SAVE_FLAG = 0x02; 360f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** the layer needs to per-pixel alpha */ 3614753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04; 3627619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran /** the layer needs to 8-bits per color component */ 3637619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08; 3645009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** clip against the layer's bounds */ 3655009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10; 3665009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** restore everything when restore() is called */ 3675009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public static final int ALL_SAVE_FLAG = 0x1F; 3685009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 3695009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 3705009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Saves the current matrix and clip onto a private stack. Subsequent 3715009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * calls to translate,scale,rotate,skew,concat or clipRect,clipPath 3725009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * will all operate as usual, but when the balancing call to restore() 3734acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * is made, those calls will be forgotten, and the settings that existed 3744acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * before the save() will be reinstated. 3754acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * 3765009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return The value to pass to restoreToCount() to balance this save() 3775009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 3785009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int save() { 3794acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran return native_save(mNativeCanvasWrapper, MATRIX_SAVE_FLAG | CLIP_SAVE_FLAG); 3805009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 3815009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 3825009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 3835009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Based on saveFlags, can save the current matrix and clip onto a private 3845009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * stack. Subsequent calls to translate,scale,rotate,skew,concat or 3855009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * clipRect,clipPath will all operate as usual, but when the balancing 3865009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * call to restore() is made, those calls will be forgotten, and the 3875009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * settings that existed before the save() will be reinstated. 3884acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * 3894acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param saveFlags flag bits that specify which parts of the Canvas state 3905009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * to save/restore 3915009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return The value to pass to restoreToCount() to balance this save() 3925009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 3935009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int save(@Saveflags int saveFlags) { 3945009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return native_save(mNativeCanvasWrapper, saveFlags); 3955009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 3965009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 3975009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 3985009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * This behaves the same as save(), but in addition it allocates an 3995009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * offscreen bitmap. All drawing calls are directed there, and only when 4005009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * the balancing call to restore() is made is that offscreen transfered to 4015009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * the canvas (or the previous layer). Subsequent calls to translate, 4025009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * scale, rotate, skew, concat or clipRect, clipPath all operate on this 4035009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * copy. When the balancing call to restore() is made, this copy is 4045009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * deleted and the previous matrix/clip state is restored. 4055009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 4065009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param bounds May be null. The maximum size the offscreen bitmap 4075009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * needs to be (in local coordinates) 4085009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param paint This is copied, and is applied to the offscreen when 4095009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * restore() is called. 4105009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param saveFlags see _SAVE_FLAG constants 4115009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return value to pass to restoreToCount() to balance this save() 4125009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4135009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags) { 4145009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran if (bounds == null) { 4155009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran bounds = new RectF(getClipBounds()); 4165009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4175009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); 4185009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4195009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4205009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4215009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Convenience for saveLayer(bounds, paint, {@link #ALL_SAVE_FLAG}) 4225009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4235009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint) { 4245009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return saveLayer(bounds, paint, ALL_SAVE_FLAG); 4255009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4265009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4275009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4285009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Helper version of saveLayer() that takes 4 values rather than a RectF. 4295009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4305009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint, 431b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran @Saveflags int saveFlags) { 4325009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return native_saveLayer(mNativeCanvasWrapper, left, top, right, bottom, 4335009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran paint != null ? paint.getNativeInstance() : 0, 434b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran saveFlags); 435b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran } 436b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran 437b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran /** 438b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * Convenience for saveLayer(left, top, right, bottom, paint, {@link #ALL_SAVE_FLAG}) 4395009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 440b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint) { 441b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG); 442b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran } 4435009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4445009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4455009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * This behaves the same as save(), but in addition it allocates an 4465009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * offscreen bitmap. All drawing calls are directed there, and only when 4475009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * the balancing call to restore() is made is that offscreen transfered to 4485009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * the canvas (or the previous layer). Subsequent calls to translate, 4495009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * scale, rotate, skew, concat or clipRect, clipPath all operate on this 4505009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * copy. When the balancing call to restore() is made, this copy is 4515009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * deleted and the previous matrix/clip state is restored. 4525009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 453eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @param bounds The maximum size the offscreen bitmap needs to be 454eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * (in local coordinates) 455eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @param alpha The alpha to apply to the offscreen when when it is 456eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran drawn during restore() 457eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @param saveFlags see _SAVE_FLAG constants 458eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @return value to pass to restoreToCount() to balance this call 4595009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4605009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayerAlpha(@Nullable RectF bounds, int alpha, @Saveflags int saveFlags) { 461eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran if (bounds == null) { 4625009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran bounds = new RectF(getClipBounds()); 4635009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4645c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, alpha, saveFlags); 4655009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4665009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4675009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4685c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Convenience for saveLayerAlpha(bounds, alpha, {@link #ALL_SAVE_FLAG}) 4695009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4705009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayerAlpha(@Nullable RectF bounds, int alpha) { 4715009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return saveLayerAlpha(bounds, alpha, ALL_SAVE_FLAG); 4725009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4735009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4745009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4755009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Helper for saveLayerAlpha() that takes 4 values instead of a RectF. 476122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran */ 477122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 4785c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran @Saveflags int saveFlags) { 479f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran alpha = Math.min(255, Math.max(0, alpha)); 480f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_saveLayerAlpha(mNativeCanvasWrapper, left, top, right, bottom, 4815c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran alpha, saveFlags); 4825009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4835009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4845009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4855009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Helper for saveLayerAlpha(left, top, right, bottom, alpha, {@link #ALL_SAVE_FLAG}) 4865009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 4875009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) { 4885009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return saveLayerAlpha(left, top, right, bottom, alpha, ALL_SAVE_FLAG); 4895009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 4905009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 4915009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 4925009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * This call balances a previous call to save(), and is used to remove all 4935009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * modifications to the matrix/clip state since the last save call. It is 4945009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * an error to call restore() more times than save() was called. 4955009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 496122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran public void restore() { 497122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran native_restore(mNativeCanvasWrapper); 498122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran } 4995009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 500122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran /** 5015c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Returns the number of matrix/clip states on the Canvas' private stack. 5025009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * This will equal # save() calls - # restore() calls. 5035009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 504eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran public int getSaveCount() { 5055009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return native_getSaveCount(mNativeCanvasWrapper); 5065009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5075009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 5085c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran /** 5095009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Efficient way to pop any calls to save() that happened after the save 5105009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * count reached saveCount. It is an error for saveCount to be less than 1. 5115009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5125009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Example: 5135009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * int count = canvas.save(); 5145009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * ... // more calls potentially to save() 5155009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * canvas.restoreToCount(count); 5165009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * // now the canvas is back in the same state it was before the initial 5175009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * // call to save(). 5185009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5195009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param saveCount The save level to restore to. 5205009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 5215009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public void restoreToCount(int saveCount) { 5225009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran native_restoreToCount(mNativeCanvasWrapper, saveCount); 5235009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5245009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 5255009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 5265009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified translation 5275009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5285009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param dx The distance to translate in X 5295009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param dy The distance to translate in Y 5305009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 5315009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public void translate(float dx, float dy) { 5325009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran native_translate(mNativeCanvasWrapper, dx, dy); 5335009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5345009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 5355009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 5365009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified scale. 5375009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5385009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param sx The amount to scale in X 5395009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param sy The amount to scale in Y 5405009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 5415009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public void scale(float sx, float sy) { 5425009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran native_scale(mNativeCanvasWrapper, sx, sy); 5435c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran } 5445c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran 5455009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 5465009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified scale. 5474043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * 5485009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param sx The amount to scale in X 5494043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * @param sy The amount to scale in Y 5504043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * @param px The x-coord for the pivot point (unchanged by the scale) 5514043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * @param py The y-coord for the pivot point (unchanged by the scale) 5525009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 5535009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public final void scale(float sx, float sy, float px, float py) { 5545009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran translate(px, py); 5555009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran scale(sx, sy); 5565009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran translate(-px, -py); 5575009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5585009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 5595009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 5605009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified rotation. 5615009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5625009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param degrees The amount to rotate, in degrees 5635009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 5645009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public void rotate(float degrees) { 5654043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran native_rotate(mNativeCanvasWrapper, degrees); 5665009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5675009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 5685009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 5695009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified rotation. 5705009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 5715009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param degrees The amount to rotate, in degrees 5725009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param px The x-coord for the pivot point (unchanged by the rotation) 573b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * @param py The y-coord for the pivot point (unchanged by the rotation) 574e09b20aee85f1dfd8c18c3d8581ac875d939ba70Sreeram Ramachandran */ 5755009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public final void rotate(float degrees, float px, float py) { 5765009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran translate(px, py); 577b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran rotate(degrees); 578b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran translate(-px, -py); 5795009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 5805009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 581b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran /** 582b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * Preconcat the current matrix with the specified skew. 5835009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 584eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @param sx The amount to skew in X 585eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @param sy The amount to skew in Y 5865009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 587eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran public void skew(float sx, float sy) { 5885009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran native_skew(mNativeCanvasWrapper, sx, sy); 5895009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 590b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran 591b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran /** 5925009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Preconcat the current matrix with the specified matrix. If the specified 5935009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * matrix is null, this method does nothing. 594b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * 595b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * @param matrix The matrix to preconcatenate with the current matrix 5965009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 597b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran public void concat(@Nullable Matrix matrix) { 5984043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran if (matrix != null) native_concat(mNativeCanvasWrapper, matrix.native_instance); 599b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran } 6004043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran 6014043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran /** 6025009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Completely replace the current matrix with the specified matrix. If the 6035009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * matrix parameter is null, then the current matrix is reset to identity. 6049c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * 6055009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * <strong>Note:</strong> it is recommended to use {@link #concat(Matrix)}, 60696f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti * {@link #scale(float, float)}, {@link #translate(float, float)} and 6079c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * {@link #rotate(float)} instead of this method. 6089c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * 609122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran * @param matrix The matrix to replace the current matrix with. If it is 610122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran * null, set the current matrix to identity. 611f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 6125009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @see #concat(Matrix) 613122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran */ 614f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran public void setMatrix(@Nullable Matrix matrix) { 615f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran native_setMatrix(mNativeCanvasWrapper, 616122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran matrix == null ? 0 : matrix.native_instance); 6179c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran } 6185009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 6195009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 6207619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran * Return, in ctm, the current transformation matrix. This does not alter 6217619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran * the matrix in the canvas, but just returns a copy of it. 622f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 623f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti @Deprecated 624f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti public void getMatrix(@NonNull Matrix ctm) { 625eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran native_getCTM(mNativeCanvasWrapper, ctm.native_instance); 626eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran } 6275009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 62838b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran /** 62938b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran * Return a new matrix with a copy of the canvas' current transformation 63038b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran * matrix. 6315009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 6325009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran @Deprecated 6335009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public final @NonNull Matrix getMatrix() { 63438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran Matrix m = new Matrix(); 63538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran //noinspection deprecation 6365009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran getMatrix(m); 6374acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran return m; 63838b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran } 63938b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran 6405009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 6414acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * Modify the current clip with the specified rectangle. 64238b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran * 64338b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran * @param rect The rect to intersect with the current clip 64438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran * @param op How the clip is modified 6459c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * @return true if the resulting clip is non-empty 646f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti */ 6471077d298655efa7755925e788d49a73e9db10afcSreeram Ramachandran public boolean clipRect(@NonNull RectF rect, @NonNull Region.Op op) { 6481077d298655efa7755925e788d49a73e9db10afcSreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 6495009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran op.nativeInt); 6505009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 651f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti 652c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran /** 653c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran * Modify the current clip with the specified rectangle, which is 654c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran * expressed in local coordinates. 655f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti * 656f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti * @param rect The rectangle to intersect with the current clip. 657f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti * @param op How the clip is modified 658f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti * @return true if the resulting clip is non-empty 659f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti */ 660f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti public boolean clipRect(@NonNull Rect rect, @NonNull Region.Op op) { 661f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 662f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti op.nativeInt); 663f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti } 664c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran 665c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran /** 666f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti * Intersect the current clip with the specified rectangle, which is 6679c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * expressed in local coordinates. 6689c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * 669f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @param rect The rectangle to intersect with the current clip. 670f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return true if the resulting clip is non-empty 67192b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran */ 6725009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public boolean clipRect(@NonNull RectF rect) { 673f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 67492b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran Region.Op.INTERSECT.nativeInt); 67592b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran } 676357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti 677357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti /** 678357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * Intersect the current clip with the specified rectangle, which is 679f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * expressed in local coordinates. 680357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * 681357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * @param rect The rectangle to intersect with the current clip. 682f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return true if the resulting clip is non-empty 68372999d6cfc1df23cd911b85730f1f532fb62bed4Sreeram Ramachandran */ 684357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti public boolean clipRect(@NonNull Rect rect) { 685357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, 686357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti Region.Op.INTERSECT.nativeInt); 687357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti } 688f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran 689f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 690f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Modify the current clip with the specified rectangle, which is 691357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * expressed in local coordinates. 692357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * 693357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti * @param left The left side of the rectangle to intersect with the 6944acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * current clip 695f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @param top The top of the rectangle to intersect with the current 69692b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran * clip 69792b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran * @param right The right side of the rectangle to intersect with the 6985c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * current clip 6995c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * @param bottom The bottom of the rectangle to intersect with the current 700f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * clip 7015009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param op How the clip is modified 702f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return true if the resulting clip is non-empty 703f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran */ 7045009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public boolean clipRect(float left, float top, float right, float bottom, 705f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran @NonNull Region.Op op) { 706f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, op.nativeInt); 7075009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran } 7085009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 7094acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** 7104acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * Intersect the current clip with the specified rectangle, which is 7114acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * expressed in local coordinates. 712f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 7134acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param left The left side of the rectangle to intersect with the 714f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * current clip 7158fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran * @param top The top of the rectangle to intersect with the current clip 7168fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran * @param right The right side of the rectangle to intersect with the 7175009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * current clip 7185009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param bottom The bottom of the rectangle to intersect with the current 7194acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * clip 7204acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @return true if the resulting clip is non-empty 7214acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran */ 7224acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public boolean clipRect(float left, float top, float right, float bottom) { 7234acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, 7245c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran Region.Op.INTERSECT.nativeInt); 7255c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran } 7265009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 7275009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 7285009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * Intersect the current clip with the specified rectangle, which is 729f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * expressed in local coordinates. 730f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * 7314acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param left The left side of the rectangle to intersect with the 7324acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * current clip 7334acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param top The top of the rectangle to intersect with the current clip 7344acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * @param right The right side of the rectangle to intersect with the 7354acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * current clip 736379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran * @param bottom The bottom of the rectangle to intersect with the current 737379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran * clip 7385009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return true if the resulting clip is non-empty 7395009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 7404acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran public boolean clipRect(int left, int top, int right, int bottom) { 7414acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, 7424acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran Region.Op.INTERSECT.nativeInt); 7434acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 7444acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 7454acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** 7464043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * Modify the current clip with the specified path. 7474043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * 7485009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param path The path to operate on the current clip 7495009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param op How the clip is modified 7505009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @return true if the resulting is non-empty 7515009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 7524043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran public boolean clipPath(@NonNull Path path, @NonNull Region.Op op) { 7534043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran return native_clipPath(mNativeCanvasWrapper, path.ni(), op.nativeInt); 7544acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran } 7554acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran 7564acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran /** 7574acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * Intersect the current clip with the specified path. 7584acd34a8e95b1191318216ebad409ec5e1b3d5f0Sreeram Ramachandran * 7594043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * @param path The path to intersect with the current clip 7604043f01f8e25f24246efadc710ad7440aab75529Sreeram Ramachandran * @return true if the resulting is non-empty 7615009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 7625009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public boolean clipPath(@NonNull Path path) { 7635009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran return clipPath(path, Region.Op.INTERSECT); 764379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran } 7655009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 766f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran /** 767f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * Modify the current clip with the specified region. Note that unlike 7685009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * clipRect() and clipPath() which transform their arguments by the 769b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * current matrix, clipRegion() assumes its argument is already in the 770b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * coordinate system of the current layer's bitmap, and so not 7715009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * transformation is performed. 7725009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * 7735009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param region The region to operate on the current clip, based on op 7745009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @param op How the clip is modified 775b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * @return true if the resulting is non-empty 776b1425cc09f8a29350520db0d4f489331df5a689bSreeram Ramachandran * 7775009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * @deprecated Unlike all other clip calls this API does not respect the 7785009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * current matrix. Use {@link #clipRect(Rect)} as an alternative. 7795009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran */ 7805009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran public boolean clipRegion(@NonNull Region region, @NonNull Region.Op op) { 7819c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran return native_clipRegion(mNativeCanvasWrapper, region.ni(), op.nativeInt); 7829c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran } 7835009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran 7845009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran /** 7859c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * Intersect the current clip with the specified region. Note that unlike 7869c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran * clipRect() and clipPath() which transform their arguments by the 7875009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * current matrix, clipRegion() assumes its argument is already in the 7885009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * coordinate system of the current layer's bitmap, and so not 7895009d5ef3fbcdc69d772b528fd22184b7d605afaSreeram Ramachandran * transformation is performed. 7905c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * 7917619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran * @param region The region to operate on the current clip, based on op 792f4f6c8de3f091be4b91a5a9d7f14e8882ec6d502Sreeram Ramachandran * @return true if the resulting is non-empty 793eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * 794eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran * @deprecated Unlike all other clip calls this API does not respect the 7957619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran * current matrix. Use {@link #clipRect(Rect)} as an alternative. 7967619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran */ 797f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti public boolean clipRegion(@NonNull Region region) { 798eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran return clipRegion(region, Region.Op.INTERSECT); 799eb27b7ec10faf47a93fbc2863092cc667b05e252Sreeram Ramachandran } 8007619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran 801 public @Nullable DrawFilter getDrawFilter() { 802 return mDrawFilter; 803 } 804 805 public void setDrawFilter(@Nullable DrawFilter filter) { 806 long nativeFilter = 0; 807 if (filter != null) { 808 nativeFilter = filter.mNativeInt; 809 } 810 mDrawFilter = filter; 811 nativeSetDrawFilter(mNativeCanvasWrapper, nativeFilter); 812 } 813 814 public enum EdgeType { 815 816 /** 817 * Black-and-White: Treat edges by just rounding to nearest pixel boundary 818 */ 819 BW(0), //!< treat edges by just rounding to nearest pixel boundary 820 821 /** 822 * Antialiased: Treat edges by rounding-out, since they may be antialiased 823 */ 824 AA(1); 825 826 EdgeType(int nativeInt) { 827 this.nativeInt = nativeInt; 828 } 829 830 /** 831 * @hide 832 */ 833 public final int nativeInt; 834 } 835 836 /** 837 * Return true if the specified rectangle, after being transformed by the 838 * current matrix, would lie completely outside of the current clip. Call 839 * this to check if an area you intend to draw into is clipped out (and 840 * therefore you can skip making the draw calls). 841 * 842 * @param rect the rect to compare with the current clip 843 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 844 * since that means it may affect a larger area (more pixels) than 845 * non-antialiased ({@link Canvas.EdgeType#BW}). 846 * @return true if the rect (transformed by the canvas' matrix) 847 * does not intersect with the canvas' clip 848 */ 849 public boolean quickReject(@NonNull RectF rect, @NonNull EdgeType type) { 850 return native_quickReject(mNativeCanvasWrapper, 851 rect.left, rect.top, rect.right, rect.bottom); 852 } 853 854 /** 855 * Return true if the specified path, after being transformed by the 856 * current matrix, would lie completely outside of the current clip. Call 857 * this to check if an area you intend to draw into is clipped out (and 858 * therefore you can skip making the draw calls). Note: for speed it may 859 * return false even if the path itself might not intersect the clip 860 * (i.e. the bounds of the path intersects, but the path does not). 861 * 862 * @param path The path to compare with the current clip 863 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 864 * since that means it may affect a larger area (more pixels) than 865 * non-antialiased ({@link Canvas.EdgeType#BW}). 866 * @return true if the path (transformed by the canvas' matrix) 867 * does not intersect with the canvas' clip 868 */ 869 public boolean quickReject(@NonNull Path path, @NonNull EdgeType type) { 870 return native_quickReject(mNativeCanvasWrapper, path.ni()); 871 } 872 873 /** 874 * Return true if the specified rectangle, after being transformed by the 875 * current matrix, would lie completely outside of the current clip. Call 876 * this to check if an area you intend to draw into is clipped out (and 877 * therefore you can skip making the draw calls). 878 * 879 * @param left The left side of the rectangle to compare with the 880 * current clip 881 * @param top The top of the rectangle to compare with the current 882 * clip 883 * @param right The right side of the rectangle to compare with the 884 * current clip 885 * @param bottom The bottom of the rectangle to compare with the 886 * current clip 887 * @param type {@link Canvas.EdgeType#AA} if the path should be considered antialiased, 888 * since that means it may affect a larger area (more pixels) than 889 * non-antialiased ({@link Canvas.EdgeType#BW}). 890 * @return true if the rect (transformed by the canvas' matrix) 891 * does not intersect with the canvas' clip 892 */ 893 public boolean quickReject(float left, float top, float right, float bottom, 894 @NonNull EdgeType type) { 895 return native_quickReject(mNativeCanvasWrapper, left, top, right, bottom); 896 } 897 898 /** 899 * Return the bounds of the current clip (in local coordinates) in the 900 * bounds parameter, and return true if it is non-empty. This can be useful 901 * in a way similar to quickReject, in that it tells you that drawing 902 * outside of these bounds will be clipped out. 903 * 904 * @param bounds Return the clip bounds here. If it is null, ignore it but 905 * still return true if the current clip is non-empty. 906 * @return true if the current clip is non-empty. 907 */ 908 public boolean getClipBounds(@Nullable Rect bounds) { 909 return native_getClipBounds(mNativeCanvasWrapper, bounds); 910 } 911 912 /** 913 * Retrieve the bounds of the current clip (in local coordinates). 914 * 915 * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty. 916 */ 917 public final @NonNull Rect getClipBounds() { 918 Rect r = new Rect(); 919 getClipBounds(r); 920 return r; 921 } 922 923 /** 924 * Fill the entire canvas' bitmap (restricted to the current clip) with the 925 * specified RGB color, using srcover porterduff mode. 926 * 927 * @param r red component (0..255) of the color to draw onto the canvas 928 * @param g green component (0..255) of the color to draw onto the canvas 929 * @param b blue component (0..255) of the color to draw onto the canvas 930 */ 931 public void drawRGB(int r, int g, int b) { 932 drawColor(Color.rgb(r, g, b)); 933 } 934 935 /** 936 * Fill the entire canvas' bitmap (restricted to the current clip) with the 937 * specified ARGB color, using srcover porterduff mode. 938 * 939 * @param a alpha component (0..255) of the color to draw onto the canvas 940 * @param r red component (0..255) of the color to draw onto the canvas 941 * @param g green component (0..255) of the color to draw onto the canvas 942 * @param b blue component (0..255) of the color to draw onto the canvas 943 */ 944 public void drawARGB(int a, int r, int g, int b) { 945 drawColor(Color.argb(a, r, g, b)); 946 } 947 948 /** 949 * Fill the entire canvas' bitmap (restricted to the current clip) with the 950 * specified color, using srcover porterduff mode. 951 * 952 * @param color the color to draw onto the canvas 953 */ 954 public void drawColor(int color) { 955 native_drawColor(mNativeCanvasWrapper, color, PorterDuff.Mode.SRC_OVER.nativeInt); 956 } 957 958 /** 959 * Fill the entire canvas' bitmap (restricted to the current clip) with the 960 * specified color and porter-duff xfermode. 961 * 962 * @param color the color to draw with 963 * @param mode the porter-duff mode to apply to the color 964 */ 965 public void drawColor(int color, @NonNull PorterDuff.Mode mode) { 966 native_drawColor(mNativeCanvasWrapper, color, mode.nativeInt); 967 } 968 969 /** 970 * Fill the entire canvas' bitmap (restricted to the current clip) with 971 * the specified paint. This is equivalent (but faster) to drawing an 972 * infinitely large rectangle with the specified paint. 973 * 974 * @param paint The paint used to draw onto the canvas 975 */ 976 public void drawPaint(@NonNull Paint paint) { 977 native_drawPaint(mNativeCanvasWrapper, paint.getNativeInstance()); 978 } 979 980 /** 981 * Draw a series of points. Each point is centered at the coordinate 982 * specified by pts[], and its diameter is specified by the paint's stroke 983 * width (as transformed by the canvas' CTM), with special treatment for 984 * a stroke width of 0, which always draws exactly 1 pixel (or at most 4 985 * if antialiasing is enabled). The shape of the point is controlled by 986 * the paint's Cap type. The shape is a square, unless the cap type is 987 * Round, in which case the shape is a circle. 988 * 989 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 990 * @param offset Number of values to skip before starting to draw. 991 * @param count The number of values to process, after skipping offset 992 * of them. Since one point uses two values, the number of 993 * "points" that are drawn is really (count >> 1). 994 * @param paint The paint used to draw the points 995 */ 996 public void drawPoints(float[] pts, int offset, int count, @NonNull Paint paint) { 997 native_drawPoints(mNativeCanvasWrapper, pts, offset, count, paint.getNativeInstance()); 998 } 999 1000 /** 1001 * Helper for drawPoints() that assumes you want to draw the entire array 1002 */ 1003 public void drawPoints(@NonNull float[] pts, @NonNull Paint paint) { 1004 drawPoints(pts, 0, pts.length, paint); 1005 } 1006 1007 /** 1008 * Helper for drawPoints() for drawing a single point. 1009 */ 1010 public void drawPoint(float x, float y, @NonNull Paint paint) { 1011 native_drawPoint(mNativeCanvasWrapper, x, y, paint.getNativeInstance()); 1012 } 1013 1014 /** 1015 * Draw a line segment with the specified start and stop x,y coordinates, 1016 * using the specified paint. 1017 * 1018 * <p>Note that since a line is always "framed", the Style is ignored in the paint.</p> 1019 * 1020 * <p>Degenerate lines (length is 0) will not be drawn.</p> 1021 * 1022 * @param startX The x-coordinate of the start point of the line 1023 * @param startY The y-coordinate of the start point of the line 1024 * @param paint The paint used to draw the line 1025 */ 1026 public void drawLine(float startX, float startY, float stopX, float stopY, 1027 @NonNull Paint paint) { 1028 native_drawLine(mNativeCanvasWrapper, startX, startY, stopX, stopY, paint.getNativeInstance()); 1029 } 1030 1031 /** 1032 * Draw a series of lines. Each line is taken from 4 consecutive values 1033 * in the pts array. Thus to draw 1 line, the array must contain at least 4 1034 * values. This is logically the same as drawing the array as follows: 1035 * drawLine(pts[0], pts[1], pts[2], pts[3]) followed by 1036 * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on. 1037 * 1038 * @param pts Array of points to draw [x0 y0 x1 y1 x2 y2 ...] 1039 * @param offset Number of values in the array to skip before drawing. 1040 * @param count The number of values in the array to process, after 1041 * skipping "offset" of them. Since each line uses 4 values, 1042 * the number of "lines" that are drawn is really 1043 * (count >> 2). 1044 * @param paint The paint used to draw the points 1045 */ 1046 public void drawLines(float[] pts, int offset, int count, Paint paint) { 1047 native_drawLines(mNativeCanvasWrapper, pts, offset, count, paint.getNativeInstance()); 1048 } 1049 1050 public void drawLines(@NonNull float[] pts, @NonNull Paint paint) { 1051 drawLines(pts, 0, pts.length, paint); 1052 } 1053 1054 /** 1055 * Draw the specified Rect using the specified paint. The rectangle will 1056 * be filled or framed based on the Style in the paint. 1057 * 1058 * @param rect The rect to be drawn 1059 * @param paint The paint used to draw the rect 1060 */ 1061 public void drawRect(@NonNull RectF rect, @NonNull Paint paint) { 1062 native_drawRect(mNativeCanvasWrapper, 1063 rect.left, rect.top, rect.right, rect.bottom, paint.getNativeInstance()); 1064 } 1065 1066 /** 1067 * Draw the specified Rect using the specified Paint. The rectangle 1068 * will be filled or framed based on the Style in the paint. 1069 * 1070 * @param r The rectangle to be drawn. 1071 * @param paint The paint used to draw the rectangle 1072 */ 1073 public void drawRect(@NonNull Rect r, @NonNull Paint paint) { 1074 drawRect(r.left, r.top, r.right, r.bottom, paint); 1075 } 1076 1077 1078 /** 1079 * Draw the specified Rect using the specified paint. The rectangle will 1080 * be filled or framed based on the Style in the paint. 1081 * 1082 * @param left The left side of the rectangle to be drawn 1083 * @param top The top side of the rectangle to be drawn 1084 * @param right The right side of the rectangle to be drawn 1085 * @param bottom The bottom side of the rectangle to be drawn 1086 * @param paint The paint used to draw the rect 1087 */ 1088 public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) { 1089 native_drawRect(mNativeCanvasWrapper, left, top, right, bottom, paint.getNativeInstance()); 1090 } 1091 1092 /** 1093 * Draw the specified oval using the specified paint. The oval will be 1094 * filled or framed based on the Style in the paint. 1095 * 1096 * @param oval The rectangle bounds of the oval to be drawn 1097 */ 1098 public void drawOval(@NonNull RectF oval, @NonNull Paint paint) { 1099 if (oval == null) { 1100 throw new NullPointerException(); 1101 } 1102 drawOval(oval.left, oval.top, oval.right, oval.bottom, paint); 1103 } 1104 1105 /** 1106 * Draw the specified oval using the specified paint. The oval will be 1107 * filled or framed based on the Style in the paint. 1108 */ 1109 public void drawOval(float left, float top, float right, float bottom, @NonNull Paint paint) { 1110 native_drawOval(mNativeCanvasWrapper, left, top, right, bottom, paint.getNativeInstance()); 1111 } 1112 1113 /** 1114 * Draw the specified circle using the specified paint. If radius is <= 0, 1115 * then nothing will be drawn. The circle will be filled or framed based 1116 * on the Style in the paint. 1117 * 1118 * @param cx The x-coordinate of the center of the cirle to be drawn 1119 * @param cy The y-coordinate of the center of the cirle to be drawn 1120 * @param radius The radius of the cirle to be drawn 1121 * @param paint The paint used to draw the circle 1122 */ 1123 public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) { 1124 native_drawCircle(mNativeCanvasWrapper, cx, cy, radius, paint.getNativeInstance()); 1125 } 1126 1127 /** 1128 * <p>Draw the specified arc, which will be scaled to fit inside the 1129 * specified oval.</p> 1130 * 1131 * <p>If the start angle is negative or >= 360, the start angle is treated 1132 * as start angle modulo 360.</p> 1133 * 1134 * <p>If the sweep angle is >= 360, then the oval is drawn 1135 * completely. Note that this differs slightly from SkPath::arcTo, which 1136 * treats the sweep angle modulo 360. If the sweep angle is negative, 1137 * the sweep angle is treated as sweep angle modulo 360</p> 1138 * 1139 * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the 1140 * geometric angle of 0 degrees (3 o'clock on a watch.)</p> 1141 * 1142 * @param oval The bounds of oval used to define the shape and size 1143 * of the arc 1144 * @param startAngle Starting angle (in degrees) where the arc begins 1145 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1146 * @param useCenter If true, include the center of the oval in the arc, and 1147 close it if it is being stroked. This will draw a wedge 1148 * @param paint The paint used to draw the arc 1149 */ 1150 public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, 1151 @NonNull Paint paint) { 1152 drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter, 1153 paint); 1154 } 1155 1156 /** 1157 * <p>Draw the specified arc, which will be scaled to fit inside the 1158 * specified oval.</p> 1159 * 1160 * <p>If the start angle is negative or >= 360, the start angle is treated 1161 * as start angle modulo 360.</p> 1162 * 1163 * <p>If the sweep angle is >= 360, then the oval is drawn 1164 * completely. Note that this differs slightly from SkPath::arcTo, which 1165 * treats the sweep angle modulo 360. If the sweep angle is negative, 1166 * the sweep angle is treated as sweep angle modulo 360</p> 1167 * 1168 * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the 1169 * geometric angle of 0 degrees (3 o'clock on a watch.)</p> 1170 * 1171 * @param startAngle Starting angle (in degrees) where the arc begins 1172 * @param sweepAngle Sweep angle (in degrees) measured clockwise 1173 * @param useCenter If true, include the center of the oval in the arc, and 1174 close it if it is being stroked. This will draw a wedge 1175 * @param paint The paint used to draw the arc 1176 */ 1177 public void drawArc(float left, float top, float right, float bottom, float startAngle, 1178 float sweepAngle, boolean useCenter, @NonNull Paint paint) { 1179 native_drawArc(mNativeCanvasWrapper, left, top, right, bottom, startAngle, sweepAngle, 1180 useCenter, paint.getNativeInstance()); 1181 } 1182 1183 /** 1184 * Draw the specified round-rect using the specified paint. The roundrect 1185 * will be filled or framed based on the Style in the paint. 1186 * 1187 * @param rect The rectangular bounds of the roundRect to be drawn 1188 * @param rx The x-radius of the oval used to round the corners 1189 * @param ry The y-radius of the oval used to round the corners 1190 * @param paint The paint used to draw the roundRect 1191 */ 1192 public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { 1193 drawRoundRect(rect.left, rect.top, rect.right, rect.bottom, rx, ry, paint); 1194 } 1195 1196 /** 1197 * Draw the specified round-rect using the specified paint. The roundrect 1198 * will be filled or framed based on the Style in the paint. 1199 * 1200 * @param rx The x-radius of the oval used to round the corners 1201 * @param ry The y-radius of the oval used to round the corners 1202 * @param paint The paint used to draw the roundRect 1203 */ 1204 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 1205 @NonNull Paint paint) { 1206 native_drawRoundRect(mNativeCanvasWrapper, left, top, right, bottom, rx, ry, paint.getNativeInstance()); 1207 } 1208 1209 /** 1210 * Draw the specified path using the specified paint. The path will be 1211 * filled or framed based on the Style in the paint. 1212 * 1213 * @param path The path to be drawn 1214 * @param paint The paint used to draw the path 1215 */ 1216 public void drawPath(@NonNull Path path, @NonNull Paint paint) { 1217 native_drawPath(mNativeCanvasWrapper, path.ni(), paint.getNativeInstance()); 1218 } 1219 1220 /** 1221 * @hide 1222 */ 1223 protected static void throwIfCannotDraw(Bitmap bitmap) { 1224 if (bitmap.isRecycled()) { 1225 throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap); 1226 } 1227 if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 && 1228 bitmap.hasAlpha()) { 1229 throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap " 1230 + bitmap); 1231 } 1232 } 1233 1234 /** 1235 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1236 * 1237 * @param patch The ninepatch object to render 1238 * @param dst The destination rectangle. 1239 * @param paint The paint to draw the bitmap with. may be null 1240 * 1241 * @hide 1242 */ 1243 public void drawPatch(@NonNull NinePatch patch, @NonNull Rect dst, @Nullable Paint paint) { 1244 patch.drawSoftware(this, dst, paint); 1245 } 1246 1247 /** 1248 * Draws the specified bitmap as an N-patch (most often, a 9-patches.) 1249 * 1250 * @param patch The ninepatch object to render 1251 * @param dst The destination rectangle. 1252 * @param paint The paint to draw the bitmap with. may be null 1253 * 1254 * @hide 1255 */ 1256 public void drawPatch(@NonNull NinePatch patch, @NonNull RectF dst, @Nullable Paint paint) { 1257 patch.drawSoftware(this, dst, paint); 1258 } 1259 1260 /** 1261 * Draw the specified bitmap, with its top/left corner at (x,y), using 1262 * the specified paint, transformed by the current matrix. 1263 * 1264 * <p>Note: if the paint contains a maskfilter that generates a mask which 1265 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1266 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1267 * Thus the color outside of the original width/height will be the edge 1268 * color replicated. 1269 * 1270 * <p>If the bitmap and canvas have different densities, this function 1271 * will take care of automatically scaling the bitmap to draw at the 1272 * same density as the canvas. 1273 * 1274 * @param bitmap The bitmap to be drawn 1275 * @param left The position of the left side of the bitmap being drawn 1276 * @param top The position of the top side of the bitmap being drawn 1277 * @param paint The paint used to draw the bitmap (may be null) 1278 */ 1279 public void drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint) { 1280 throwIfCannotDraw(bitmap); 1281 native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top, 1282 paint != null ? paint.getNativeInstance() : 0, mDensity, mScreenDensity, bitmap.mDensity); 1283 } 1284 1285 /** 1286 * Draw the specified bitmap, scaling/translating automatically to fill 1287 * the destination rectangle. If the source rectangle is not null, it 1288 * specifies the subset of the bitmap to draw. 1289 * 1290 * <p>Note: if the paint contains a maskfilter that generates a mask which 1291 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1292 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1293 * Thus the color outside of the original width/height will be the edge 1294 * color replicated. 1295 * 1296 * <p>This function <em>ignores the density associated with the bitmap</em>. 1297 * This is because the source and destination rectangle coordinate 1298 * spaces are in their respective densities, so must already have the 1299 * appropriate scaling factor applied. 1300 * 1301 * @param bitmap The bitmap to be drawn 1302 * @param src May be null. The subset of the bitmap to be drawn 1303 * @param dst The rectangle that the bitmap will be scaled/translated 1304 * to fit into 1305 * @param paint May be null. The paint used to draw the bitmap 1306 */ 1307 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, 1308 @Nullable Paint paint) { 1309 if (dst == null) { 1310 throw new NullPointerException(); 1311 } 1312 throwIfCannotDraw(bitmap); 1313 final long nativePaint = paint == null ? 0 : paint.getNativeInstance(); 1314 1315 float left, top, right, bottom; 1316 if (src == null) { 1317 left = top = 0; 1318 right = bitmap.getWidth(); 1319 bottom = bitmap.getHeight(); 1320 } else { 1321 left = src.left; 1322 right = src.right; 1323 top = src.top; 1324 bottom = src.bottom; 1325 } 1326 1327 native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top, right, bottom, 1328 dst.left, dst.top, dst.right, dst.bottom, nativePaint, mScreenDensity, 1329 bitmap.mDensity); 1330 } 1331 1332 /** 1333 * Draw the specified bitmap, scaling/translating automatically to fill 1334 * the destination rectangle. If the source rectangle is not null, it 1335 * specifies the subset of the bitmap to draw. 1336 * 1337 * <p>Note: if the paint contains a maskfilter that generates a mask which 1338 * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), 1339 * then the bitmap will be drawn as if it were in a Shader with CLAMP mode. 1340 * Thus the color outside of the original width/height will be the edge 1341 * color replicated. 1342 * 1343 * <p>This function <em>ignores the density associated with the bitmap</em>. 1344 * This is because the source and destination rectangle coordinate 1345 * spaces are in their respective densities, so must already have the 1346 * appropriate scaling factor applied. 1347 * 1348 * @param bitmap The bitmap to be drawn 1349 * @param src May be null. The subset of the bitmap to be drawn 1350 * @param dst The rectangle that the bitmap will be scaled/translated 1351 * to fit into 1352 * @param paint May be null. The paint used to draw the bitmap 1353 */ 1354 public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, 1355 @Nullable Paint paint) { 1356 if (dst == null) { 1357 throw new NullPointerException(); 1358 } 1359 throwIfCannotDraw(bitmap); 1360 final long nativePaint = paint == null ? 0 : paint.getNativeInstance(); 1361 1362 int left, top, right, bottom; 1363 if (src == null) { 1364 left = top = 0; 1365 right = bitmap.getWidth(); 1366 bottom = bitmap.getHeight(); 1367 } else { 1368 left = src.left; 1369 right = src.right; 1370 top = src.top; 1371 bottom = src.bottom; 1372 } 1373 1374 native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top, right, bottom, 1375 dst.left, dst.top, dst.right, dst.bottom, nativePaint, mScreenDensity, 1376 bitmap.mDensity); 1377 } 1378 1379 /** 1380 * Treat the specified array of colors as a bitmap, and draw it. This gives 1381 * the same result as first creating a bitmap from the array, and then 1382 * drawing it, but this method avoids explicitly creating a bitmap object 1383 * which can be more efficient if the colors are changing often. 1384 * 1385 * @param colors Array of colors representing the pixels of the bitmap 1386 * @param offset Offset into the array of colors for the first pixel 1387 * @param stride The number of colors in the array between rows (must be 1388 * >= width or <= -width). 1389 * @param x The X coordinate for where to draw the bitmap 1390 * @param y The Y coordinate for where to draw the bitmap 1391 * @param width The width of the bitmap 1392 * @param height The height of the bitmap 1393 * @param hasAlpha True if the alpha channel of the colors contains valid 1394 * values. If false, the alpha byte is ignored (assumed to 1395 * be 0xFF for every pixel). 1396 * @param paint May be null. The paint used to draw the bitmap 1397 * 1398 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1399 * requires an internal copy of color buffer contents every time this method is called. Using a 1400 * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime 1401 * and copies of pixel data. 1402 */ 1403 @Deprecated 1404 public void drawBitmap(@NonNull int[] colors, int offset, int stride, float x, float y, 1405 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1406 // check for valid input 1407 if (width < 0) { 1408 throw new IllegalArgumentException("width must be >= 0"); 1409 } 1410 if (height < 0) { 1411 throw new IllegalArgumentException("height must be >= 0"); 1412 } 1413 if (Math.abs(stride) < width) { 1414 throw new IllegalArgumentException("abs(stride) must be >= width"); 1415 } 1416 int lastScanline = offset + (height - 1) * stride; 1417 int length = colors.length; 1418 if (offset < 0 || (offset + width > length) || lastScanline < 0 1419 || (lastScanline + width > length)) { 1420 throw new ArrayIndexOutOfBoundsException(); 1421 } 1422 // quick escape if there's nothing to draw 1423 if (width == 0 || height == 0) { 1424 return; 1425 } 1426 // punch down to native for the actual draw 1427 native_drawBitmap(mNativeCanvasWrapper, colors, offset, stride, x, y, width, height, hasAlpha, 1428 paint != null ? paint.getNativeInstance() : 0); 1429 } 1430 1431 /** 1432 * Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y 1433 * 1434 * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas 1435 * requires an internal copy of color buffer contents every time this method is called. Using a 1436 * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime 1437 * and copies of pixel data. 1438 */ 1439 @Deprecated 1440 public void drawBitmap(@NonNull int[] colors, int offset, int stride, int x, int y, 1441 int width, int height, boolean hasAlpha, @Nullable Paint paint) { 1442 // call through to the common float version 1443 drawBitmap(colors, offset, stride, (float)x, (float)y, width, height, 1444 hasAlpha, paint); 1445 } 1446 1447 /** 1448 * Draw the bitmap using the specified matrix. 1449 * 1450 * @param bitmap The bitmap to draw 1451 * @param matrix The matrix used to transform the bitmap when it is drawn 1452 * @param paint May be null. The paint used to draw the bitmap 1453 */ 1454 public void drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint) { 1455 nativeDrawBitmapMatrix(mNativeCanvasWrapper, bitmap.ni(), matrix.ni(), 1456 paint != null ? paint.getNativeInstance() : 0); 1457 } 1458 1459 /** 1460 * @hide 1461 */ 1462 protected static void checkRange(int length, int offset, int count) { 1463 if ((offset | count) < 0 || offset + count > length) { 1464 throw new ArrayIndexOutOfBoundsException(); 1465 } 1466 } 1467 1468 /** 1469 * Draw the bitmap through the mesh, where mesh vertices are evenly 1470 * distributed across the bitmap. There are meshWidth+1 vertices across, and 1471 * meshHeight+1 vertices down. The verts array is accessed in row-major 1472 * order, so that the first meshWidth+1 vertices are distributed across the 1473 * top of the bitmap from left to right. A more general version of this 1474 * method is drawVertices(). 1475 * 1476 * @param bitmap The bitmap to draw using the mesh 1477 * @param meshWidth The number of columns in the mesh. Nothing is drawn if 1478 * this is 0 1479 * @param meshHeight The number of rows in the mesh. Nothing is drawn if 1480 * this is 0 1481 * @param verts Array of x,y pairs, specifying where the mesh should be 1482 * drawn. There must be at least 1483 * (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values 1484 * in the array 1485 * @param vertOffset Number of verts elements to skip before drawing 1486 * @param colors May be null. Specifies a color at each vertex, which is 1487 * interpolated across the cell, and whose values are 1488 * multiplied by the corresponding bitmap colors. If not null, 1489 * there must be at least (meshWidth+1) * (meshHeight+1) + 1490 * colorOffset values in the array. 1491 * @param colorOffset Number of color elements to skip before drawing 1492 * @param paint May be null. The paint used to draw the bitmap 1493 */ 1494 public void drawBitmapMesh(@NonNull Bitmap bitmap, int meshWidth, int meshHeight, 1495 @NonNull float[] verts, int vertOffset, @Nullable int[] colors, int colorOffset, 1496 @Nullable Paint paint) { 1497 if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) { 1498 throw new ArrayIndexOutOfBoundsException(); 1499 } 1500 if (meshWidth == 0 || meshHeight == 0) { 1501 return; 1502 } 1503 int count = (meshWidth + 1) * (meshHeight + 1); 1504 // we mul by 2 since we need two floats per vertex 1505 checkRange(verts.length, vertOffset, count * 2); 1506 if (colors != null) { 1507 // no mul by 2, since we need only 1 color per vertex 1508 checkRange(colors.length, colorOffset, count); 1509 } 1510 nativeDrawBitmapMesh(mNativeCanvasWrapper, bitmap.ni(), meshWidth, meshHeight, 1511 verts, vertOffset, colors, colorOffset, 1512 paint != null ? paint.getNativeInstance() : 0); 1513 } 1514 1515 public enum VertexMode { 1516 TRIANGLES(0), 1517 TRIANGLE_STRIP(1), 1518 TRIANGLE_FAN(2); 1519 1520 VertexMode(int nativeInt) { 1521 this.nativeInt = nativeInt; 1522 } 1523 1524 /** 1525 * @hide 1526 */ 1527 public final int nativeInt; 1528 } 1529 1530 /** 1531 * Draw the array of vertices, interpreted as triangles (based on mode). The 1532 * verts array is required, and specifies the x,y pairs for each vertex. If 1533 * texs is non-null, then it is used to specify the coordinate in shader 1534 * coordinates to use at each vertex (the paint must have a shader in this 1535 * case). If there is no texs array, but there is a color array, then each 1536 * color is interpolated across its corresponding triangle in a gradient. If 1537 * both texs and colors arrays are present, then they behave as before, but 1538 * the resulting color at each pixels is the result of multiplying the 1539 * colors from the shader and the color-gradient together. The indices array 1540 * is optional, but if it is present, then it is used to specify the index 1541 * of each triangle, rather than just walking through the arrays in order. 1542 * 1543 * @param mode How to interpret the array of vertices 1544 * @param vertexCount The number of values in the vertices array (and 1545 * corresponding texs and colors arrays if non-null). Each logical 1546 * vertex is two values (x, y), vertexCount must be a multiple of 2. 1547 * @param verts Array of vertices for the mesh 1548 * @param vertOffset Number of values in the verts to skip before drawing. 1549 * @param texs May be null. If not null, specifies the coordinates to sample 1550 * into the current shader (e.g. bitmap tile or gradient) 1551 * @param texOffset Number of values in texs to skip before drawing. 1552 * @param colors May be null. If not null, specifies a color for each 1553 * vertex, to be interpolated across the triangle. 1554 * @param colorOffset Number of values in colors to skip before drawing. 1555 * @param indices If not null, array of indices to reference into the 1556 * vertex (texs, colors) array. 1557 * @param indexCount number of entries in the indices array (if not null). 1558 * @param paint Specifies the shader to use if the texs array is non-null. 1559 */ 1560 public void drawVertices(@NonNull VertexMode mode, int vertexCount, @NonNull float[] verts, 1561 int vertOffset, @Nullable float[] texs, int texOffset, @Nullable int[] colors, 1562 int colorOffset, @Nullable short[] indices, int indexOffset, int indexCount, 1563 @NonNull Paint paint) { 1564 checkRange(verts.length, vertOffset, vertexCount); 1565 if (texs != null) { 1566 checkRange(texs.length, texOffset, vertexCount); 1567 } 1568 if (colors != null) { 1569 checkRange(colors.length, colorOffset, vertexCount / 2); 1570 } 1571 if (indices != null) { 1572 checkRange(indices.length, indexOffset, indexCount); 1573 } 1574 nativeDrawVertices(mNativeCanvasWrapper, mode.nativeInt, vertexCount, verts, 1575 vertOffset, texs, texOffset, colors, colorOffset, 1576 indices, indexOffset, indexCount, paint.getNativeInstance()); 1577 } 1578 1579 /** 1580 * Draw the text, with origin at (x,y), using the specified paint. The 1581 * origin is interpreted based on the Align setting in the paint. 1582 * 1583 * @param text The text to be drawn 1584 * @param x The x-coordinate of the origin of the text being drawn 1585 * @param y The y-coordinate of the origin of the text being drawn 1586 * @param paint The paint used for the text (e.g. color, size, style) 1587 */ 1588 public void drawText(@NonNull char[] text, int index, int count, float x, float y, 1589 @NonNull Paint paint) { 1590 if ((index | count | (index + count) | 1591 (text.length - index - count)) < 0) { 1592 throw new IndexOutOfBoundsException(); 1593 } 1594 native_drawText(mNativeCanvasWrapper, text, index, count, x, y, paint.mBidiFlags, 1595 paint.getNativeInstance(), paint.mNativeTypeface); 1596 } 1597 1598 /** 1599 * Draw the text, with origin at (x,y), using the specified paint. The 1600 * origin is interpreted based on the Align setting in the paint. 1601 * 1602 * @param text The text to be drawn 1603 * @param x The x-coordinate of the origin of the text being drawn 1604 * @param y The y-coordinate of the origin of the text being drawn 1605 * @param paint The paint used for the text (e.g. color, size, style) 1606 */ 1607 public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { 1608 native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags, 1609 paint.getNativeInstance(), paint.mNativeTypeface); 1610 } 1611 1612 /** 1613 * Draw the text, with origin at (x,y), using the specified paint. 1614 * The origin is interpreted based on the Align setting in the paint. 1615 * 1616 * @param text The text to be drawn 1617 * @param start The index of the first character in text to draw 1618 * @param end (end - 1) is the index of the last character in text to draw 1619 * @param x The x-coordinate of the origin of the text being drawn 1620 * @param y The y-coordinate of the origin of the text being drawn 1621 * @param paint The paint used for the text (e.g. color, size, style) 1622 */ 1623 public void drawText(@NonNull String text, int start, int end, float x, float y, 1624 @NonNull Paint paint) { 1625 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1626 throw new IndexOutOfBoundsException(); 1627 } 1628 native_drawText(mNativeCanvasWrapper, text, start, end, x, y, paint.mBidiFlags, 1629 paint.getNativeInstance(), paint.mNativeTypeface); 1630 } 1631 1632 /** 1633 * Draw the specified range of text, specified by start/end, with its 1634 * origin at (x,y), in the specified Paint. The origin is interpreted 1635 * based on the Align setting in the Paint. 1636 * 1637 * @param text The text to be drawn 1638 * @param start The index of the first character in text to draw 1639 * @param end (end - 1) is the index of the last character in text 1640 * to draw 1641 * @param x The x-coordinate of origin for where to draw the text 1642 * @param y The y-coordinate of origin for where to draw the text 1643 * @param paint The paint used for the text (e.g. color, size, style) 1644 */ 1645 public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, 1646 @NonNull Paint paint) { 1647 if (text instanceof String || text instanceof SpannedString || 1648 text instanceof SpannableString) { 1649 native_drawText(mNativeCanvasWrapper, text.toString(), start, end, x, y, 1650 paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); 1651 } else if (text instanceof GraphicsOperations) { 1652 ((GraphicsOperations) text).drawText(this, start, end, x, y, 1653 paint); 1654 } else { 1655 char[] buf = TemporaryBuffer.obtain(end - start); 1656 TextUtils.getChars(text, start, end, buf, 0); 1657 native_drawText(mNativeCanvasWrapper, buf, 0, end - start, x, y, 1658 paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); 1659 TemporaryBuffer.recycle(buf); 1660 } 1661 } 1662 1663 /** 1664 * Render a run of all LTR or all RTL text, with shaping. This does not run 1665 * bidi on the provided text, but renders it as a uniform right-to-left or 1666 * left-to-right run, as indicated by dir. Alignment of the text is as 1667 * determined by the Paint's TextAlign value. 1668 * 1669 * @param text the text to render 1670 * @param index the start of the text to render 1671 * @param count the count of chars to render 1672 * @param contextIndex the start of the context for shaping. Must be 1673 * no greater than index. 1674 * @param contextCount the number of characters in the context for shaping. 1675 * ContexIndex + contextCount must be no less than index 1676 * + count. 1677 * @param x the x position at which to draw the text 1678 * @param y the y position at which to draw the text 1679 * @param isRtl whether the run is in RTL direction 1680 * @param paint the paint 1681 * @hide 1682 */ 1683 public void drawTextRun(@NonNull char[] text, int index, int count, int contextIndex, 1684 int contextCount, float x, float y, boolean isRtl, @NonNull Paint paint) { 1685 1686 if (text == null) { 1687 throw new NullPointerException("text is null"); 1688 } 1689 if (paint == null) { 1690 throw new NullPointerException("paint is null"); 1691 } 1692 if ((index | count | text.length - index - count) < 0) { 1693 throw new IndexOutOfBoundsException(); 1694 } 1695 1696 native_drawTextRun(mNativeCanvasWrapper, text, index, count, 1697 contextIndex, contextCount, x, y, isRtl, paint.getNativeInstance(), paint.mNativeTypeface); 1698 } 1699 1700 /** 1701 * Render a run of all LTR or all RTL text, with shaping. This does not run 1702 * bidi on the provided text, but renders it as a uniform right-to-left or 1703 * left-to-right run, as indicated by dir. Alignment of the text is as 1704 * determined by the Paint's TextAlign value. 1705 * 1706 * @param text the text to render 1707 * @param start the start of the text to render. Data before this position 1708 * can be used for shaping context. 1709 * @param end the end of the text to render. Data at or after this 1710 * position can be used for shaping context. 1711 * @param x the x position at which to draw the text 1712 * @param y the y position at which to draw the text 1713 * @param isRtl whether the run is in RTL direction 1714 * @param paint the paint 1715 * @hide 1716 */ 1717 public void drawTextRun(@NonNull CharSequence text, int start, int end, int contextStart, 1718 int contextEnd, float x, float y, boolean isRtl, @NonNull Paint paint) { 1719 1720 if (text == null) { 1721 throw new NullPointerException("text is null"); 1722 } 1723 if (paint == null) { 1724 throw new NullPointerException("paint is null"); 1725 } 1726 if ((start | end | end - start | text.length() - end) < 0) { 1727 throw new IndexOutOfBoundsException(); 1728 } 1729 1730 if (text instanceof String || text instanceof SpannedString || 1731 text instanceof SpannableString) { 1732 native_drawTextRun(mNativeCanvasWrapper, text.toString(), start, end, 1733 contextStart, contextEnd, x, y, isRtl, paint.getNativeInstance(), paint.mNativeTypeface); 1734 } else if (text instanceof GraphicsOperations) { 1735 ((GraphicsOperations) text).drawTextRun(this, start, end, 1736 contextStart, contextEnd, x, y, isRtl, paint); 1737 } else { 1738 int contextLen = contextEnd - contextStart; 1739 int len = end - start; 1740 char[] buf = TemporaryBuffer.obtain(contextLen); 1741 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1742 native_drawTextRun(mNativeCanvasWrapper, buf, start - contextStart, len, 1743 0, contextLen, x, y, isRtl, paint.getNativeInstance(), paint.mNativeTypeface); 1744 TemporaryBuffer.recycle(buf); 1745 } 1746 } 1747 1748 /** 1749 * Draw the text in the array, with each character's origin specified by 1750 * the pos array. 1751 * 1752 * This method does not support glyph composition and decomposition and 1753 * should therefore not be used to render complex scripts. It also doesn't 1754 * handle supplementary characters (eg emoji). 1755 * 1756 * @param text The text to be drawn 1757 * @param index The index of the first character to draw 1758 * @param count The number of characters to draw, starting from index. 1759 * @param pos Array of [x,y] positions, used to position each 1760 * character 1761 * @param paint The paint used for the text (e.g. color, size, style) 1762 */ 1763 @Deprecated 1764 public void drawPosText(@NonNull char[] text, int index, int count, @NonNull float[] pos, 1765 @NonNull Paint paint) { 1766 if (index < 0 || index + count > text.length || count*2 > pos.length) { 1767 throw new IndexOutOfBoundsException(); 1768 } 1769 for (int i = 0; i < count; i++) { 1770 drawText(text, index + i, 1, pos[i * 2], pos[i * 2 + 1], paint); 1771 } 1772 } 1773 1774 /** 1775 * Draw the text in the array, with each character's origin specified by 1776 * the pos array. 1777 * 1778 * This method does not support glyph composition and decomposition and 1779 * should therefore not be used to render complex scripts. It also doesn't 1780 * handle supplementary characters (eg emoji). 1781 * 1782 * @param text The text to be drawn 1783 * @param pos Array of [x,y] positions, used to position each character 1784 * @param paint The paint used for the text (e.g. color, size, style) 1785 */ 1786 @Deprecated 1787 public void drawPosText(@NonNull String text, @NonNull float[] pos, @NonNull Paint paint) { 1788 drawPosText(text.toCharArray(), 0, text.length(), pos, paint); 1789 } 1790 1791 /** 1792 * Draw the text, with origin at (x,y), using the specified paint, along 1793 * the specified path. The paint's Align setting determins where along the 1794 * path to start the text. 1795 * 1796 * @param text The text to be drawn 1797 * @param path The path the text should follow for its baseline 1798 * @param hOffset The distance along the path to add to the text's 1799 * starting position 1800 * @param vOffset The distance above(-) or below(+) the path to position 1801 * the text 1802 * @param paint The paint used for the text (e.g. color, size, style) 1803 */ 1804 public void drawTextOnPath(@NonNull char[] text, int index, int count, @NonNull Path path, 1805 float hOffset, float vOffset, @NonNull Paint paint) { 1806 if (index < 0 || index + count > text.length) { 1807 throw new ArrayIndexOutOfBoundsException(); 1808 } 1809 native_drawTextOnPath(mNativeCanvasWrapper, text, index, count, 1810 path.ni(), hOffset, vOffset, 1811 paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); 1812 } 1813 1814 /** 1815 * Draw the text, with origin at (x,y), using the specified paint, along 1816 * the specified path. The paint's Align setting determins where along the 1817 * path to start the text. 1818 * 1819 * @param text The text to be drawn 1820 * @param path The path the text should follow for its baseline 1821 * @param hOffset The distance along the path to add to the text's 1822 * starting position 1823 * @param vOffset The distance above(-) or below(+) the path to position 1824 * the text 1825 * @param paint The paint used for the text (e.g. color, size, style) 1826 */ 1827 public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset, 1828 float vOffset, @NonNull Paint paint) { 1829 if (text.length() > 0) { 1830 native_drawTextOnPath(mNativeCanvasWrapper, text, path.ni(), hOffset, vOffset, 1831 paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); 1832 } 1833 } 1834 1835 /** 1836 * Save the canvas state, draw the picture, and restore the canvas state. 1837 * This differs from picture.draw(canvas), which does not perform any 1838 * save/restore. 1839 * 1840 * <p> 1841 * <strong>Note:</strong> This forces the picture to internally call 1842 * {@link Picture#endRecording} in order to prepare for playback. 1843 * 1844 * @param picture The picture to be drawn 1845 */ 1846 public void drawPicture(@NonNull Picture picture) { 1847 picture.endRecording(); 1848 int restoreCount = save(); 1849 picture.draw(this); 1850 restoreToCount(restoreCount); 1851 } 1852 1853 /** 1854 * Draw the picture, stretched to fit into the dst rectangle. 1855 */ 1856 public void drawPicture(@NonNull Picture picture, @NonNull RectF dst) { 1857 save(); 1858 translate(dst.left, dst.top); 1859 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1860 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 1861 } 1862 drawPicture(picture); 1863 restore(); 1864 } 1865 1866 /** 1867 * Draw the picture, stretched to fit into the dst rectangle. 1868 */ 1869 public void drawPicture(@NonNull Picture picture, @NonNull Rect dst) { 1870 save(); 1871 translate(dst.left, dst.top); 1872 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 1873 scale((float) dst.width() / picture.getWidth(), 1874 (float) dst.height() / picture.getHeight()); 1875 } 1876 drawPicture(picture); 1877 restore(); 1878 } 1879 1880 /** 1881 * Releases the resources associated with this canvas. 1882 * 1883 * @hide 1884 */ 1885 public void release() { 1886 mFinalizer.dispose(); 1887 } 1888 1889 /** 1890 * Free up as much memory as possible from private caches (e.g. fonts, images) 1891 * 1892 * @hide 1893 */ 1894 public static native void freeCaches(); 1895 1896 /** 1897 * Free up text layout caches 1898 * 1899 * @hide 1900 */ 1901 public static native void freeTextLayoutCaches(); 1902 1903 private static native long initRaster(long nativeBitmapOrZero); 1904 private static native void native_setBitmap(long canvasHandle, 1905 long bitmapHandle, 1906 boolean copyState); 1907 private static native boolean native_isOpaque(long canvasHandle); 1908 private static native int native_getWidth(long canvasHandle); 1909 private static native int native_getHeight(long canvasHandle); 1910 1911 private static native int native_save(long canvasHandle, int saveFlags); 1912 private static native int native_saveLayer(long nativeCanvas, float l, 1913 float t, float r, float b, 1914 long nativePaint, 1915 int layerFlags); 1916 private static native int native_saveLayerAlpha(long nativeCanvas, float l, 1917 float t, float r, float b, 1918 int alpha, int layerFlags); 1919 private static native void native_restore(long canvasHandle); 1920 private static native void native_restoreToCount(long canvasHandle, 1921 int saveCount); 1922 private static native int native_getSaveCount(long canvasHandle); 1923 1924 private static native void native_translate(long canvasHandle, 1925 float dx, float dy); 1926 private static native void native_scale(long canvasHandle, 1927 float sx, float sy); 1928 private static native void native_rotate(long canvasHandle, float degrees); 1929 private static native void native_skew(long canvasHandle, 1930 float sx, float sy); 1931 private static native void native_concat(long nativeCanvas, 1932 long nativeMatrix); 1933 private static native void native_setMatrix(long nativeCanvas, 1934 long nativeMatrix); 1935 private static native boolean native_clipRect(long nativeCanvas, 1936 float left, float top, 1937 float right, float bottom, 1938 int regionOp); 1939 private static native boolean native_clipPath(long nativeCanvas, 1940 long nativePath, 1941 int regionOp); 1942 private static native boolean native_clipRegion(long nativeCanvas, 1943 long nativeRegion, 1944 int regionOp); 1945 private static native void nativeSetDrawFilter(long nativeCanvas, 1946 long nativeFilter); 1947 private static native boolean native_getClipBounds(long nativeCanvas, 1948 Rect bounds); 1949 private static native void native_getCTM(long nativeCanvas, 1950 long nativeMatrix); 1951 private static native boolean native_quickReject(long nativeCanvas, 1952 long nativePath); 1953 private static native boolean native_quickReject(long nativeCanvas, 1954 float left, float top, 1955 float right, float bottom); 1956 private static native void native_drawColor(long nativeCanvas, int color, 1957 int mode); 1958 private static native void native_drawPaint(long nativeCanvas, 1959 long nativePaint); 1960 private static native void native_drawPoint(long canvasHandle, float x, float y, 1961 long paintHandle); 1962 private static native void native_drawPoints(long canvasHandle, float[] pts, 1963 int offset, int count, 1964 long paintHandle); 1965 private static native void native_drawLine(long nativeCanvas, float startX, 1966 float startY, float stopX, 1967 float stopY, long nativePaint); 1968 private static native void native_drawLines(long canvasHandle, float[] pts, 1969 int offset, int count, 1970 long paintHandle); 1971 private static native void native_drawRect(long nativeCanvas, float left, 1972 float top, float right, 1973 float bottom, 1974 long nativePaint); 1975 private static native void native_drawOval(long nativeCanvas, float left, float top, 1976 float right, float bottom, long nativePaint); 1977 private static native void native_drawCircle(long nativeCanvas, float cx, 1978 float cy, float radius, 1979 long nativePaint); 1980 private static native void native_drawArc(long nativeCanvas, float left, float top, 1981 float right, float bottom, 1982 float startAngle, float sweep, boolean useCenter, 1983 long nativePaint); 1984 private static native void native_drawRoundRect(long nativeCanvas, 1985 float left, float top, float right, float bottom, 1986 float rx, float ry, long nativePaint); 1987 private static native void native_drawPath(long nativeCanvas, 1988 long nativePath, 1989 long nativePaint); 1990 private native void native_drawBitmap(long nativeCanvas, long nativeBitmap, 1991 float left, float top, 1992 long nativePaintOrZero, 1993 int canvasDensity, 1994 int screenDensity, 1995 int bitmapDensity); 1996 private native void native_drawBitmap(long nativeCanvas, long nativeBitmap, 1997 float srcLeft, float srcTop, float srcRight, float srcBottom, 1998 float dstLeft, float dstTop, float dstRight, float dstBottom, 1999 long nativePaintOrZero, int screenDensity, int bitmapDensity); 2000 private static native void native_drawBitmap(long nativeCanvas, int[] colors, 2001 int offset, int stride, float x, 2002 float y, int width, int height, 2003 boolean hasAlpha, 2004 long nativePaintOrZero); 2005 private static native void nativeDrawBitmapMatrix(long nativeCanvas, 2006 long nativeBitmap, 2007 long nativeMatrix, 2008 long nativePaint); 2009 private static native void nativeDrawBitmapMesh(long nativeCanvas, 2010 long nativeBitmap, 2011 int meshWidth, int meshHeight, 2012 float[] verts, int vertOffset, 2013 int[] colors, int colorOffset, 2014 long nativePaint); 2015 private static native void nativeDrawVertices(long nativeCanvas, int mode, int n, 2016 float[] verts, int vertOffset, float[] texs, int texOffset, 2017 int[] colors, int colorOffset, short[] indices, 2018 int indexOffset, int indexCount, long nativePaint); 2019 2020 private static native void native_drawText(long nativeCanvas, char[] text, 2021 int index, int count, float x, 2022 float y, int flags, long nativePaint, 2023 long nativeTypeface); 2024 private static native void native_drawText(long nativeCanvas, String text, 2025 int start, int end, float x, 2026 float y, int flags, long nativePaint, 2027 long nativeTypeface); 2028 2029 private static native void native_drawTextRun(long nativeCanvas, String text, 2030 int start, int end, int contextStart, int contextEnd, 2031 float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 2032 2033 private static native void native_drawTextRun(long nativeCanvas, char[] text, 2034 int start, int count, int contextStart, int contextCount, 2035 float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 2036 2037 private static native void native_drawTextOnPath(long nativeCanvas, 2038 char[] text, int index, 2039 int count, long nativePath, 2040 float hOffset, 2041 float vOffset, int bidiFlags, 2042 long nativePaint, long nativeTypeface); 2043 private static native void native_drawTextOnPath(long nativeCanvas, 2044 String text, long nativePath, 2045 float hOffset, 2046 float vOffset, 2047 int flags, long nativePaint, long nativeTypeface); 2048 private static native void finalizer(long nativeCanvas); 2049} 2050