Bitmap createScaleBitmap()需要注意点

来源:互联网 发布:淘宝动漫周边 侵权 编辑:程序博客网 时间:2024/05/17 06:33
我们在创建Bitmap对象的时候,可能需要源于原来的Bitmap,然后做一些修改创建一个新的Bitmap,如以下方法:

public static Bitmap createBitmap(Bitmap src);

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height);

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
            Matrix m, boolean filter);

public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
            boolean filter);

注意API里的说明:     
     * Returns an immutable bitmap from the specified subset of the source
     * bitmap. The new bitmap may be the same object as source, or a copy may
     * have been made. It is initialized with the same density as the original
     * bitmap.


在创建Bitmap的时候,里面会有这么一个判断
    if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
            height == source.getHeight() && (m ==
 null || m.isIdentity())) 
            return source;
    }

在这里看出,如果原图是不可变的,新创建的图大小与原图一样,则直接返回原图。
如果代码里这样使用:
Bitmap newBmp = Bitmap.createBitmap(src, 0, 0, w, h);
src.recycle();
当 w == src.getWidth() && h == src.getHeight()时,可能newBmp直接返回的就是src这个对象,如果src被recycle()释放掉之后,再去使用newBmp,可能就会报异常

0 0
原创粉丝点击