欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-07-19 19:30:29
核心需要高斯模糊的库 compile 'jp.wasabeef:glide-transformations:2.0.1' 针对于3.7的版本 使用方法...

核心需要高斯模糊的库

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 高斯模糊效果的实例代码,希望对大家有所帮助