View实现圆角的几种方式

文章目录

    • 1.通过给view设置background实现圆角
    • 2.通过glide加载图片设置圆角
    • 3.通过CardView实现圆角
    • 4.利用View 的 ViewOutlineProvider 实现圆角

1.通过给view设置background实现圆角

这种方式是通过shape设置背景色的方式实现圆角,不影响view的绘制区域,只是通过设置背景色影响显示区域来实现圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><cornersandroid:bottomLeftRadius="11dp"/><solid android:color="@color/element_tcl_navigation_item_bg_middle_focus_color" />
</shape>

corner标签代表圆角,支持设置4个圆角一个单个圆角设置

bottomLeftRadius :左下圆角

bottomRightRadius : 右下圆角

topLeftRadius : 左上圆角

topRightRadius :右上圆角

solid标签代表填充色,纯色填充

gradient标签代表渐变色填充

shape也支持java代码创建:

GradientDrawable在Android 中便是shape标签的代码实现,

val gradientDrawable = background as GradientDrawable
gradientDrawable.setColor(Color.GRAY)
gradientDrawable.cornerRadius = 10F
view.background = gradientDrawable

上述就是代码动态创建shape的示例

想要设置某个圆角可以使用

val radii = floatArrayOf(corner,corner,0f,0f,0f,0f,corner,corner)
gradientDrawable.cornerRadii = radii

setCornerRadii(@Nullable float[] radii) 该方法实现某个圆角的设置,对应关系就是8位数组是左上,右上,右下,左下,两位对应一个圆角,值就是想要的圆角值。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:left="5dp"android:right="5dp"><shape><solid android:color="@color/white" /><cornersandroid:bottomLeftRadius="10dp"android:topLeftRadius="10dp" /></shape></item></layer-list>

需要注意这种layer-list的写法,这种需要代码动态创建时需要用到LayerDrawable

这种方式需要动态修改shape的时候需要

 val layerDrawable = background as LayerDrawableval drawable = layerDrawable.getDrawable(0)val drawables = arrayOf(drawable)val realLayoutDrawable = LayerDrawable(drawables)//layer drawable left 和 right的值realLayoutDrawable.setLayerInset(0, 5,0,5,0 )view.background = realLayoutDrawable

layerDrawable相当于一个数组中包含了很多GradientDrawable,需要修改时,需要先get出来 修改后再放入即setLayerInset(int index, int l, int t, int r, int b)

2.通过glide加载图片设置圆角

    fun loadImageIntoBackgroundWithSomeCorner(view: View,context: Context,url: String,topLeftCorner: Int,topRightCorner: Int,bottomLeftCorner: Int,bottomRightCorner: Int,imageLoaderDrawableListener: ImageLoaderDrawableListener) {val roundedCorners = GranularRoundedCorners(topLeftCorner.toFloat(),topRightCorner.toFloat(),bottomRightCorner.toFloat(),bottomLeftCorner.toFloat())val requestOptions =RequestOptions().transform(CenterCrop(), roundedCorners)Glide.with(context).asBitmap().load(url).apply(requestOptions).into(object : CustomViewTarget<View, Bitmap>(view) {override fun onLoadFailed(errorDrawable: Drawable?) {imageLoaderDrawableListener.onLoadFail()}override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {if (resource != null && !resource.isRecycled) {imageLoaderDrawableListener.onLoadImageSuccess(url, BitmapDrawable(context.resources, resource))}}override fun onResourceCleared(placeholder: Drawable?) {}})}

Glide加载图片圆角通过设置RequestOptions来实现

GranularRoundedCorners(float topLeft, float topRight, float bottomRight, float bottomLeft) {this.topLeft = topLeft;this.topRight = topRight;this.bottomRight = bottomRight;this.bottomLeft = bottomLeft;
}

GranularRoundedCorners可以设置单个圆角

RoundedCorners(int roundingRadius)

RoundedCorners是直接设置4个圆角

3.通过CardView实现圆角

CardView 是自带圆角实现的,我们只需要在它的定义中加一句 app:cardCornerRadius=”10dp” 即可。

<?xml version="1.0" encoding="utf-8"? 
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent" <androidx.cardview.widget.CardViewandroid:layout_width="256dp"android:layout_height="128dp"app:cardBackgroundColor="#0084FF"app:cardCornerRadius="10dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" / </androidx.constraintlayout.widget.ConstraintLayout 

CardView是原生组件,缺点是只支持四个圆角设置

4.利用View 的 ViewOutlineProvider 实现圆角

ViewOutlineProvider 是Android 5.0之后新增的设置圆角的方式。

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class VideoListOutlineProvider(private val radius: Int, private val ancorView: View?) :ViewOutlineProvider() {companion object {const val radiusLeft = 1const val radiusRight = 2}//圆角左边或者右边private var radiusDirection = radiusLeftconstructor(radiusDirection: Int, radius: Int, ancorView: View?) : this(radius, ancorView) {this.radiusDirection = radiusDirection}override fun getOutline(view: View, outline: Outline?) {if (radiusDirection == radiusLeft) {outline?.setRoundRect(0, 0, view.width + radius, view.height, radius.toFloat())} else if (radiusDirection == radiusRight) {outline?.setRoundRect(-radius, 0, view.width, view.height, radius.toFloat())}}
}

这种实现方式,本质上是修改了 View 的轮廓。

本质来说,此种方式只支持设置4个圆角,但是可以控制裁剪区域实现单个圆角的绘制,需要注意识别清楚裁剪区域,不然会导致view直接被裁剪丢弃。具体参照上述实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/59874.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言数值表示——进制、数值存储方式

进制 进制也就是进位制&#xff0c;是人们规定的一种进位方法对于任何一种进制—X进制&#xff0c;就表示某一位置上的数运算时是逢X进一位 十进制是逢十进一&#xff0c;十六进制是逢十六进一&#xff0c;二进制就是逢二进一&#xff0c;以此类推&#xff0c;x进制就是逢x进位…

Visual Studio中平台和配置的概念

在 Visual Studio 中&#xff0c;“平台”&#xff08;Platform&#xff09;和 “配置”&#xff08;Configuration&#xff09;是用于管理项目构建和设置的两个关键概念。在 “解决方案配置管理器” 中设置和管理 平台&#xff08;Platform&#xff09;&#xff1a; 指项目构…

虹科荣誉丨最佳雇主!虹科荣获2023年度最佳数智化雇主奖项

2023年度最佳数智化雇主 广州虹科电子科技有限公司 由《中国经营报》和科锐国际联合发起的“2023卓越雇主品牌”申报活动中&#xff0c;经过专业评选机构及权威媒体等选拔&#xff0c;广州虹科电子科技有限公司荣获2023年度最佳数智化雇主奖。 虹科&#xff1a;您可靠的解决方…

《动手学深度学习》-57长短期记忆网络LSTM

沐神版《动手学深度学习》学习笔记&#xff0c;记录学习过程&#xff0c;详细的内容请大家购买书籍查阅。 b站视频链接 开源教程链接 长短期记忆网络&#xff08;LSTM&#xff09; 长期以来&#xff0c;隐变量模型存在长期信息保存和短期输入缺失的问题。解决这一问题的最早…

ESP32-CAM模块Arduino环境搭建测试

ESP32-CAM模块Arduino环境搭建测试 一.ESP32OV2640摄像头模块CameraWebServer视频查看 二.测试ESP32-CAM(后续称cam模块)代码是否上传执行成功测试 const int led0 12; const int led1 13;void setup() {// put your setup code here, to run once:pinMode(led0, OUTPUT);pin…

In-Context Retrieval-Augmented Language Models

本文是LLM系列文章&#xff0c;针对《In-Context Retrieval-Augmented Language Models》的翻译。 上下文检索增强语言模型 摘要1 引言2 相关工作3 我们的框架4 实验细节5 具有现成检索器的上下文RALM的有效性6 用面向LM的重新排序改进上下文RALM7 用于开放域问答的上下文RALM…

JS小球绕着椭圆形的轨迹旋转并且近大远小

在ivx中案例如下&#xff1a; VxEditor 效果如下&#xff0c;近大远小 主要代码如下&#xff1a; const centerX 360 / 2; // 椭圆中心的X坐标 const centerY 120 / 2; // 椭圆中心的Y坐标 const a 100; // 长半轴 const b 60; // 短半轴const elementsWithClassName d…

Vim快捷键及使用技巧

Vim的几种模式: ● 普通模式:打开文件时的默认模式,在其他模式下按下ESC键都可返回到该模式。 ● 插入模式:按i/o/a键进入该模式,进行文本编辑操作,不同之处在于插入字符的位置在光标之前还是之后。 ● 命令行模式:普通模式下输入冒号(:)后会进入该模式,在该模式…

QEMU 仿真RISC-V freeRTOS 程序

1. 安裝RISC-V 仿真環境 --QEMU 安裝包下載地址: https://www.qemu.org/ 安裝命令及安裝成功效果如下所示, target-list 設定爲riscv32-softmmu, $ cat ~/project/qemu-8.0.4/install.sh sudo apt-get install libglib2.0-dev sudo apt-get install libpixman-1-dev ./co…

设计模式之组合模式

文章目录 一、介绍二、案例 一、介绍 组合模式(Composite Pattern)&#xff0c;属于结构型设计模式。组合模式常用于树形的数据结构&#xff0c;比如&#xff1a;多级菜单、部门层级关系、html文本中的dom树。它的特点是使用户对单个对象和组合对象的使用是相同的。 二、案例…

golang逃逸技术分析

“ 申请到栈内存好处&#xff1a;函数返回直接释放&#xff0c;不会引起垃圾回收&#xff0c;对性能没有影响。 申请到堆上面的内存才会引起垃圾回收。 func F() { a : make([]int, 0, 20) b : make([]int, 0, 20000) l : 20 c : make([]int, 0, l)} “ a和b代码一样&#xff0…

自动化管理管理工具----Ansible

目录 ​编辑 一、Ansible概念 1.1特点 二、工作机制&#xff08;日常模块&#xff09; 2.1 核心程序 三、Ansible 环境安装部署 四、ansible 命令行模块 4.1command 模块 4.2shell 模块 4.3cron 模块 4.4user 模块 4.5group 模块 4.6copy模块 4.7file模块 4.8ho…

保护网站安全:学习蓝莲花的安装和使用,复现跨站脚本攻击漏洞及XSS接收平台

这篇文章旨在用于网络安全学习&#xff0c;请勿进行任何非法行为&#xff0c;否则后果自负。 环境准备 一、XSS基础 1、反射型XSS 攻击介绍 原理 攻击者通过向目标网站提交包含恶意脚本的请求&#xff0c;然后将该恶意脚本注入到响应页面中&#xff0c;使其他用户在查看…

Linux 通过 Docker 部署 Nacos 2.2.3 服务发现与配置中心

目录 环境准备Nacos 数据库创建Docker 部署 Nacos1. 创建挂载目录2. 下载镜像3. 创建和启动容器4. 访问控制台 导入 Nacos 配置SpringBoot 整合 Nacospom 依赖application.yml 配置 参考官方链接微服务商城源码 环境准备 名称版本IP端口Nacos2.2.3192.168.10.218848、9848MySQ…

Linux学习之Ubuntu 20使用systemd管理OpenResty服务

sudo cat /etc/issue可以看到操作系统的版本是Ubuntu 20.04.4 LTS&#xff0c;sudo lsb_release -r可以看到版本是20.04&#xff0c;sudo uname -r可以看到内核版本是5.5.19&#xff0c;sudo make -v可以看到版本是GNU Make 4.2.1。 需要先参考我的博客《Linux学习之Ubuntu 2…

Spring-SpringBoot-SpringMVC-MyBatis常见面试题

文章目录 Spring篇springbean是安全的的?什么是AOP你们工作中有用过AOP吗spring中的事务是如何实现的spring中事务失效场景Spring的生命周期spring中的循坏依赖springMVC的执行流程springboot的启动原理常用注解MyBatis执行流程Mybatis是否支持延迟加载&#xff1f;Mybatis的一…

学习创建第一个 React 项目

目标 本篇的目标是配置好基础的环境并创建出第一个 React 项目。 由于之前没接触过相关的知识&#xff0c;所以还需要了解其依赖的一些概念。 步骤主要参考First React app using create-react-app | VS code | npx | npm - YouTube 0. 简单了解相关概念 JavaScript 一种语…

Python Qt(七)Listview

源代码&#xff1a; # -*- coding: utf-8 -*-# Form implementation generated from reading ui file qt_listview.ui # # Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not…

CLICK HOUSE

一、clickhouse简介 MPP架构的列式存储数据库&#xff08;DBMS&#xff1a;Database Management System&#xff09;&#xff0c;能够使用 SQL 查询实时生成分析数据报告。ClickHouse的全称是Click Stream&#xff0c;Data WareHouse。 ClickHouse的全称由两部分组成&#xf…