mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-06-09 16:14:09 +08:00
Refactor: use BitmapPool
This commit is contained in:
parent
62abca0b1e
commit
a5487ade83
@ -50,23 +50,30 @@ public class BlurTransformation implements Transformation<Bitmap> {
|
|||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
|
|
||||||
Bitmap.Config.ARGB_8888);
|
int width = source.getWidth();
|
||||||
Canvas canvas = new Canvas(outBitmap);
|
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);
|
canvas.drawBitmap(source, 0, 0, null);
|
||||||
|
|
||||||
RenderScript rs = RenderScript.create(mContext);
|
RenderScript rs = RenderScript.create(mContext);
|
||||||
Allocation overlayAlloc = Allocation.createFromBitmap(rs, outBitmap);
|
Allocation overlayAlloc = Allocation.createFromBitmap(rs, bitmap);
|
||||||
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
|
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
|
||||||
blur.setInput(overlayAlloc);
|
blur.setInput(overlayAlloc);
|
||||||
blur.setRadius(mRadius);
|
blur.setRadius(mRadius);
|
||||||
blur.forEach(overlayAlloc);
|
blur.forEach(overlayAlloc);
|
||||||
overlayAlloc.copyTo(outBitmap);
|
overlayAlloc.copyTo(bitmap);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
rs.destroy();
|
rs.destroy();
|
||||||
|
|
||||||
return BitmapResource.obtain(outBitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -45,13 +45,17 @@ public class ColorFilterTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
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);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
|
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
canvas.drawBitmap(source, 0, 0, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@ -24,6 +24,7 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapShader;
|
import android.graphics.BitmapShader;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Matrix;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
|
||||||
public class CropCircleTransformation implements Transformation<Bitmap> {
|
public class CropCircleTransformation implements Transformation<Bitmap> {
|
||||||
@ -42,24 +43,25 @@ public class CropCircleTransformation implements Transformation<Bitmap> {
|
|||||||
int width = (source.getWidth() - size) / 2;
|
int width = (source.getWidth() - size) / 2;
|
||||||
int height = (source.getHeight() - size) / 2;
|
int height = (source.getHeight() - size) / 2;
|
||||||
|
|
||||||
Bitmap squaredBitmap = Bitmap.createBitmap(source, width, height, size, size);
|
Bitmap bitmap = mBitmapPool.get(width, height, source.getConfig());
|
||||||
if (squaredBitmap != source) {
|
if (bitmap == null) {
|
||||||
source.recycle();
|
bitmap = Bitmap.createBitmap(width, height, source.getConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
|
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP,
|
||||||
BitmapShader.TileMode.CLAMP);
|
BitmapShader.TileMode.CLAMP);
|
||||||
|
Matrix matrix = new Matrix();
|
||||||
|
matrix.setTranslate(-width, -height);
|
||||||
|
shader.setLocalMatrix(matrix);
|
||||||
paint.setShader(shader);
|
paint.setShader(shader);
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
|
|
||||||
float r = size / 2f;
|
float r = size / 2f;
|
||||||
canvas.drawCircle(r, r, r, paint);
|
canvas.drawCircle(r, r, r, paint);
|
||||||
|
|
||||||
squaredBitmap.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,11 @@ public class CropSquareTransformation implements Transformation<Bitmap> {
|
|||||||
mWidth = (source.getWidth() - size) / 2;
|
mWidth = (source.getWidth() - size) / 2;
|
||||||
mHeight = (source.getHeight() - 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) {
|
if (bitmap != source) {
|
||||||
source.recycle();
|
source.recycle();
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,11 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
mHeight = source.getHeight();
|
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 scaleX = (float) mWidth / source.getWidth();
|
||||||
float scaleY = (float) mHeight / source.getHeight();
|
float scaleY = (float) mHeight / source.getHeight();
|
||||||
float scale = Math.max(scaleX, scaleY);
|
float scale = Math.max(scaleX, scaleY);
|
||||||
@ -61,9 +66,9 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
float top = (mHeight - scaledHeight) / 2;
|
float top = (mHeight - scaledHeight) / 2;
|
||||||
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, source.getConfig());
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
canvas.drawBitmap(source, null, targetRect, null);
|
canvas.drawBitmap(source, null, targetRect, null);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@ -42,7 +42,10 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
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);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
ColorMatrix saturation = new ColorMatrix();
|
ColorMatrix saturation = new ColorMatrix();
|
||||||
@ -50,6 +53,7 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
|
|||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
|
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
canvas.drawBitmap(source, 0, 0, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@ -48,7 +48,10 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
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);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
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));
|
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
||||||
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
|
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
|
||||||
radius, radius, paint);
|
radius, radius, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user