Imageview图片圆形显示
16lz
2021-01-23
1.布局文件中设置ImageView的参数
android:scaleType="fitXY"
2.创建圆形的方法
public static Bitmap circleBitmap(Bitmap source) { int width = source.getWidth(); Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawCircle(width / 2, width / 2, width / 2, paint); //设置图片相交情况下的处理方式 //setXfermode:设置当绘制的图像出现相交情况时候的处理方式的,它包含的常用模式有: //PorterDuff.Mode.SRC_IN 取两层图像交集部分,只显示上层图像 //PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source, 0, 0, paint); return bitmap;}
3.调用方法(如果使用Picasso加载网络图片,可以参考下面代码)
Picasso.with(this.getActivity()).load(user.getImageurl()).transform(new Transformation() { @Override public Bitmap transform(Bitmap bitmap) { Bitmap bitmap1 = BitmapUtils.circleBitmap(bitmap); bitmap.recycle(); return bitmap1; } @Override public String key() { return ""; }}).into(ivMeIcon);
(注:如果想先压缩成正方形在圆形处理,可以先调用下面的方法)
//图片压缩public static Bitmap zoom(Bitmap source,float width,float height){ Matrix matrix=new Matrix(); matrix.postScale(width/source.getWidth(),height/source.getHeight()); Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false); return bitmap;}
更多相关文章
- android调用图库获取图片显示在img中
- Android 使用Drawable实现圆角与圆形图案
- Android Base64转换图片,SimpleAdapter动态显示Bitmap图片
- Android使用Base64将图片与String的转换
- Android 判断两个图片相等
- Android充电进度获取,圆形波浪进度展示
- android 图片浏览器
- android之4.0控件switch自定义开关背景图片和控制宽度
- Android:指定分辨率和清晰度的图片压缩方法源码