Android中ImageView半边圆角处理记录2

Android2023年2月21日 am8:08发布1年前 (2023)更新 3XCN.COM站长
0 0 0
广告也精彩
目录

前言

之前介绍过对ImageView进行圆角处理,具体文章《Android中ImageView半边圆角处理记录 -125啦读书导航 - 阅读是一种生活方式 ()》,后面发现网上还有一种更简单的一种方式。

  1. 有点:代码少,简单
  2. 缺点:边界不够圆滑(存在锯齿)

正文

具体效果如下(左侧是RoundImageView2,右侧是之前RoundImageView),看圆角处,RoundImageView2就存在锯齿。

Android中ImageView半边圆角处理记录2

代码片段

public class RoundImageView2 extends androidx.appcompat.widget.AppCompatImageView {

    private final Path mPath;
    private float mWidth, mHeight;

    public RoundImageView2(Context context) {
        this(context, null);
    }

    public RoundImageView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPath = new Path();
    }

    @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();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int radius = 20; //圆角半径
        if (mWidth > radius && mHeight > radius) {
            mPath.moveTo(radius, 0);
            mPath.lineTo(mWidth - radius, 0);
            mPath.quadTo(mWidth, 0, mWidth, radius);
            mPath.lineTo(mWidth, mHeight - radius);
            mPath.quadTo(mWidth, mHeight, mWidth - radius, mHeight);
            mPath.lineTo(radius, mHeight);
            mPath.quadTo(0, mHeight, 0, mHeight - radius);
            mPath.lineTo(0, radius);
            mPath.quadTo(0, 0, radius, 0);
            canvas.clipPath(mPath);
        }
        super.onDraw(canvas);
    }

}

参考文章

  1. Android中ImageView半边圆角处理记录 -125啦读书导航 - 阅读是一种生活方式 ()

 历史上的今天

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

相关文章

广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...

网站升级中

公告

近期网站升级中,可能存在一些bug。欢迎反馈 https://www.91es.com/we.html

本站域名

本站域名 : 91es.com3xcn.com。本站邮箱 : 站长邮箱 i@oorr.cn,通知邮箱we@oorr.cn ,如有更新,请看公告 。