CardView设置任意角为圆角

2023-10-21 09:04

本文主要是介绍CardView设置任意角为圆角,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注意:material:1.1.0以上版本在RadiusCardView节点下一定要添加 android:theme=“@style/Theme.MaterialComponents”,不然会报错,另外,由于是重写自MaterialCardView,所以一定要导入material包:

 implementation 'com.google.android.material:material:1.1.0'

样式:
在这里插入图片描述

源码:

RadiusCardView

import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;import com.google.android.material.card.MaterialCardView;
import com.seocoo.user.R;public class RadiusCardView extends MaterialCardView {private float tlRadiu;private float trRadiu;private float brRadiu;private float blRadiu;public RadiusCardView(Context context) {this(context, null);}public RadiusCardView(Context context, AttributeSet attrs) {this(context, attrs, R.attr.materialCardViewStyle);}public RadiusCardView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setRadius(0);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RadiusCardView);tlRadiu = array.getDimension(R.styleable.RadiusCardView_rcv_topLeftRadiu, 0);trRadiu = array.getDimension(R.styleable.RadiusCardView_rcv_topRightRadiu, 0);brRadiu = array.getDimension(R.styleable.RadiusCardView_rcv_bottomRightRadiu, 0);blRadiu = array.getDimension(R.styleable.RadiusCardView_rcv_bottomLeftRadiu, 0);setBackground(new ColorDrawable());}@Overrideprotected void onDraw(Canvas canvas) {Path path = new Path();RectF rectF = getRectF();float[] readius = {tlRadiu,tlRadiu,trRadiu,trRadiu,brRadiu,brRadiu,blRadiu,blRadiu};path.addRoundRect(rectF,readius,Path.Direction.CW);canvas.clipPath(path,Region.Op.INTERSECT);super.onDraw(canvas);}private RectF getRectF() {Rect rect = new Rect();getDrawingRect(rect);RectF rectF = new RectF(rect);return rectF;}
}

attr文件:

    <declare-styleable name="RadiusCardView"><!--        左上圆角大小--><attr name="rcv_topLeftRadiu" format="dimension" /><!--        右上圆角大小--><attr name="rcv_topRightRadiu" format="dimension" /><!--        右下圆角大小--><attr name="rcv_bottomRightRadiu" format="dimension" /><!--        左下圆角大小--><attr name="rcv_bottomLeftRadiu" format="dimension" /></declare-styleable>

使用案例:

    <RadiusCardView所在路径.RadiusCardViewandroid:layout_width="match_parent"android:layout_height="200dp"android:theme="@style/Theme.MaterialComponents" //这行一定要添加,不然会报错app:rcv_topLeftRadiu="20dp"app:rcv_topRightRadiu="40dp"app:rcv_bottomLeftRadiu="10dp"app:rcv_bottomRightRadiu="60dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:background="@color/color295C8E"/><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@color/color9E7900"/></LinearLayout></RadiusCardView所在路径.RadiusCardView>

这篇关于CardView设置任意角为圆角的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/253360

相关文章

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

uniapp设置微信小程序的交互反馈

链接:uni.showToast(OBJECT) | uni-app官网 (dcloud.net.cn) 设置操作成功的弹窗: title是我们弹窗提示的文字 showToast是我们在加载的时候进入就会弹出的提示。 2.设置失败的提示窗口和标签 icon:'error'是设置我们失败的logo 设置的文字上限是7个文字,如果需要设置的提示文字过长就需要设置icon并给

Tomcat性能参数设置

转自:http://blog.csdn.net/chinadeng/article/details/6591542 Tomcat性能参数设置 2010 - 12 - 27 Tomcat性能参数设置 博客分类: Java Linux Tomcat 网络应用 多线程 Socket 默认参数不适合生产环境使用,因此需要修改一些参数   1、修改启动时内存参数、并指定J

linux下非标准波特率的设置和使用

通常,在linux下面,设置串口使用终端IO的相关函数设置,如tcsetattr等函数,linux内部有一个对常用波特率列表的索引,根据设置的波特率用底层驱动来设置异步通信芯片的寄存器 对于非标准的任意波特率需要用ioctl(fd, TIOCGSERIAL, p)和ioctl(fd, TIOCSSERIAL, p)的配合,ioctl的最后一个参数是struct serial_struct *

linux 内核提权总结(demo+exp分析) -- 任意读写(四)

hijack_modprobe_path篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm     原理同hijack_prctl, 当用户执行错误格式的elf文件时内核调用call_usermod

linux 内核提权总结(demo+exp分析) -- 任意读写(三)

hijack_prctl篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm   prctl函数: 用户态函数,可用于定制进程参数,非常适合和内核进行交互 用户态执行prctl函数后触发prctl系统

linux 内核提权总结(demo+exp分析) -- 任意读写(二)

hijack_vdso篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm     vdso: 内核实现的一个动态库,存在于内核,然后映射到用户态空间,可由用户态直接调用 内核中的vdso如果被修改

linux 内核提权总结(demo+exp分析) -- 任意读写(一)

cred篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm   每个线程在内核中都对应一个线程结构块thread_infothread_info中存在task_struct类型结构体 struct t

win7如何设置SATA硬盘

Win7在安装时设置的是IDE,安装完后需要在注册表中设置为SATA,否则直接设BIOS会不认硬盘,具体如下 注册表子项:HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Msahci 找到Start键,将值0改为3

centOS7.0设置默认进入字符界面

刚装的,带有x window桌面,每次都是进的桌面,想改成自动进命令行的。记得以前是修改 /etc/inittab 但是这个版本inittab里的内容不一样了没有id:x:initdefault这一行而且我手动加上也不管用,这个centos 7下 /etc/inittab 的内容 Targets systemd uses targets which serve a simil