• CardView设置任意角为圆角


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

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

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

    源码:

    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());
        }
     
        @Override
        protected 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    attr文件:

        
            
            
            
            
            
            
            
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用案例:

        
     
            
                
     
                
     
            
     
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    虚拟机备份中的CBT技术
    Flink(七)【输出算子(Sink)】
    TPO69 01|Why Snakes Have Forked Tongues|阅读真题精读|10:40-11:40+15:30-16:57
    基于STM32结合CubeMX学习Free-RT-OS的源码之两类中断解析
    Docker-----emqx部署
    ARM 体系结构要点总结
    蓝帽杯2022年半决赛 writeup(附取证题目+解压密码+附件)
    ConcurrentHashMap底层原理分析(put方法)
    外汇天眼:外汇投资小白必读! 4大外汇交易常见提问释疑
    鸡尾酒学习——薄荷夏日
  • 原文地址:https://blog.csdn.net/liuguitong_BK/article/details/133945324