设置DisplayImageOptions属性的时候用这个BitmapDisplayer即可实现CenterCrop效果。

public class CenterCropRoundedBitmapDisplayer implements BitmapDisplayer {

protected final int cornerRadius;

protected final int margin;

public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels) {

this(cornerRadiusPixels, 0);

}

public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) {

this.cornerRadius = cornerRadiusPixels;

this.margin = marginPixels;

}

public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {

if(!(imageAware instanceof ImageViewAware)) {

throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");

} else {

imageAware.setImageDrawable(new CenterCropRoundedBitmapDisplayer.RoundedDrawable(bitmap, this.cornerRadius, this.margin,imageAware.getWidth(),imageAware.getHeight()));

}

}

public static class RoundedDrawable extends Drawable {

protected final float cornerRadius;

protected final int margin;

protected final RectF mRect = new RectF();

protected final RectF mBitmapRect;

protected final BitmapShader bitmapShader;

protected final Paint paint;

float scale;

float dx = 0, dy = 0;

public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin,int vwidth,int vheight) {

this.cornerRadius = (float)cornerRadius;

this.margin = margin;

this.bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

this.mBitmapRect = new RectF((float)margin, (float)margin, (float)(bitmap.getWidth() - margin), (float)(bitmap.getHeight() - margin));

this.paint = new Paint();

this.paint.setAntiAlias(true);

this.paint.setShader(this.bitmapShader);

this.paint.setFilterBitmap(true);

this.paint.setDither(true);

/*设置centercrop属性*/

if (bitmap.getWidth() * vheight > vwidth * bitmap.getHeight()) {

scale = (float) vheight / (float) bitmap.getHeight();

dx = (vwidth - bitmap.getWidth() * scale) * 0.5f;

} else {

scale = (float) vwidth / (float) bitmap.getWidth();

dy = (vheight - bitmap.getHeight() * scale) * 0.5f;

}

}

protected void onBoundsChange(Rect bounds) {

super.onBoundsChange(bounds);

this.mRect.set((float)this.margin, (float)this.margin, (float)(bounds.width() - this.margin), (float)(bounds.height() - this.margin));

Matrix shaderMatrix = new Matrix();

shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect, Matrix.ScaleToFit.FILL);

shaderMatrix.setScale(scale, scale);

shaderMatrix.postTranslate(Math.round(dx), Math.round(dy));

this.bitmapShader.setLocalMatrix(shaderMatrix);

}

public void draw(Canvas canvas) {

canvas.drawRoundRect(this.mRect, this.cornerRadius, this.cornerRadius, this.paint);

}

public int getOpacity() {

return -3;

}

public void setAlpha(int alpha) {

this.paint.setAlpha(alpha);

}

public void setColorFilter(ColorFilter cf) {

this.paint.setColorFilter(cf);

}

}

}

glide scaletype 无效_android 自定义圆角ImageView,后设置scaleType=centerCrop无效?相关推荐

  1. 安卓自定义view中 绘画基本图形点线面,矩形,方形,圆,扇形,文字及沿着特定方向布局,自定义圆角ImageView图片等等相关api使用方法及举例

    安卓自定义view中 绘画基本图形点线面,矩形,方形,圆,扇形,文字及沿着特定方向布局,自定义圆角ImageView图片等等相关api使用方法及举例,图片压缩处理逻辑 本文旨在介绍自定义View的实现 ...

  2. android imageview 锯齿,[置顶] android 自定义圆角ImageView以及锯齿的处理

    看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...

  3. android imageview 加边框,RCImageView 自定义圆角ImageView,带边框效果

    概况 RCImageView 圆型或者圆角图片(带有边框效果),适配了ImageView的ScaleType属性 RoundImageView 定义圆角图片,四个角圆角弧度可各自定义,也可不定义默认角 ...

  4. android imageview 锯齿,android 自定义圆角ImageView以及锯齿的处理

    看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...

  5. [置顶] android 自定义圆角ImageView以及锯齿的处理

    看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...

  6. 计算机 设置登录密码 无效,bios设置开机密码无效怎么办

    bios可从CMOS中读写系统设置的具体信息. 其主要功能是为计算机提供最底层的.最直接的硬件设置和控制.那么bios设置开机密码无效怎么办呢?今天学习啦小编就和大家说说bios设置开机密码无效的解决 ...

  7. Android图片缓存框架 - Glide自定义圆角 (五)

    目录 1. Glide API 2. Glide导入包 3. Glide 7种加载图片方式 4. 取消加载 5 Glide加载GIF 6. Glide的RequestOption 7. Glide自定 ...

  8. Glide加载圆形图片和自定义圆角图片和对指定的角加载圆角

    先上效果图: 因注释很细,就不一一解释说明了! 1.Glide加载圆形图片: 自定义GlideCircleTransUtils继承BitmapTransformation,重写transform()方 ...

  9. android圆角ImageView的几种实现方式

    前言 好长一段时间没写博客,继续吧.今天突然想起金三银四的时候,面试遇到的一个问题:如何实现圆角imageView.所在公司只用了其中一种方式,今天总结一下: 第一种:Glide加载图片自带api 如 ...

  10. Android显示九宫图(自定义圆角,仿微信九宫格图)

    详细解析Android显示九宫图(自定义圆角,仿微信九宫格图) 这是一个自定义九宫格图片框架,里面有设置圆角大小,还有当图片一张的时候控件自定义的大小,图片的间隔,四张图片的时候图片自定义为两行两列等 ...

最新文章

  1. php怎样验证验证码对错,PHP生成中文验证码并检测对错实例
  2. JSON JsonArray和JsonObject学习资料
  3. Android使用缓存优化ListView
  4. c++编译时候fatal error C1075: end of file found before the left brace '{' at
  5. javafor循环打印图案_C程序使用循环打印盒子图案
  6. 第2节 mapreduce深入学习:15、reduce端的join算法的实现
  7. Mac终端命令失效( command not found)/
  8. android分区调整大小写,小米2 32G开发版 无损分区调整
  9. JDK动态代理(通俗白话)
  10. inspinia中文管理后台_JAVA项目实战开发电商项目案例(六与七)商品分类与商品模块管理开发
  11. 手机网页 弹窗layerUI
  12. 对于离散行业如何选型MES系统,你知道吗?
  13. CF487E Tourists
  14. 阅读笔记--神经网络与深度学习(邱锡鹏)
  15. 自动生成用于测试和评估自动驾驶汽车的各种挑战性场景
  16. 超级计算机也无法算尽圆周率,圆周率到底能不能算尽?
  17. html可识别的字体,7款有用的工具来识别字体
  18. webpack基本使用
  19. 二次方程计算器-字符串处理
  20. 1351:面朝大海 春暖花开 [ 数据加强版 ]

热门文章

  1. vmware安装报错及注册时无权输入许可证密钥的解决办法及步骤
  2. stata 空间杜宾模型_空间面板数据模型及Stata实现
  3. 如何在uniapp中使用百度云实现OCR身份证识别功能
  4. Oracle批量修改字段长度
  5. 部分大学上演造假运动闯关本科评估(图)
  6. TransE,知识图谱嵌入(KGE)论文精读
  7. 【电路设计】AD17使用及PCB绘制总结
  8. java 快速删除文件夹_java File删除文件夹完整版
  9. C#安装本地nupkg包
  10. SQL server增删改查