本文实例为大家分享了Android MarkTipsView文字标识控件的具体代码,供大家参考,具体内容如下 效果预览 使用 使用方式跟TextView使用方式一样,背景设置直接s
本文实例为大家分享了Android MarkTipsView文字标识控件的具体代码,供大家参考,具体内容如下
效果预览
使用方式跟TextView使用方式一样,背景设置直接setBackgroundColor
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.RectF;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
public class MarkTipsView extends AppCompatTextView {
private Paint paint;
private int width;
private int height;
private int markWidth = 12;
private int markHeight = 12;
private float radius = 10;
private int solid = Color.parseColor("#F1AA44");
public MarkTipsView(Context context) {
super(context);
initAttributeSet(context, null);
}
public MarkTipsView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttributeSet(context, attrs);
}
public MarkTipsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttributeSet(context, attrs);
}
private void initAttributeSet(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
TypedArray array = context.obtainStyledAttributes(attrs,new int[]{android.R.attr.background});
solid = array.getInt(0,solid);
array.recycle();
}
@Override
public void setBackgroundColor(int color) {
solid = color;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() + markHeight);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(solid);
//矩形
Path path = new Path();
RectF rect = new RectF(0, 0, width, height - markHeight);
path.addRoundRect(rect, radius, radius, Path.Direction.CCW);
//三角形
Path onePath = new Path();
onePath.lineTo(width / 2 - markWidth, rect.height());
onePath.lineTo(width / 2, height);
onePath.lineTo(width / 2 + markWidth, rect.height());
onePath.close();
path.addPath(onePath);
//绘制路径
canvas.drawPath(path, paint);
super.onDraw(canvas);
//重绘文字
setText(getText());
}
public int getMarkHeight() {
return markHeight;
}
public void setMarkHeight(int markHeight) {
this.markHeight = markHeight;
invalidate();
}
public int getMarkWidth() {
return markWidth;
}
public void setMarkWidth(int markWidth) {
this.markWidth = markWidth;
invalidate();
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
invalidate();
}
}
--结束END--
本文标题: Android MarkTipsView文字标识控件使用方法
本文链接: https://www.lsjlt.com/news/123766.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0