Android基于Fresco实现圆角和圆形图片

作者:YX_BB 时间:2023-02-14 20:03:29 

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通常会给我们这样一张图作为默认图片

Android基于Fresco实现圆角和圆形图片

理论上来讲,只需要加入下列这行代码,就可以完成这部分工作了

app:placeholderImage="@drawable/ic_avatar_default"

然而圆形图片本身已经是圆形的了,在有些机型上就出现了这个样式。

Android基于Fresco实现圆角和圆形图片

搜索了一波,自带的属性都不能解决这个问题,干脆自己来定义这个圆形的实现吧,同时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

标签:Android,圆角,圆形
0
投稿

猜你喜欢

  • 分享java中设置代理的两种方式

    2023-10-28 10:48:52
  • Java servlet后端开发超详细教程

    2022-11-01 06:13:50
  • 详解JavaScript中的函数声明和函数表达式

    2023-04-26 01:56:07
  • C语言运算符优先级列表(超详细)

    2023-07-04 08:17:54
  • 解析Tars-Java客户端源码

    2023-04-08 01:18:39
  • java实现短地址服务的方法(附代码)

    2023-11-15 19:13:41
  • IDEA集成MyBatis Generator插件的使用

    2023-08-12 00:28:47
  • 如何在IDEA Maven项目中导入本地jar包的步骤

    2023-03-25 06:43:48
  • C#Winform窗口移动方法

    2023-04-10 04:46:18
  • Android LayerDrawable超详细讲解

    2023-12-03 16:36:30
  • Java实现矩阵加减乘除及转制等运算功能示例

    2023-07-05 04:49:53
  • 深入了解ViewPager2的使用

    2021-07-06 17:26:26
  • DevExpress GridView自动滚动效果

    2022-01-19 09:08:54
  • Java 并发编程之线程挂起、恢复与终止

    2022-02-18 11:51:51
  • Java基于递归和循环两种方式实现未知维度集合的笛卡尔积算法示例

    2021-07-19 04:41:18
  • JAVA中Comparable接口和自定义比较器示例讲解

    2023-11-20 22:16:32
  • C#使⽤XmlReader和XmlWriter操作XML⽂件

    2023-12-13 10:25:00
  • C#如何对多线程、多任务管理(demo)

    2023-05-19 06:00:21
  • 浅谈Java解释器模式

    2021-08-23 23:45:59
  • Springboot导出文件,前端下载文件方式

    2023-07-21 11:27:05
  • asp之家 软件编程 m.aspxhome.com