日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 代码设置textview draw,Android 自定义气泡TextView

發(fā)布時間:2025/3/15 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 代码设置textview draw,Android 自定义气泡TextView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

效果如下:

可以設(shè)置顏色、描邊、三角形高度和方向,以向上居中和向下居中為例

氣泡.png

實現(xiàn)思路:

使用Canvas繪制氣泡形狀,因為氣泡中間只顯示文字,所以我直接繼承TextView,重寫onDraw方法。

關(guān)鍵代碼:

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可以理解為為矩形左上角點的坐標(biāo)

right, bottom可以理解為矩形右下角點的坐標(biāo)

如圖所示:

矩形.jpg

3、Path繪制三角形,先moveTo移動到任意一點,然后lineTo畫線:

描點.jpg

完整代碼:

BubbleView.java

/**

* 類描述: 氣泡

* 創(chuàng)建人: liufei

* 創(chuàng)建時間: 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();

//設(shè)置抗鋸齒

mPaint.setAntiAlias(true);

//設(shè)置填充

mPaint.setStyle(Paint.Style.FILL);

//設(shè)置防抖動

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

}

//根據(jù)字號求字體高度

private int getFontHeight() {

Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();

return Math.round(fontMetrics.descent - fontMetrics.ascent);

}

//根據(jù)字號求字體寬度

private int getFontWidth() {

return (int) mPaint.measureText(getText().toString());

}

//設(shè)置氣泡顏色

public void setBubbleColor(int color) {

this.bgColor = ContextCompat.getColor(getContext(), color);

invalidate();

}

//設(shè)置氣泡描邊

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" />

新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!

總結(jié)

以上是生活随笔為你收集整理的android 代码设置textview draw,Android 自定义气泡TextView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。