BitmapDrawable.java revision c2974809373697147cbe5754835cc871fb93aef1
1fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera/* 2fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Copyright (C) 2006 The Android Open Source Project 3fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 4fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Licensed under the Apache License, Version 2.0 (the "License"); 5fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * you may not use this file except in compliance with the License. 6fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * You may obtain a copy of the License at 7fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 8fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * http://www.apache.org/licenses/LICENSE-2.0 9fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 10fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Unless required by applicable law or agreed to in writing, software 11fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * distributed under the License is distributed on an "AS IS" BASIS, 12fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * See the License for the specific language governing permissions and 14fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * limitations under the License. 15fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 16fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 17fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherapackage android.graphics.drawable; 18fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 19fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.content.res.Resources; 20fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.content.res.TypedArray; 21fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.Bitmap; 22fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.BitmapFactory; 23fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.Canvas; 24fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.ColorFilter; 25fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.Paint; 26fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.PixelFormat; 27fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.Rect; 28fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.Shader; 29fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.graphics.BitmapShader; 30fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.util.AttributeSet; 31fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.util.DisplayMetrics; 32fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport android.view.Gravity; 33fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 34fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport org.xmlpull.v1.XmlPullParser; 35fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport org.xmlpull.v1.XmlPullParserException; 36fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 37fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheraimport java.io.IOException; 38fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 39fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera/** 40fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a 41fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * BitmapDrawable from a file path, an input stream, through XML inflation, or from 42fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * a {@link android.graphics.Bitmap} object. 43fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * <p>It can be defined in an XML file with the <code><bitmap></code> element.</p> 4490855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * <p> 4590855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * Also see the {@link android.graphics.Bitmap} class, which handles the management and 4690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * transformation of raw bitmap graphics, and should be used when drawing to a 4790855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * {@link android.graphics.Canvas}. 4890855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * </p> 4990855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * 5090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * @attr ref android.R.styleable#BitmapDrawable_src 51fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @attr ref android.R.styleable#BitmapDrawable_antialias 52fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @attr ref android.R.styleable#BitmapDrawable_filter 53fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @attr ref android.R.styleable#BitmapDrawable_dither 54fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @attr ref android.R.styleable#BitmapDrawable_gravity 55fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @attr ref android.R.styleable#BitmapDrawable_tileMode 56fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 57fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherapublic class BitmapDrawable extends Drawable { 58fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 59fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private static final int DEFAULT_PAINT_FLAGS = 60fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG; 61fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private BitmapState mBitmapState; 62fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private Bitmap mBitmap; 6390855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private int mTargetDensity; 64fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 65fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private final Rect mDstRect = new Rect(); // Gravity.apply() sets this 66fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 6790855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private boolean mApplyGravity; 6890855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private boolean mRebuildShader; 6990855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private boolean mMutated; 70fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 7190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson // These are scaled to match the target density. 7290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private int mBitmapWidth; 7390855078eb989944bca1824058d7231cd68e5021Henrik B Andersson private int mBitmapHeight; 7490855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 7590855078eb989944bca1824058d7231cd68e5021Henrik B Andersson /** 7690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * Create an empty drawable, not dealing with density. 7790855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * @deprecated Use {@link #BitmapDrawable(Resources)} to ensure 7890855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * that the drawable has correctly set its target density. 7990855078eb989944bca1824058d7231cd68e5021Henrik B Andersson */ 8090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson @Deprecated 8190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson public BitmapDrawable() { 82fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState = new BitmapState((Bitmap) null); 83fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 84fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 85fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 86fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Create an empty drawable, setting initial target density based on 87fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * the display metrics of the resources. 88fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 89fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(Resources res) { 90fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState = new BitmapState((Bitmap) null); 91fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mTargetDensity = mTargetDensity; 92fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 93fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 94fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 9590855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * Create drawable from a bitmap, not dealing with density. 96fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure 97fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * that the drawable has correctly set its target density. 98fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 99fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Deprecated 10090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson public BitmapDrawable(Bitmap bitmap) { 101fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera this(new BitmapState(bitmap), null); 102fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 103fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 104fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 105fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Create drawable from a bitmap, setting initial target density based on 106fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * the display metrics of the resources. 107fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 108fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(Resources res, Bitmap bitmap) { 109fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera this(new BitmapState(bitmap), res); 110fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mTargetDensity = mTargetDensity; 111fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 11290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 113fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 114fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Create a drawable by opening a given file path and decoding the bitmap. 115fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @deprecated Use {@link #BitmapDrawable(Resources, String)} to ensure 116fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * that the drawable has correctly set its target density. 117fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 118fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(String filepath) { 119fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera this(new BitmapState(BitmapFactory.decodeFile(filepath)), null); 120fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap == null) { 121fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath); 122fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 123fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 124fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 125fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 12690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * Create a drawable by opening a given file path and decoding the bitmap. 127fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 128fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(Resources res, String filepath) { 129fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera this(new BitmapState(BitmapFactory.decodeFile(filepath)), null); 130fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mTargetDensity = mTargetDensity; 131fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap == null) { 132fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath); 133fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 134fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 135fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 136fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 13790855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * Create a drawable by decoding a bitmap from the given input stream. 138fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @deprecated Use {@link #BitmapDrawable(Resources, java.io.InputStream)} to ensure 139fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * that the drawable has correctly set its target density. 14090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson */ 141fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(java.io.InputStream is) { 14290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson this(new BitmapState(BitmapFactory.decodeStream(is)), null); 143fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap == null) { 144fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is); 145fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 146fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 147fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 148fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 149fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Create a drawable by decoding a bitmap from the given input stream. 150fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 151fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public BitmapDrawable(Resources res, java.io.InputStream is) { 15290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson this(new BitmapState(BitmapFactory.decodeStream(is)), null); 153fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mTargetDensity = mTargetDensity; 154fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap == null) { 155fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is); 156fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 157fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 15890855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 159fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public final Paint getPaint() { 160fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapState.mPaint; 161fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 162fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 163fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public final Bitmap getBitmap() { 164fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmap; 165fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 16690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 167fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private void computeBitmapSize() { 168fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity); 169fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity); 170fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 171fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 172fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private void setBitmap(Bitmap bitmap) { 173fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmap = bitmap; 174fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (bitmap != null) { 175fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera computeBitmapSize(); 176fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } else { 177fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapWidth = mBitmapHeight = -1; 178fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 179fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 180fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 181fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 182fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Set the density scale at which this drawable will be rendered. This 183fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * method assumes the drawable will be rendered at the same density as the 184fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * specified canvas. 185fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 186fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @param canvas The Canvas from which the density scale must be obtained. 187fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 188fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#setDensity(int) 189fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#getDensity() 190fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 191fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setTargetDensity(Canvas canvas) { 192fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTargetDensity(canvas.getDensity()); 193fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 194fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 195fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 196fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Set the density scale at which this drawable will be rendered. 197fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 198fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @param metrics The DisplayMetrics indicating the density scale for this drawable. 199fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 200fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#setDensity(int) 201fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#getDensity() 20290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson */ 203fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setTargetDensity(DisplayMetrics metrics) { 204fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTargetDensity = metrics.densityDpi; 205fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap != null) { 206fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera computeBitmapSize(); 207fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 208fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 209fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 210fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 211fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Set the density at which this drawable will be rendered. 212fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 21390855078eb989944bca1824058d7231cd68e5021Henrik B Andersson * @param density The density scale for this drawable. 214fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 215fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#setDensity(int) 216fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @see android.graphics.Bitmap#getDensity() 217fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 218fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setTargetDensity(int density) { 219fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTargetDensity = density == 0 ? DisplayMetrics.DENSITY_DEFAULT : density; 220fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmap != null) { 22190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson computeBitmapSize(); 222fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 223fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 224fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 225fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** Get the gravity used to position/stretch the bitmap within its bounds. 226fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * See android.view.Gravity 227fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @return the gravity applied to the bitmap 228fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 229fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getGravity() { 230fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapState.mGravity; 23190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson } 232fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 233fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** Set the gravity used to position/stretch the bitmap within its bounds. 234fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera See android.view.Gravity 235fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @param gravity the gravity 236fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 237fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setGravity(int gravity) { 238fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mGravity = gravity; 239fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mApplyGravity = true; 240fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 241fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 242fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setAntiAlias(boolean aa) { 243fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mPaint.setAntiAlias(aa); 244fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 245fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 246fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 247fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setFilterBitmap(boolean filter) { 248fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mPaint.setFilterBitmap(filter); 249fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 250fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 251fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 252fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setDither(boolean dither) { 253fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mPaint.setDither(dither); 254fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 255fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 256fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public Shader.TileMode getTileModeX() { 257fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapState.mTileModeX; 258fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 259fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 260fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public Shader.TileMode getTileModeY() { 26190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson return mBitmapState.mTileModeY; 262fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 263fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 264fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setTileModeX(Shader.TileMode mode) { 265fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTileModeXY(mode, mBitmapState.mTileModeY); 266fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 267fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 268fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public final void setTileModeY(Shader.TileMode mode) { 26990855078eb989944bca1824058d7231cd68e5021Henrik B Andersson setTileModeXY(mBitmapState.mTileModeX, mode); 270fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 271fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 272fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setTileModeXY(Shader.TileMode xmode, Shader.TileMode ymode) { 273fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final BitmapState state = mBitmapState; 274fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (state.mTileModeX != xmode || state.mTileModeY != ymode) { 275fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera state.mTileModeX = xmode; 27690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson state.mTileModeY = ymode; 277fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mRebuildShader = true; 278fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 279fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 280fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 281fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 282fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getChangingConfigurations() { 283fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return super.getChangingConfigurations() | mBitmapState.mChangingConfigurations; 284fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 285fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 286fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 287fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera protected void onBoundsChange(Rect bounds) { 288fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera super.onBoundsChange(bounds); 289fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mApplyGravity = true; 290fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 291fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 292fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 293fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void draw(Canvas canvas) { 294fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Bitmap bitmap = mBitmap; 295fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (bitmap != null) { 296fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final BitmapState state = mBitmapState; 297fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mRebuildShader) { 298fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader.TileMode tmx = state.mTileModeX; 299fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader.TileMode tmy = state.mTileModeY; 30090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 301fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (tmx == null && tmy == null) { 302fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera state.mPaint.setShader(null); 303fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } else { 304fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader s = new BitmapShader(bitmap, 305fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera tmx == null ? Shader.TileMode.CLAMP : tmx, 306fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera tmy == null ? Shader.TileMode.CLAMP : tmy); 307fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera state.mPaint.setShader(s); 308fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 309fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mRebuildShader = false; 310fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera copyBounds(mDstRect); 311fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 312fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 313fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader shader = state.mPaint.getShader(); 314fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (shader == null) { 315fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mApplyGravity) { 316fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight, 317fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera getBounds(), mDstRect); 318fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mApplyGravity = false; 319fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 320fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera canvas.drawBitmap(bitmap, null, mDstRect, state.mPaint); 32190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson } else { 322fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mApplyGravity) { 323fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mDstRect.set(getBounds()); 324fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mApplyGravity = false; 325fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 326fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera canvas.drawRect(mDstRect, state.mPaint); 32790855078eb989944bca1824058d7231cd68e5021Henrik B Andersson } 328fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 329fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 330fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 331fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 332fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setAlpha(int alpha) { 333fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mPaint.setAlpha(alpha); 334fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 335fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 336fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 337fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void setColorFilter(ColorFilter cf) { 338fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mPaint.setColorFilter(cf); 33990855078eb989944bca1824058d7231cd68e5021Henrik B Andersson } 340fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 341fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera /** 342fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * A mutable BitmapDrawable still shares its Bitmap with any other Drawable 343fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * that comes from the same resource. 344fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 345fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * @return This drawable. 34690855078eb989944bca1824058d7231cd68e5021Henrik B Andersson */ 347fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 348fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public Drawable mutate() { 349fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (!mMutated && super.mutate() == this) { 350fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState = new BitmapState(mBitmapState); 351fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mMutated = true; 352fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 353fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return this; 35490855078eb989944bca1824058d7231cd68e5021Henrik B Andersson } 355fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 356fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 357fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 358fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera throws XmlPullParserException, IOException { 359fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera super.inflate(r, parser, attrs); 360fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 361fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.BitmapDrawable); 362fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 363fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final int id = a.getResourceId(com.android.internal.R.styleable.BitmapDrawable_src, 0); 364fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (id == 0) { 365fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera throw new XmlPullParserException(parser.getPositionDescription() + 366fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera ": <bitmap> requires a valid src attribute"); 367fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 368fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final Bitmap bitmap = BitmapFactory.decodeResource(r, id); 369fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (bitmap == null) { 370fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera throw new XmlPullParserException(parser.getPositionDescription() + 371fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera ": <bitmap> requires a valid src attribute"); 372fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 373fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mBitmap = bitmap; 374fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setBitmap(bitmap); 375fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTargetDensity(r.getDisplayMetrics()); 376fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 377fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final Paint paint = mBitmapState.mPaint; 378fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.setAntiAlias(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_antialias, 379fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.isAntiAlias())); 380fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.setFilterBitmap(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_filter, 381fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.isFilterBitmap())); 382fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.setDither(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_dither, 383fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera paint.isDither())); 384fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setGravity(a.getInt(com.android.internal.R.styleable.BitmapDrawable_gravity, Gravity.FILL)); 385fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int tileMode = a.getInt(com.android.internal.R.styleable.BitmapDrawable_tileMode, -1); 386fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (tileMode != -1) { 387fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera switch (tileMode) { 388fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera case 0: 389fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTileModeXY(Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 390fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera break; 391fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera case 1: 392fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 393fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera break; 394fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera case 2: 395fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setTileModeXY(Shader.TileMode.MIRROR, Shader.TileMode.MIRROR); 396fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera break; 397fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 398fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 399fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 400fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera a.recycle(); 401fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 402fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 40390855078eb989944bca1824058d7231cd68e5021Henrik B Andersson @Override 404fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getIntrinsicWidth() { 405fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapWidth; 406fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 407fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 408fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 409fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getIntrinsicHeight() { 410fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapHeight; 411fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 412fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 413fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 414fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getOpacity() { 415fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (mBitmapState.mGravity != Gravity.FILL) { 416fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return PixelFormat.TRANSLUCENT; 417fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 418fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Bitmap bm = mBitmap; 419fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return (bm == null || bm.hasAlpha() || mBitmapState.mPaint.getAlpha() < 255) ? 420fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE; 421fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 422fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 423fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 424fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public final ConstantState getConstantState() { 425fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState.mChangingConfigurations = super.getChangingConfigurations(); 426fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mBitmapState; 427fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 428fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 429fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera final static class BitmapState extends ConstantState { 430fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Bitmap mBitmap; 431fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int mChangingConfigurations; 432fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int mGravity = Gravity.FILL; 433fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Paint mPaint = new Paint(DEFAULT_PAINT_FLAGS); 434fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader.TileMode mTileModeX; 435fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera Shader.TileMode mTileModeY; 436fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT; 437fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 438fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera BitmapState(Bitmap bitmap) { 439fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmap = bitmap; 440fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 441fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 442fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera BitmapState(BitmapState bitmapState) { 443fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera this(bitmapState.mBitmap); 44490855078eb989944bca1824058d7231cd68e5021Henrik B Andersson mChangingConfigurations = bitmapState.mChangingConfigurations; 445fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mGravity = bitmapState.mGravity; 446fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTileModeX = bitmapState.mTileModeX; 447fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTileModeY = bitmapState.mTileModeY; 448fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTargetDensity = bitmapState.mTargetDensity; 449fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mPaint = new Paint(bitmapState.mPaint); 450fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 45190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 452fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 453fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public Drawable newDrawable() { 454fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return new BitmapDrawable(this, null); 455fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 456fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 457fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 458fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public Drawable newDrawable(Resources res) { 459fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return new BitmapDrawable(this, res); 460fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 461fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 462fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera @Override 463fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera public int getChangingConfigurations() { 464fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return mChangingConfigurations; 465fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 466fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 467fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 468fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera private BitmapDrawable(BitmapState state, Resources res) { 469fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mBitmapState = state; 470fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (res != null) { 471fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTargetDensity = res.getDisplayMetrics().densityDpi; 472fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } else if (state != null) { 47390855078eb989944bca1824058d7231cd68e5021Henrik B Andersson mTargetDensity = state.mTargetDensity; 474fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } else { 475fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera mTargetDensity = DisplayMetrics.DENSITY_DEFAULT; 476fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 477fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera setBitmap(state.mBitmap); 478fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 479fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera} 48090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson