Android圆角ViewGrup/View库(可加边框),妈妈再也不用担心设计圆角了!

项目地址:RoundCorners

比较常用的ViewGroup和View的圆角实现(可加边框),一发治好设计的圆角病。

Demo

效果预览

特点

  • LinearLayout、RelativeLayout、FrameLayout、ViewPager支持圆角
  • ImageView、TextView、View、Button支持圆角
  • CircleImageView(圆形图片)
  • 支持边框(不遮挡图片)
  • 可正常设置ripple(波纹不会突破边框)
  • 使用xml进行配置,使用简单

基本用法

Step 1. 添加JitPack仓库
在项目根目录下的 build.gradle 中添加仓库:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Step 2. 添加项目依赖

dependencies {
    implementation 'com.github.KuangGang:RoundCorners:1.0.2'
}

Step 3. 在布局文件中添加需要的RoundCorners

<com.kproduce.roundcorners.CircleImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@mipmap/ic_test"
    app:rStrokeColor="@android:color/holo_red_dark"
    app:rStrokeWidth="5dp" />

<com.kproduce.roundcorners.RoundImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:src="@mipmap/ic_test"
    app:rRadius="30dp"/>

<com.kproduce.roundcorners.RoundTextView
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@android:color/holo_blue_dark"
    android:gravity="center"
    android:text="Hello!"
    android:textColor="@android:color/white"
    android:textSize="40sp"
    app:rRightRadius="30dp" />

<com.kproduce.roundcorners.RoundRelativeLayout
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:rTopRightRadius="30dp"
    app:rBottomRightRadius="30dp"
    app:rStrokeColor="@android:color/holo_green_dark"
    app:rStrokeWidth="5dp">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark" />
</com.kproduce.roundcorners.RoundRelativeLayout>

<com.kproduce.roundcorners.RoundTextView
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@android:color/holo_blue_dark"
    android:gravity="center"
    android:text="Hello!"
    android:textColor="@android:color/white"
    android:textSize="40sp"
    app:rLeftRadius="50dp"
    app:rRightRadius="50dp"/>
……

支持的属性、方法

属性名含义默认值
rRadius统一设置四个角的圆角半径0dp
rLeftRadius左边两个角圆角半径0dp
rRightRadius右边两个角圆角半径0dp
rTopRadius上边两个角圆角半径0dp
rBottomRadius下边两个角圆角半径0dp
rTopLeftRadius左上角圆角半径0dp
rTopRightRadius右上角圆角半径0dp
rBottomLeftRadius左下角圆角半径0dp
rBottomRightRadius右下角圆角半径0dp
rStrokeWidth边框宽度0dp
rStrokeColor边框颜色Color.WHITE

原理浅解

Android View的绘制流程
View的绘制看一下这篇文章即可,代码版本比较早,但是逻辑基本相同。

  1. 使用Path的addRoundRect方法,将需要剪切的圆角半径进行设置。
  2. 所有View和ViewGroup的绘制都需要经过draw方法,在draw结束之后使用第一步的Path进行画布切割。
  3. 注意在draw中减少创建对象次数。

版本记录

版本号更新内容
1.0.21.增加边框
2.增加RoundButton/RoundViewPager
1.0.11.修复低版本系统圆角View黑框问题
2.增加CircleImageView
1.0.0First Version
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老 匡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值