1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-06-08 23:44:04 +08:00

Refactor: use BitmapPool

This commit is contained in:
wasabeef 2015-02-03 22:59:32 +09:00
parent 62abca0b1e
commit a5487ade83
7 changed files with 48 additions and 18 deletions

View File

@ -50,23 +50,30 @@ public class BlurTransformation implements Transformation<Bitmap> {
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
}
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(source, 0, 0, null);
RenderScript rs = RenderScript.create(mContext);
Allocation overlayAlloc = Allocation.createFromBitmap(rs, outBitmap);
Allocation overlayAlloc = Allocation.createFromBitmap(rs, bitmap);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(mRadius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(outBitmap);
overlayAlloc.copyTo(bitmap);
source.recycle();
rs.destroy();
return BitmapResource.obtain(outBitmap, mBitmapPool);
return BitmapResource.obtain(bitmap, mBitmapPool);
}
@Override

View File

@ -45,13 +45,17 @@ public class ColorFilterTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(source, 0, 0, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@ -24,6 +24,7 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
public class CropCircleTransformation implements Transformation<Bitmap> {
@ -42,24 +43,25 @@ public class CropCircleTransformation implements Transformation<Bitmap> {
int width = (source.getWidth() - size) / 2;
int height = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, width, height, size, size);
if (squaredBitmap != source) {
source.recycle();
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setTranslate(-width, -height);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);
}

View File

@ -41,7 +41,11 @@ public class CropSquareTransformation implements Transformation<Bitmap> {
mWidth = (source.getWidth() - size) / 2;
mHeight = (source.getHeight() - size) / 2;
Bitmap bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
}
if (bitmap != source) {
source.recycle();
}

View File

@ -51,6 +51,11 @@ public class CropTransformation implements Transformation<Bitmap> {
mHeight = source.getHeight();
}
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(mWidth, mHeight, source.getConfig());
}
float scaleX = (float) mWidth / source.getWidth();
float scaleY = (float) mHeight / source.getHeight();
float scale = Math.max(scaleX, scaleY);
@ -61,9 +66,9 @@ public class CropTransformation implements Transformation<Bitmap> {
float top = (mHeight - scaledHeight) / 2;
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, source.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(source, null, targetRect, null);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@ -42,7 +42,10 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
}
Canvas canvas = new Canvas(bitmap);
ColorMatrix saturation = new ColorMatrix();
@ -50,6 +53,7 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
canvas.drawBitmap(source, 0, 0, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@ -48,7 +48,10 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
@ -56,6 +59,7 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
radius, radius, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);