-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d150dff
commit 520d580
Showing
11 changed files
with
459 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
GlideImageView/src/main/java/com/sunfusheng/transformation/BlurTransformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.sunfusheng.transformation; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; | ||
import com.bumptech.glide.util.Util; | ||
import com.sunfusheng.util.BlurUtils; | ||
|
||
import java.security.MessageDigest; | ||
|
||
/** | ||
* @author sunfusheng on 2018/6/25. | ||
*/ | ||
public class BlurTransformation extends BitmapTransformation { | ||
private final String ID = getClass().getName(); | ||
|
||
private static int MAX_RADIUS = 25; | ||
private static int DEFAULT_DOWN_SAMPLING = 1; | ||
|
||
private int radius; | ||
private int sampling; | ||
|
||
public BlurTransformation() { | ||
this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING); | ||
} | ||
|
||
public BlurTransformation(int radius) { | ||
this(radius, DEFAULT_DOWN_SAMPLING); | ||
} | ||
|
||
public BlurTransformation(int radius, int sampling) { | ||
this.radius = radius; | ||
this.sampling = sampling; | ||
} | ||
|
||
@Override | ||
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { | ||
int width = toTransform.getWidth(); | ||
int height = toTransform.getHeight(); | ||
int scaledWidth = width / sampling; | ||
int scaledHeight = height / sampling; | ||
|
||
Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); | ||
Canvas canvas = new Canvas(bitmap); | ||
canvas.scale(1 / (float) sampling, 1 / (float) sampling); | ||
Paint paint = new Paint(); | ||
paint.setFlags(Paint.FILTER_BITMAP_FLAG); | ||
canvas.drawBitmap(toTransform, 0, 0, paint); | ||
bitmap = BlurUtils.blur(bitmap, radius, true); | ||
return bitmap; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof BlurTransformation) { | ||
BlurTransformation other = (BlurTransformation) obj; | ||
return radius == other.radius && sampling == other.sampling; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Util.hashCode(ID.hashCode(), Util.hashCode(radius)); | ||
} | ||
|
||
@Override | ||
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { | ||
messageDigest.update((ID + radius + sampling).getBytes(CHARSET)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.