CardView不好用,自己写个带border的圆角容器
Posted 汤米粥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CardView不好用,自己写个带border的圆角容器相关的知识,希望对你有一定的参考价值。
package com.common.base.view; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.os.Build; import android.util.AttributeSet; import android.widget.RelativeLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.common.base.R; public class RoundRelativeLayout extends RelativeLayout { private Path mPath; private Paint mPaint; private RectF mRectF; private RectF mChildRectF; private float mRadius; private float mBorderWidth = 1; public RoundRelativeLayout(@NonNull Context context) { this(context, null); } public RoundRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RoundRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundRelativeLayout); mRadius = ta.getDimension(R.styleable.RoundRelativeLayout_rlRadius, 0); mBorderWidth = ta.getDimension(R.styleable.RoundRelativeLayout_rlBorderWidth, 1); ta.recycle(); mPath = new Path(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mRectF = new RectF(); mChildRectF = new RectF(); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); } public void setRadius(float radius) { mRadius = radius; postInvalidate(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mRectF.set(0, 0, w, h); mChildRectF.set(mBorderWidth, mBorderWidth, w - mBorderWidth, h - mBorderWidth); } @SuppressLint("MissingSuperCall") @Override public void draw(Canvas canvas) { draw28(canvas); } @Override protected void dispatchDraw(Canvas canvas) { dispatchDraw27(canvas); } private void draw28(Canvas canvas) { canvas.save(); canvas.clipPath(genPath()); super.draw(canvas); canvas.restore(); } private void dispatchDraw27(Canvas canvas) { canvas.saveLayer(mChildRectF, null, Canvas.ALL_SAVE_FLAG); super.dispatchDraw(canvas); mPaint.setStyle(Paint.Style.FILL); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawPath(genChildPath(), mPaint); canvas.restore(); } private Path genPath() { mPath.reset(); mPath.addRoundRect(mRectF, mRadius, mRadius, Path.Direction.CW); return mPath; } private Path genChildPath() { mPath.reset(); mPath.addRoundRect(mChildRectF, mRadius, mRadius, Path.Direction.CW); return mPath; } }
<declare-styleable name="RoundRelativeLayout">
<attr name="rlRadius" format="dimension" />
<attr name="rlBorderWidth" format="dimension" />
</declare-styleable>
以上是关于CardView不好用,自己写个带border的圆角容器的主要内容,如果未能解决你的问题,请参考以下文章