先上实现代码:
/**
* @desc 带有角度的 textview
* @createtime: 2021/3/25
*/
public class CornerTextView extends AppCompatTextView {
private String tag = "CornerTextView";
private int mBackgroundColor = -1;
private Paint bgPaint;
private int leftTop;
private int leftBottom;
private int rightTop;
private int rightBottom;
private Path path = new Path();
private RectF rect = new RectF();
public CornerTextView(@NonNull Context context) {
this(context, null);
}
public CornerTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public CornerTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
//初始化 画笔
bgPaint = new Paint();
bgPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
//需要提前绘制背景色,要不然是覆盖在文字上面了
drawCornerBg(canvas);
super.onDraw(canvas);
}
//绘制带有角度的背景色
private void drawCornerBg(Canvas canvas) {
if (mBackgroundColor == -1) {
return;
}
if (leftTop == 0 && leftBottom == 0 && rightBottom == 0 && rightTop == 0) {
//只需要设置背景色即可
setBackgroundColor(mBackgroundColor);
return;
}
float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上,右上,右下,左下
rect.left = 0;
rect.right = getWidth();
rect.top = 0;
rect.bottom = getHeight();
path.addRoundRect(rect, outerR, Path.Direction.CW);
canvas.drawPath(path, bgPaint);
}
public void setCorner(int corner) {
this.leftTop = corner;
this.rightTop = corner;
this.leftBottom = corner;
this.rightBottom = corner;
}
public void setCorner(int leftTop, int rightTop, int leftBottom, int rightBottom) {
this.leftTop = leftTop;
this.rightTop = rightTop;
this.leftBottom = leftBottom;
this.rightBottom = rightBottom;
}
public void setBgBackGround(@ColorInt int color) {
bgPaint.setColor(color);
mBackgroundColor = color;
}
//动态设置好背景色和弧度后,调用刷新,重新绘制
public void afterOkInvalidate() {
invalidate();
}
}
主要实现代码就一块:
根据角度,确认path路径,添加到绘制页面绘制即可,省却了通过shape 固定定义图形的尴尬
//绘制带有角度的背景色
private void drawCornerBg(Canvas canvas) {
if (mBackgroundColor == -1) {
return;
}
if (leftTop == 0 && leftBottom == 0 && rightBottom == 0 && rightTop == 0) {
//只需要设置背景色即可
setBackgroundColor(mBackgroundColor);
return;
}
float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上,右上,右下,左下
rect.left = 0;
rect.right = getWidth();
rect.top = 0;
rect.bottom = getHeight();
path.addRoundRect(rect, outerR, Path.Direction.CW);
canvas.drawPath(path, bgPaint);
}
若是想通过xml布局定义好背景色和弧度,可以自定义控件属性来现实。
代码使用示例:
LinearLayout ll_container = findViewById(R.id.ll_container);
CornerTextView cornerTextView = new CornerTextView(this);
cornerTextView.setPadding(15,8,18,8);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.bottomMargin=50;
ll_container.addView(cornerTextView,params);
cornerTextView.setText("感觉到很干净回到家");
cornerTextView.setTextSize(16);
cornerTextView.setTextColor(Color.parseColor("#00ff00"));
cornerTextView.setBgBackGround(Color.parseColor("#ffff00"));
cornerTextView.setCorner(35,10,10,35);
cornerTextView.afterOkInvalidate();
示例图片:
image.png
网友评论