Android基于Fresco实现圆角和圆形图片
作者:YX_BB 发布时间:2023-02-14 20:03:29
标签:Android,圆角,圆形
Fresco是FaceBook开源的Android平台图片加载库,可以从网络,从本地文件系统,本地资源加载图片
Fresco本身已经实现了圆角以及圆形图片的功能。
<!--圆形图片,一般用作头像-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
app:placeholderImage="@drawable/ic_avatar_default"
app:roundAsCircle="true"/>
<!--圆角图片,为了美观大多数图片都会有这样的处理。-->
<!--当图片为正方形的时候,将roundedCornerRadius设置为边长的一半,也可以形成圆形图片的效果-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
app:placeholderImage="@drawable/ic_avatar_default"
app:roundedCornerRadius="5dp"/>
工作中,遇到圆形头像的时候,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属性如下
<!--适配android10的图片控件-->
<declare-styleable name="RoundImageView">
<!--圆形图片-->
<attr name="asCircle" format="boolean"/>
<!--左上角圆角半径-->
<attr name="leftTopRadius" format="dimension"/>
<!--右上角圆角半径-->
<attr name="rightTopRadius" format="dimension"/>
<!--右下角圆角半径-->
<attr name="rightBottomRadius" format="dimension"/>
<!--左下角圆角半径-->
<attr name="leftBottomRadius" format="dimension"/>
<!--四个圆角半径,会覆盖上边四个圆角值-->
<attr name="cornerRadius" format="dimension"/>
</declare-styleable>
来源:https://blog.csdn.net/YX_BB/article/details/104561359


猜你喜欢
- 直接插入排序直接插入排序的思路很容易理解,它是这样的:1.把待排序的数组分成已排序和未排序两部分,初始的时候把第一个元素认为是已排好序的。2
- android:id 为控件指定相应的ID android:text 指定控件的文本,置尽量使用strings.xml android:gr
- 在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInfla
- 本文实例为大家分享了Android实现可复用的筛选页面的具体代码,供大家参考,具体内容如下窗口代码/** * 筛选页面 * 1.将用户的输入
- 发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson。GitHub主页:http
- 一、序言Java多线程编程线程池被广泛使用,甚至成为了标配。线程池本质是池化技术的应用,和连接池类似,创建连接与关闭连接属于耗时操作,创建线
- 在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转
- @ConditionalOnProperty作用及用法在spring boot中有时候需要控制配置类是否生效,可以使用@Conditiona
- 一、效果图二、描述更改Android项目中的语言,这个作用于只用于此APP,不会作用于整个系统三、解决方案(一)布局文件<Linear
- 接着上一篇进行学习java文件上传下载1。五、断点续传 对于熟用QQ的程序员,QQ的断点续传功能应该是印象很深刻的。因为它很实用也
- 今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查、分页、排序、事务操作等功能。下面先来介绍一下JPA中一些常用的
- 系统参数系统级全局变量,该参数在程序中任何位置都可以访问到。优先级最高,覆盖程序中同名配置。系统参数的标准格式为:-Dargname=arg
- string fileExt = Path.GetExtension(excelPath);string conn = "&quo
- 本文实例为大家分享了Android实现歌词滚动效果的具体代码,供大家参考,具体内容如下自定义TextViewpublic class Ver
- 本文实例讲述了Android编程学习之抽象类AbsListView用法。分享给大家供大家参考,具体如下:一、继承关系public abstr
- ps: 不想看代码的滑到最下面有apk包百度网盘下载地址1. 先看效果图 不然都是耍流氓2.项目目录3.一些配置build.gradlepl
- 问题使用Runtime调用python脚本一直没有结果,经排查是因为 cv2 的 import 问题java代码:python代码:在导入c
- 使用方法:先把mvcpager.dll引用加入mvc项目中。前台代码前台:@{Layout = null;}@using Webdiyer.
- 引言上文Android:实现一个自定义有限制区域的图例(角度自识别)涂鸦工具类(中)中我们已经实现了在复杂的异形区域中涂鸦,最后生成图片保存
- 现如今的APP各式各样,同样也带来了各种需求,一个下拉刷新都能玩出花样了,前两天订饭的时候不经意间看到了“百度外卖”的下拉刷新,今天的主题就