Skip to content

Commit

Permalink
add BlurTransformation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Jun 25, 2018
1 parent d150dff commit 520d580
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 189 deletions.
32 changes: 15 additions & 17 deletions GlideImageView/src/main/java/com/sunfusheng/GlideImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.widget.ImageView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.sunfusheng.progress.GlideApp;
Expand All @@ -31,14 +30,15 @@ public class GlideImageLoader {

private String url;
private WeakReference<ImageView> imageViewWeakReference;
private RequestOptions requestOptions;
private GlideRequest<Drawable> glideRequest;

public static GlideImageLoader create(ImageView imageView) {
return new GlideImageLoader(imageView);
}

private GlideImageLoader(ImageView imageView) {
imageViewWeakReference = new WeakReference<>(imageView);
glideRequest = GlideApp.with(getContext()).asDrawable();
}

public ImageView getImageView() {
Expand All @@ -59,15 +59,11 @@ public String getUrl() {
return url;
}

public RequestOptions getRequestOptions() {
if (requestOptions == null) {
requestOptions = new RequestOptions();
public GlideRequest getGlideRequest() {
if (glideRequest == null) {
glideRequest = GlideApp.with(getContext()).asDrawable();
}
return requestOptions;
}

public void setRequestOptions(RequestOptions requestOptions) {
this.requestOptions = requestOptions;
return glideRequest;
}

protected Uri resId2Uri(@DrawableRes int resId) {
Expand All @@ -82,32 +78,34 @@ protected GlideRequest<Drawable> loadImage(Object obj) {
if (obj instanceof String) {
url = (String) obj;
}
return GlideApp.with(getContext()).load(obj);
return glideRequest.load(obj);
}


protected GlideImageLoader loadImage(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation) {
GlideRequest<Drawable> glideRequest = loadImage(obj);
glideRequest = loadImage(obj);
if (placeholder != 0) {
glideRequest = glideRequest.placeholder(placeholder).error(placeholder);
glideRequest = glideRequest.placeholder(placeholder);
}

if (transformation != null) {
glideRequest = glideRequest.transform(transformation);
}

glideRequest = glideRequest.apply(getRequestOptions());

glideRequest.into(new GlideImageViewTarget(getImageView()));
return this;
}

public GlideImageLoader listener(OnProgressListener onProgressListener) {
public GlideImageLoader listener(Object obj, OnProgressListener onProgressListener) {
if (obj instanceof String) {
url = (String) obj;
}
ProgressManager.addListener(url, onProgressListener);
return this;
}

private class GlideImageViewTarget extends DrawableImageViewTarget {
public GlideImageViewTarget(ImageView view) {
GlideImageViewTarget(ImageView view) {
super(view);
}

Expand Down
36 changes: 17 additions & 19 deletions GlideImageView/src/main/java/com/sunfusheng/GlideImageView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sunfusheng;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
Expand All @@ -17,6 +18,7 @@
/**
* @author sunfusheng on 2017/11/10.
*/
@SuppressLint("CheckResult")
public class GlideImageView extends ImageView {

private boolean enableState = false;
Expand Down Expand Up @@ -49,55 +51,47 @@ public GlideImageLoader getImageLoader() {
}

public GlideImageView apply(RequestOptions options) {
getImageLoader().setRequestOptions(options);
getImageLoader().getGlideRequest().apply(options);
return this;
}

public GlideImageView centerCrop() {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.centerCrop());
getImageLoader().getGlideRequest().centerCrop();
return this;
}

public GlideImageView fitCenter() {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.fitCenter());
getImageLoader().getGlideRequest().fitCenter();
return this;
}

public GlideImageView diskCacheStrategy(@NonNull DiskCacheStrategy strategy) {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.diskCacheStrategy(strategy));
getImageLoader().getGlideRequest().diskCacheStrategy(strategy);
return this;
}

public GlideImageView placeholder(@DrawableRes int resId) {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.placeholder(resId));
getImageLoader().getGlideRequest().placeholder(resId);
return this;
}

public GlideImageView error(@DrawableRes int resId) {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.error(resId));
getImageLoader().getGlideRequest().error(resId);
return this;
}

public GlideImageView fallback(@DrawableRes int resId) {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.fallback(resId));
getImageLoader().getGlideRequest().fallback(resId);
return this;
}

public GlideImageView dontAnimate() {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.dontAnimate());
getImageLoader().getGlideRequest().dontTransform();
return this;
}

public GlideImageView dontTransform() {
RequestOptions options = getImageLoader().getRequestOptions();
getImageLoader().setRequestOptions(options.dontTransform());
getImageLoader().getGlideRequest().dontTransform();
return this;
}

Expand All @@ -106,7 +100,7 @@ public void load(String url) {
}

public void load(String url, @DrawableRes int placeholder) {
load(url, placeholder, null);
load(url, placeholder, 0);
}

public void load(String url, @DrawableRes int placeholder, int radius) {
Expand All @@ -121,8 +115,12 @@ public void load(String url, @DrawableRes int placeholder, int radius, OnProgres
load(url, placeholder, new RadiusTransformation(getContext(), radius), onProgressListener);
}

public void load(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation) {
getImageLoader().loadImage(obj, placeholder, transformation);
}

public void load(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation, OnProgressListener onProgressListener) {
getImageLoader().loadImage(obj, placeholder, transformation).listener(onProgressListener);
getImageLoader().listener(obj, onProgressListener).loadImage(obj, placeholder, transformation);
}

public void loadCircle(String url) {
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,23 @@

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.util.Util;

import java.security.MessageDigest;

/**
* @author by sunfusheng on 2017/6/6.
*/
public class CircleTransformation extends BitmapTransformation {

public CircleTransformation() {
}
private final String ID = getClass().getName();

@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
int x = (toTransform.getWidth() - size) / 2;
int y = (toTransform.getHeight() - size) / 2;

private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;

int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;

Bitmap square = Bitmap.createBitmap(source, x, y, size, size);
Bitmap square = Bitmap.createBitmap(toTransform, x, y, size, size);
Bitmap circle = pool.get(size, size, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(circle);
Expand All @@ -44,7 +37,20 @@ private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
public boolean equals(Object obj) {
if (obj instanceof BlurTransformation) {
return this == obj;
}
return false;
}

@Override
public int hashCode() {
return Util.hashCode(ID.hashCode());
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update(ID.getBytes(CHARSET));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.graphics.RectF;
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.Utils;

import java.security.MessageDigest;
Expand All @@ -18,6 +19,7 @@
* @author by sunfusheng on 2017/6/6.
*/
public class RadiusTransformation extends BitmapTransformation {
private final String ID = getClass().getName();

private int radius;

Expand All @@ -27,28 +29,36 @@ public RadiusTransformation(Context context, int radius) {

@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return radiusCrop(pool, toTransform);
}

private Bitmap radiusCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int width = toTransform.getWidth();
int height = toTransform.getHeight();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int size = Math.min(source.getWidth(), source.getHeight());
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
return bitmap;
}

Bitmap bitmap = pool.get(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
canvas.drawRoundRect(0, 0, size, size, radius, radius, paint);
return bitmap;
@Override
public boolean equals(Object obj) {
if (obj instanceof RadiusTransformation) {
RadiusTransformation other = (RadiusTransformation) obj;
return radius == other.radius;
}
return source;
return false;
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
public int hashCode() {
return Util.hashCode(ID.hashCode(), Util.hashCode(radius));
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius).getBytes(CHARSET));
}

}
Loading

0 comments on commit 520d580

Please sign in to comment.