广告
返回顶部
首页 > 资讯 > 精选 >Android ImageView 设置圆角及外边框样式
  • 845
分享到

Android ImageView 设置圆角及外边框样式

androidkotlin 2023-08-19 10:08:24 845人浏览 薄情痞子
摘要

目录 ImageView 设置圆角及外边框样式一、设置圆角逻辑:裁剪画布二、设置外边框逻辑:在绘制 Drawable 后再绘制外边框三、在项目中集成1. 添加依赖2. 控件样式3. 在 xml 中使用(1)分别指定4个圆角的大小(2

ImageView 设置圆角及外边框样式

我们通常可以通过以下几种方式来实现:

  1. 通过图片加载库,比如 Glide 或 Fresco 设置图片转换规则来剪裁 bitmap 实现圆角,通过绘制圆角矩形边框来实现外边框样式;
  2. 通过自定义的父布局包裹 ImageView,在父布局中设置指定的路径裁剪画布,实现圆角;
  3. 在 ImageView 中设置指定路径及画笔裁剪画布,绘制边框,实现圆角及外边框样式;

本篇主要介绍的是第 3 种方式,先来看效果图:

设置圆角效果图设置圆角及外边框效果图


一、设置圆角逻辑:裁剪画布

// 以下是伪代码// 指定浮点数组,包含 4 组 [x,y] 半径值,顺序:左上,右上,右下,左下val radii = floatArrayOf(    topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,    bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius)// 设置圆角矩形坐标val rectF = RectF()rectF.set(left, top, right, bottom)// 设置上下左右 4 个圆角路径val path = Path()path.addRoundRect(rectF, radii, Path.Direction.CW)// 或者 设置圆形path.addCircle(radius, radius, radius, Path.Direction.CW)// 裁剪画布canvas.clipPath(path)super.onDraw(canvas)

二、设置外边框逻辑:在绘制 Drawable 后再绘制外边框

// 以下是伪代码// 指定浮点数组,包含 4 组 [x,y] 半径值,顺序:左上,右上,右下,左下val radii = floatArrayOf(    topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,    bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius)// 设置外边框矩形坐标val borderRectF = RectF()borderRectF.set(left, top, right, bottom)// 设置外边框矩形路径val borderPath = Path()borderPath.addRoundRect(borderRectF, radii, Path.Direction.CW)// 设置外边框画笔val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {    color = borderColor    strokeWidth = borderWidth    style = Paint.Style.STROKE}// 先让系统完成 Drawable 绘制super.onDraw(canvas)// 再绘制圆角矩形边框canvas.drawPath(borderPath, borderPaint)// 或者 绘制圆形边框canvas.drawCircle(radius, radius, radius, borderPaint)

示例中效果都已实现,代码已发布到 MavenCentral 仓库,可直接在项目中集成使用


三、在项目中集成

1. 添加依赖

repositories {    mavenCentral()}
implementation 'io.GitHub.weilianyang:RoundImageView:1.0.2'

2. 控件样式

<declare-styleable name="RoundImageView">        <attr name="riv_radius" fORMat="dimension" />        <attr name="riv_topLeft_radius" format="dimension" />        <attr name="riv_topRight_radius" format="dimension" />        <attr name="riv_bottomLeft_radius" format="dimension" />        <attr name="riv_bottomRight_radius" format="dimension" />        <attr name="riv_roundAsCircle" format="boolean" />        <attr name="riv_borderColor" format="color" />        <attr name="riv_borderWidth" format="dimension" />declare-styleable>

3. 在 xml 中使用

(1)分别指定4个圆角的大小

<com.william.widget.RoundImageView    Android:layout_width="150dp"    android:layout_height="150dp"    android:scaleType="centerCrop"    app:riv_bottomLeft_radius="32dp"    app:riv_bottomRight_radius="25dp"    app:riv_topLeft_radius="14dp"    app:riv_topRight_radius="20dp" />

(2)作为圆形图片使用

<com.william.widget.RoundImageView    android:layout_width="150dp"    android:layout_height="150dp"    android:scaleType="centerCrop"    app:riv_roundAsCircle="true" />

(3)设置外边框宽度和颜色

<com.william.widget.RoundImageView    android:layout_width="150dp"    android:layout_height="150dp"    android:scaleType="centerCrop"    app:riv_borderColor="#ff00ff"    app:riv_borderWidth="5dp"    app:riv_radius="1dp" />

4. 在 代码 中使用

(1)指定圆角大小及边框样式

fun setRadiusAndBorder(    radius: Float,    borderWidth: Float = 0f,    @ColorInt borderColor: Int = 0,    asCircle: Boolean = false,) {    this.radius = radius    this.borderWidth = borderWidth    this.borderColor = borderColor    this.roundAsCircle = asCircle    updateBorderPaint()}

(2)分别指定4个圆角的大小及边框样式

fun setRadiusAndBorder(    topLeftRadius: Float = 0f,    topRightRadius: Float = 0f,    bottomLeftRadius: Float = 0f,    bottomRightRadius: Float = 0f,    borderWidth: Float = 0f,    @ColorInt borderColor: Int = 0) {    this.topLeftRadius = topLeftRadius    this.topRightRadius = topRightRadius    this.bottomLeftRadius = bottomLeftRadius    this.bottomRightRadius = bottomRightRadius    this.borderWidth = borderWidth    this.borderColor = borderColor    updateBorderPaint()}

github 源码RoundImageView

来源地址:https://blog.csdn.net/java_android_man/article/details/127115286

--结束END--

本文标题: Android ImageView 设置圆角及外边框样式

本文链接: https://www.lsjlt.com/news/375283.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android ImageView 设置圆角及外边框样式
    目录 ImageView 设置圆角及外边框样式一、设置圆角逻辑:裁剪画布二、设置外边框逻辑:在绘制 Drawable 后再绘制外边框三、在项目中集成1. 添加依赖2. 控件样式3. 在 xml 中使用(1)分别指定4个圆角的大小(2...
    99+
    2023-08-19
    android kotlin
  • css3中设置圆角边框的样式有哪些
    这篇文章主要为大家展示了“css3中设置圆角边框的样式有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css3中设置圆角边框的样式有哪些”这篇文章吧。 ...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作