温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android项目实战之Glide 高斯模糊效果的实例代码

发布时间:2020-10-10 21:10:20 来源:脚本之家 阅读:220 作者:听着music睡 栏目:移动开发

核心需要高斯模糊的库

compile 'jp.wasabeef:glide-transformations:2.0.1'

针对于3.7的版本

使用方法为:

//加载背景,
      Glide.with(MusicPlayerActivity.this)
          .load(service.getImageUri())
          .dontAnimate()
          .error(R.drawable.no_music_rotate_img)
          // 设置高斯模糊,模糊程度(最大25) 缩放比例
          .bitmapTransform(new BlurTransformation(this, 14, 3))
          .into(allBg);

针对4.+

Glide.with(context)
        .load(url)
        .apply(RequestOptions.bitmapTransform(new BlurTransformation(25,8)))
        .into(view);            

BlurTransformation 类源码:

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 java.security.MessageDigest;
import jp.wasabeef.glide.transformations.internal.FastBlur;
public class BlurTransformation extends BitmapTransformation {
  private static final int VERSION = 1;
  private static final String ID = "BlurTransformation." + VERSION;
  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 = FastBlur.blur(bitmap, radius, true);
    return bitmap;
  }
  @Override public String toString() {
    return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
  }
  @Override public boolean equals(Object o) {
    return o instanceof BlurTransformation &&
        ((BlurTransformation) o).radius == radius &&
        ((BlurTransformation) o).sampling == sampling;
  }
  @Override public int hashCode() {
    return ID.hashCode() + radius * 1000 + sampling * 10;
  }
  @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
    messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
  }
}

总结

以上所述是小编给大家介绍的Android项目实战之Glide 高斯模糊效果的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI