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

Android 图片设置圆角

程序员文章站 2022-08-17 16:15:08
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片 ......

  android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片

       方法一:

  通过第三方框架glide实现图片显示有圆角,有三种写法如下:

  1.1,第一种实现:

  requestoptions options = new requestoptions().error(r.drawable.img_load_failure).bitmaptransform(new roundedcorners(30));//图片圆角为30

  glide.with(this).load(url) //图片地址

  .apply(options)

  .into(imagview);

  1.2,第二种实现:

  requestoptions requestoptions = new requestoptions();

  requestoptions.placeholder(r.drawable.ic_launcher_background);

  requestoptions.circlecroptransform();

  requestoptions.transforms( new roundedcorners(30));

  glide.with(this).load(url) //图片地址

  .apply(options)

  .into(imagview);

  1.3,第三种实现:

  requestoptions options = new requestoptions().centercrop() .transform(new roundtransform(this,30));

  glide.with(this).load(url) //图片地址

  .apply(options)

  .into(imagview);

  public class roundtransform extends bitmaptransformation {

  private static float radius = 0f;

  public roundtransform(context context) {

  this(context, 4);

  }

  public roundtransform(context context, int dp) {

  super(context);

  this.radius = resources.getsystem().getdisplaymetrics().density * dp;

  }

  @override

  protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) {

  bitmap bitmap = transformationutils.centercrop(pool, totransform, outwidth, outheight);

  return roundcrop(pool, bitmap);

  }

  private static bitmap roundcrop(bitmappool pool, bitmap source) {

  if (source == null) return null;

  bitmap result = pool.get(source.getwidth(), source.getheight(), bitmap.config.argb_8888);

  if (result == null) {

  result = bitmap.createbitmap(source.getwidth(), source.getheight(), bitmap.config.argb_8888);

  } 无锡人流多少钱 http://www.bhnfkyy.com

  canvas canvas = new canvas(result);

  paint paint = new paint();

  paint.setshader(new bitmapshader(source, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp));

  paint.setantialias(true);

  rectf rectf = new rectf(0f, 0f, source.getwidth(), source.getheight());

  canvas.drawroundrect(rectf, radius, radius, paint);

  return result;

  }

  public string getid() {

  return getclass().getname() + math.round(radius);

  }

  @override

  public void updatediskcachekey(messagedigest messagedigest) {

  }

  }