我正在尝试使用反射创建一个可绘制对象,这是我的代码:
public Drawable createReflectedImage(Drawable d) {
int width = d.getIntrinsicWidth();
int height = d.getIntrinsicHeight();
Bitmap originalImage = Bitmap.createBitmap(width, height,
Config.ARGB_8888);
d.setBounds(0, 0, width, height);
Canvas canvas = new Canvas(originalImage);
d.draw(canvas);
final int reflectionGap = 4;
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(originalImage, 0, 0, null);
Paint deafaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff,
TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
BitmapDrawable drawable = new BitmapDrawable(getContext()
.getResources(), bitmapWithReflection);
originalImage.recycle();
reflectionImage.recycle();
return drawable;
}
每次createReflectedImage
调用时都会显示以下日志,即使我只是在此函数结束时返回 null。
10-29 10:56:48.541: I/dalvikvm-heap(23989): Grow heap (frag case) to 19.332MB for 960016-byte allocation
谁能帮我弄清楚这个内存泄漏是怎么发生的?