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