android 代码设置textview draw,Android 自定义气泡TextView
效果如下:
可以設置顏色、描邊、三角形高度和方向,以向上居中和向下居中為例
氣泡.png
實現思路:
使用Canvas繪制氣泡形狀,因為氣泡中間只顯示文字,所以我直接繼承TextView,重寫onDraw方法。
關鍵代碼:
1、在attrs.xml文件中自定義屬性,我定義了氣泡顏色、描邊顏色、描邊寬度、三角形高度、三角形方向代碼如下:
2、Canvas繪制矩形,提供了兩種重載方式:
drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint)
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint)
rx表示圓心
ry表示半徑
left, top可以理解為為矩形左上角點的坐標
right, bottom可以理解為矩形右下角點的坐標
如圖所示:
矩形.jpg
3、Path繪制三角形,先moveTo移動到任意一點,然后lineTo畫線:
描點.jpg
完整代碼:
BubbleView.java
/**
* 類描述: 氣泡
* 創建人: liufei
* 創建時間: 2019/10/25 15:14
*/
public class BubbleView extends AppCompatTextView {
private Paint mPaint;
private Paint mStrokePaint;
//背景顏色
private int bgColor;
//描邊顏色
private int strokeColor;
//描邊寬
private int strokeWidth;
//view總高
private int height;
//view總寬
private int width;
//矩形高
private int labelHeight;
//圓角半徑
private int mRadius;
//三角形高
private int triangleHeight;
//三角形方向
private int triangleDirection;
public BubbleView(Context context) {
this(context, null);
}
public BubbleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BubbleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
public void init(Context context, AttributeSet attrs, int defStyleAttr) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BubbleView);
bgColor = a.getColor(R.styleable.BubbleView_bubbleColor, 0);
strokeColor = a.getColor(R.styleable.BubbleView_bubbleStrokeColor, 0);
strokeWidth = a.getDimensionPixelOffset(R.styleable.BubbleView_bubbleStrokeWidth, 0);
triangleHeight = a.getDimensionPixelOffset(R.styleable.BubbleView_triangleHeight, 30);
triangleDirection = a.getInt(R.styleable.BubbleView_triangleDirection, 0);
a.recycle();
}
setGravity(Gravity.CENTER);
initPaint();
labelHeight = getFontHeight() + getPaddingTop() + getPaddingBottom();
height = labelHeight + triangleHeight * 2 + strokeWidth * 2;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getPaddingStart() + getFontWidth() + getPaddingEnd() + strokeWidth * 2;
setMeasuredDimension(width, height);
}
//初始化畫筆
public void initPaint() {
mPaint = new Paint();
//設置抗鋸齒
mPaint.setAntiAlias(true);
//設置填充
mPaint.setStyle(Paint.Style.FILL);
//設置防抖動
mPaint.setDither(true);
//字體大小
mPaint.setTextSize(getTextSize());
}
//初始化描邊畫筆
public void initStrokePaint() {
mStrokePaint = new Paint();
mStrokePaint.setAntiAlias(true);
mStrokePaint.setStyle(Paint.Style.FILL);
mStrokePaint.setDither(true);
}
@Override
protected void onDraw(Canvas canvas) {
drawView(canvas);
super.onDraw(canvas);
}
//繪制氣泡
private void drawView(Canvas canvas) {
if (strokeColor != 0 && strokeWidth != 0) {
initStrokePaint();
mStrokePaint.setColor(strokeColor);
mRadius = labelHeight / 2 + strokeWidth;
drawRound(canvas, mStrokePaint, 0);
drawTriangle(canvas, mStrokePaint, 0);
}
if (bgColor != 0) {
mPaint.setColor(bgColor);
mRadius = labelHeight / 2;
drawRound(canvas, mPaint, strokeWidth);
drawTriangle(canvas, mPaint, strokeWidth);
}
}
//繪制矩形
private void drawRound(Canvas canvas, Paint paint, int stroke) {
canvas.drawRoundRect(stroke, triangleHeight + stroke,
width - stroke, height - triangleHeight - stroke,
mRadius, mRadius, paint);
}
//繪制三角形
private void drawTriangle(Canvas canvas, Paint paint, int stroke) {
Path path = new Path();
switch (triangleDirection) {
//上
case 1:
path.moveTo(width / 2 - triangleHeight + stroke / 2, triangleHeight + stroke);
path.lineTo(width / 2, stroke + stroke / 2);
path.lineTo(width / 2 + triangleHeight - stroke / 2, triangleHeight + stroke);
break;
//下
case 2:
path.moveTo(width / 2 - triangleHeight + stroke / 2, height - triangleHeight - stroke);
path.lineTo(width / 2, height - stroke - stroke / 2);
path.lineTo(width / 2 + triangleHeight - stroke / 2, height - triangleHeight - stroke);
break;
default:
return;
}
canvas.drawPath(path, paint);
}
//根據字號求字體高度
private int getFontHeight() {
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
return Math.round(fontMetrics.descent - fontMetrics.ascent);
}
//根據字號求字體寬度
private int getFontWidth() {
return (int) mPaint.measureText(getText().toString());
}
//設置氣泡顏色
public void setBubbleColor(int color) {
this.bgColor = ContextCompat.getColor(getContext(), color);
invalidate();
}
//設置氣泡描邊
public void setStroke(int color, int width) {
this.strokeColor = ContextCompat.getColor(getContext(), color);
this.strokeWidth = width;
invalidate();
}
}
使用
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="6dp"
android:text="向上向上"
android:textColor="#000000"
android:textSize="20sp"
app:bubbleColor="#ffffff"
app:bubbleStrokeColor="#000000"
app:bubbleStrokeWidth="2dp"
app:triangleDirection="top"
app:triangleHeight="10dp" />
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的android 代码设置textview draw,Android 自定义气泡TextView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习笔记:访问数据库
- 下一篇: Mysql错误:服务名无效。 请键入