首页 > 学院 > 开发设计 > 正文

如何放大一个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的值。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表