80,354
社区成员
发帖
与我相关
我的任务
分享
//出错的函数是这样的
Bitmap btSrc = Bitmap.createBitmap(....);
Matrix mx = new Matrix();
int clipX = .. , clipY = .. ,clipW = ..,clipH = ..;
for(int i=0;i<5;i++){
Bitmap btMid = Bitmap.createBitmap(btSrc, 0, 0, btSrc.getWidth(), btSrc.getHeight(), mx, true);
Bitmap btClip = Bitmap.createBitmap(btMid, clipX,clipY,clipW,clipH);
if(btMid!=null&&!btMid.isRecycled()){
btMid.recycle(); //释放图片占用的堆栈内存。
btMid = null;
}
if(btClip!=null&&!btClip.isRecycled()){
btClip.recycle();
btClip = null;
}
}
Bitmap btMid = Bitmap.createBitmap(btSrc, 0, 0, btSrc.getWidth(), btSrc.getHeight(), mx, true);
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);
if (m == null || m.isIdentity()) {
bitmap = createBitmap(neww, newh,
source.hasAlpha() ? Config.ARGB_8888 : Config.RGB_565);
paint = null; // not needed
} else {
/* the dst should have alpha if the src does, or if our matrix
doesn't preserve rectness
*/
boolean hasAlpha = source.hasAlpha() || !m.rectStaysRect();
RectF deviceR = new RectF();
m.mapRect(deviceR, dstR);
neww = Math.round(deviceR.width());
newh = Math.round(deviceR.height());
bitmap = createBitmap(neww, newh, hasAlpha ? Config.ARGB_8888 : Config.RGB_565);
if (hasAlpha) {
bitmap.eraseColor(0);
}
canvas.translate(-deviceR.left, -deviceR.top);
canvas.concat(m);
paint = new Paint();
paint.setFilterBitmap(filter);
if (!m.rectStaysRect()) {
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;
canvas.setBitmap(bitmap);
canvas.drawBitmap(source, srcR, dstR, paint);
return bitmap;
}
/**
* Returns a mutable bitmap with the specified width and height. Its
* initial density is as per {@link #getDensity}.
*
* @param width The width of the bitmap
* @param height The height of the bitmap
* @param config The bitmap config to create.
* @throws IllegalArgumentException if the width or height are <= 0
*/
public static Bitmap createBitmap(int width, int height, Config config) {
Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
bm.eraseColor(0); // start with black/transparent pixels
return bm;
}