CardView 设置其中一个圆角的大小(自定义其中一个圆角的大小)(设置指定圆角大小)

 

 

样式:

使用方法:

    <RadiusCardView所在路径.RadiusCardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:rcv_topLeftRadiu="20dp"
        app:rcv_topRightRadiu="40dp"
        app:rcv_bottomLeftRadiu="10dp"
        app:rcv_bottomRightRadiu="60dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="@color/color295C8E"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/color9E7900"/>

        </LinearLayout>

    </RadiusCardView所在路径.RadiusCardView>

 

源码:

重写CardView的onDraw方法

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;

/**
 * Created by jingzz on 2020/3/20.
 */
public class RadiusCardView extends CardView {

    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;
    }
}
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>

 

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值