Android基于Fresco怎么实现圆角和圆形图片
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"Android基于Fresco怎么实现圆角和圆形图片"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Android基于Fresco怎么实现圆角
千家信息网最后更新 2026年11月02日Android基于Fresco怎么实现圆角和圆形图片
这篇文章主要介绍"Android基于Fresco怎么实现圆角和圆形图片"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Android基于Fresco怎么实现圆角和圆形图片"文章能帮助大家解决问题。
Fresco本身已经实现了圆角以及圆形图片的功能。
工作中,遇到圆形头像的时候,UI通常会给我们这样一张图作为默认图片

理论上来讲,只需要加入下列这行代码,就可以完成这部分工作了
app:placeholderImage="@drawable/ic_avatar_default"
然而圆形图片本身已经是圆形的了,在有些机型上就出现了这个样式。

搜索了一波,自带的属性都不能解决这个问题,干脆自己来定义这个圆形的实现吧,同时Fresco自带的圆角效果只能保证使用统一的半径,想要让四个圆角的半径不同,只能在java文件中设置,不够灵活,定义圆角半径的属性也需要做些变更。
思路:自定义RoundImageView继承自 SimpleDraweeVie,具备其所有的功能。
Canvas的clipPath(Path path)可以根据Path,将Canvas剪裁成我们想要的图形。
public class RoundImageView extends SimpleDraweeView { private final static int DEFAULT_VALUE = 0; private float mWidth; private float mHeight; private Path mPath; // 圆角角度 private float mCornerRadius; // 左上角圆角角度 private float mLeftTopRadius; // 右上角圆角角度 private float mRightTopRadius; // 右下角圆角角度 private float mRightBottomRadius; // 左下角圆角角度 private float mLeftBottomRadius; // 是否使用圆形图片 private boolean mAsCircle; // 圆形图片半径 private float mRadius; public RoundImageView(Context context) { this(context, null); } public RoundImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initData(); initAttrs(context, attrs); } private void initData() { mPath = new Path(); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView); mCornerRadius = typedArray.getDimension(R.styleable.RoundImageView_cornerRadius, DEFAULT_VALUE); mAsCircle = typedArray.getBoolean(R.styleable.RoundImageView_asCircle, false); if (mCornerRadius <= 0) { // 说明用户没有设置四个圆角的有效值,此时四个圆角各自使用自己的值 mLeftTopRadius = typedArray.getDimension(R.styleable.RoundImageView_leftTopRadius, DEFAULT_VALUE); mRightTopRadius = typedArray.getDimension(R.styleable.RoundImageView_rightTopRadius, DEFAULT_VALUE); mRightBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_rightBottomRadius, DEFAULT_VALUE); mLeftBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_leftBottomRadius, DEFAULT_VALUE); } else { // 使用了统一的圆角,因此使用mCornerRadius统一的值 mLeftTopRadius = mCornerRadius; mRightTopRadius = mCornerRadius; mRightBottomRadius = mCornerRadius; mLeftBottomRadius = mCornerRadius; } typedArray.recycle(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mWidth = getWidth(); mHeight = getHeight(); // 如果开启了圆形标记 if (mAsCircle) { mRadius = Math.min(mWidth / 2, mHeight / 2); } } @Override protected void onDraw(Canvas canvas) { // 如果开启了圆形标记,圆形图片的优先级高于圆角图片 if(mAsCircle) { drawCircleImage(canvas); } else { drawCornerImage(canvas); } super.onDraw(canvas); } /** * 画中间圆形 * @param canvas */ private void drawCircleImage(Canvas canvas) { mPath.addCircle(mWidth / 2, mHeight / 2, mRadius, Path.Direction.CW); canvas.clipPath(mPath); } /** * 画圆角 * @param canvas */ private void drawCornerImage(Canvas canvas) { if (mWidth > mCornerRadius && mHeight > mCornerRadius) { // 设置四个角的x,y半径值 float[] radius = {mLeftTopRadius, mLeftTopRadius, mRightTopRadius, mRightTopRadius, mRightBottomRadius, mRightBottomRadius, mLeftBottomRadius, mLeftBottomRadius}; mPath.addRoundRect(new RectF(0,0, mWidth, mHeight), radius, Path.Direction.CW); canvas.clipPath(mPath); } }}attr属性如下
关于"Android基于Fresco怎么实现圆角和圆形图片"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
圆角
圆形
图片
半径
角度
属性
知识
不同
功能
行业
问题
工作
实用
不够
代码
内容
同时
图形
头像
实用性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技
什么是科技互联网
南京欣网互联网络科技有限公司
互联网科技工作委员会主任
中关村互联网 科技集团
涨乐互联网科技
互联网核心科技是什么
艾特科技互联网
无线互联网科技
长亮科技 互联网金融
互联网科技素材视频竖屏
昭通云南众福互联网科技有限公司
中和天地互联网科技有限公司
黑科技 体验场景 互联网大会
互联网科技年终奖
扫呗互联网科技有限公司
互联网科技企业商标注意事项
安徽零酒互联网科技有限公司
山东蛋芒互联网科技有限公司
武汉亿视互联网科技有限公司
A股互联网金融科技公司
科技信息互联网
沃里互联网科技
人人互联网科技是培训
厦门千工互联网科技有限公司
民营互联网公司国防科技
互联网科技和法律行业
群盈汇互联网科技有限公司
互联网高科技行业有哪些企业
东风日产互联网科技公司叫什么