国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何放大一個drawable

2019-11-06 09:56:49
字體:
來源:轉載
供稿:網友

縮小一個drawable在網上很容易找到答案,不過變大一個drawable就不是那么好找的了。

不通的思路:Bitmap.createBitmap

網上很多方案都是使用這個方法

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,            Matrix m, boolean filter) {        checkXYSign(x, y);        checkWidthHeight(width, height);        if (x + width > source.getWidth()) {            throw new IllegalArgumentException("x + width must be <= bitmap.width()");        }        if (y + height > source.getHeight()) {            throw new IllegalArgumentException("y + height must be <= bitmap.height()");        }        // check if we can just return our argument unchanged        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&                height == source.getHeight() && (m == null || m.isIdentity())) {            return source;        }        int neww = width;        int newh = height;        Canvas canvas = new Canvas();        Bitmap bitmap;        Paint paint;        Rect srcR = new Rect(x, y, x + width, y + height);        RectF dstR = new RectF(0, 0, width, height);        Config newConfig = Config.ARGB_8888;        final Config config = source.getConfig();        // GIF files generate null configs, assume ARGB_8888        if (config != null) {            switch (config) {                case RGB_565:                    newConfig = Config.RGB_565;                    break;                case ALPHA_8:                    newConfig = Config.ALPHA_8;                    break;                //noinspection dePRecation                case ARGB_4444:                case ARGB_8888:                default:                    newConfig = Config.ARGB_8888;                    break;            }        }        if (m == null || m.isIdentity()) {            bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha());            paint = null;   // not needed        } else {            final boolean transformed = !m.rectStaysRect();            RectF deviceR = new RectF();            m.mapRect(deviceR, dstR);            neww = Math.round(deviceR.width());            newh = Math.round(deviceR.height());            bitmap = createBitmap(neww, newh, transformed ? Config.ARGB_8888 : newConfig,                    transformed || source.hasAlpha());            canvas.translate(-deviceR.left, -deviceR.top);            canvas.concat(m);            paint = new Paint();            paint.setFilterBitmap(filter);            if (transformed) {                paint.setAntiAlias(true);            }        }        // The new bitmap was created from a known bitmap source so assume that        // they use the same density        bitmap.mDensity = source.mDensity;        bitmap.setHasAlpha(source.hasAlpha());        bitmap.setPremultiplied(source.mRequestPremultiplied);        canvas.setBitmap(bitmap);        canvas.drawBitmap(source, srcR, dstR, paint);        canvas.setBitmap(null);        return bitmap;    }

這個方法很容易看出,無法創建比原圖片大的圖片,可以縮小,但是無法放大。
 if (x + width > source.getWidth()) {            throw new IllegalArgumentException("x + width must be <= bitmap.width()");        }        if (y + height > source.getHeight()) {            throw new IllegalArgumentException("y + height must be <= bitmap.height()");        }

換思路:利用canvas

大體看了一下Bitmap的諸多方法,貌似都不行,那就試著用canvas重新畫一個bitmap了,直接上代碼
  public static Drawable createDrawable(Context context, int width, int height, Drawable drawable) {        if (drawable == null || width <= 0 || height <= 0) {            return null;        }
        Bitmap newBitmap = null;
        try {    
	    newBitmap = Bitmap.createBitmap(width, height, config);        } catch (OutOfMemoryError e) {            e.printStackTrace();        }
if (newBitmap == null) {
           return null;
        }
        Canvas canvas = new Canvas();        canvas.setBitmap(newBitmap);        drawable.setBounds(new Rect(0, 0, width, height));        drawable.draw(canvas);        return new BitmapDrawable(context.getResources(), newBitmap);    }
width和height就是需要的寬和高,當然,如果放大的話,事先算好了就可以。代碼很簡單,不過最后轉出來的drawable變成了bitmapdrawable。注意最后的地方:new BitmapDrawable的時候,一定要傳resource,否則會使用DisplayMetrics.DENSITY_DEFAULT的值。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新营市| 石城县| 普陀区| 武安市| 普陀区| 金溪县| 牡丹江市| 南涧| 安阳县| 正宁县| 柏乡县| 商南县| 股票| 东阳市| 平湖市| 山东省| 马尔康县| 永寿县| 上林县| 施秉县| 凤凰县| 彩票| 阳山县| 平泉县| 海林市| 汉源县| 奉贤区| 富阳市| 南陵县| 清水河县| 浠水县| 铅山县| 苏尼特左旗| 永德县| 偏关县| 丹凤县| 武强县| 车险| 高雄县| 特克斯县| 剑河县|