18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.example.smart.widget.dialog;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
 
import androidx.annotation.Nullable;
 
import com.example.basecore.util.LogUtils;
 
/**
 * 箭头气泡view
 */
public class BubbuleView extends View {
 
    private Paint mpaint;    //画笔
    private TextPaint textPaint;  //文字画笔
 
    public static final int RECT_RADIO = 20;  //背景的圆角度
    private Path path2 = new Path();
    private float mTriangleArrowSizePX;
    private String title = "主播可分得麦手礼物收入的10%";
 
    /**
     * 是否换行
     */
    private boolean isFoldText;
 
    private int leftMargin;
    private int verticalMargin;
    private int horizontalMargin;
    private float titlesize_px;
 
    private Rect titlebound;//标题的宽高
 
    private RectF bgRect;
 
    private int mTxtWidth, mTxtHeight;
    private int mWidth, mHeight;
 
    public BubbuleView(Context context) {
        super(context);
        init();
    }
 
    public BubbuleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public BubbuleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
 
 
    private void init() {
        mpaint = new Paint();
        textPaint = new TextPaint();
        mpaint.setAntiAlias(true);
        textPaint.setAntiAlias(true);
        mpaint.setColor(Color.parseColor("#ff8200"));
        textPaint.setColor(Color.parseColor("#ffffff"));
 
        mTriangleArrowSizePX = dip2px(getContext(), 7);
        leftMargin = dip2px(getContext(), 12);
        titlesize_px = dip2px(getContext(), 10);
        verticalMargin = dip2px(getContext(), 8);
        horizontalMargin = dip2px(getContext(), 10);
 
        textPaint.setTextSize(titlesize_px);
 
        titlebound = new Rect();
        bgRect = new RectF();
 
        setFoldText();
        measureWidthAndHeight();
    }
 
    public void setFoldText() {
        this.isFoldText = true;
        title = "主播可分得麦手礼物\n收入的10%";
        measureWidthAndHeight();
    }
 
    private void measureWidthAndHeight() {
        textPaint.getTextBounds(title, 0, title.length(), titlebound);
        mTxtWidth = titlebound.width();
        mTxtHeight = titlebound.height();
        LogUtils.e("txt width = " + mTxtWidth + ",height = " + mTxtHeight);
 
        if (isFoldText) {
            Rect rtemp = new Rect();
            String str = "主播可分得麦手礼物";
            textPaint.getTextBounds(str, 0, str.length(), rtemp);
            mTxtWidth = rtemp.width();
            //不知道为什么要补偿5
            StaticLayout myStaticLayout = new StaticLayout(title, textPaint, mTxtWidth + 5,
                    Layout.Alignment.ALIGN_NORMAL, 1f, 0.0f, false);
 
            mTxtHeight = myStaticLayout.getHeight();
            LogUtils.e("fold txt width = " + mTxtWidth + ",height = " + mTxtHeight);
        }
        requestLayout();
    }
 
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int paddingleftandright = getPaddingLeft() + getPaddingRight();
        int paddingtopandbottom = getPaddingTop() + getPaddingBottom();
 
        //宽度和高度
        mWidth = mTxtWidth + paddingleftandright;
        mHeight = (int) (mTxtHeight + paddingtopandbottom + mTriangleArrowSizePX);
        LogUtils.e(" width = " + mWidth + ",height = " + mHeight);
        bgRect.set(0, mTriangleArrowSizePX, mWidth, mHeight);
        setMeasuredDimension(mWidth, mHeight);
    }
 
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        //画背景
        canvas.drawRoundRect(bgRect, RECT_RADIO, RECT_RADIO, mpaint);
 
        //画三角
        path2.moveTo(leftMargin, mTriangleArrowSizePX);
        path2.lineTo(leftMargin + (mTriangleArrowSizePX / 2) + 6, 0);
        //补偿4dp
        path2.lineTo(leftMargin + mTriangleArrowSizePX + 12, mTriangleArrowSizePX);
        path2.close();
        canvas.drawPath(path2, mpaint);
 
        //画文字
        if (isFoldText) {
            StaticLayout myStaticLayout = new StaticLayout(title, textPaint, titlebound.width() + 5,
                    Layout.Alignment.ALIGN_NORMAL, 1f, 0.0f, false);
            canvas.translate(horizontalMargin, verticalMargin + mTriangleArrowSizePX);
            myStaticLayout.draw(canvas);
        } else {
            canvas.drawText(title, horizontalMargin, verticalMargin + mTxtHeight + mTriangleArrowSizePX, textPaint);
        }
 
    }
 
 
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}