本文实例为大家分享了android自定义带箭头对话框的具体代码,供大家参考,具体内容如下
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.path;
import android.support.annotation.nullable;
import android.util.attributeset;
import android.view.gravity;
import com.sankuai.shangou.stone.util.densityutil;
import com.sankuai.waimai.store.search.r;
/**
* created by android studio. user: liangyongyao date: 2021/3/7 des: 带倒三角的气泡
*/
public class bubblearrowtextview extends android.support.v7.widget.appcompattextview {
private final static int triangle_direction_top = 1;
private final static int triangle_direction_bottom = 2;
private final static int triangle_direction_left = 1;
private final static int triangle_direction_right = 2;
private paint mpaint;
private paint mstrokepaint;
private int mbgcolor;
private int mstrokecolor;
private int mstrokewidth;
private int mtotalheight;
private int mtotalwidth;
private int mlabelheight;
private int mtriangleheight;
private int mtrianglewidth;
private int mradius;
private int triangledirection;
public bubblearrowtextview(context context) {
this(context, null);
}
public bubblearrowtextview(context context,
@nullable attributeset attrs) {
this(context, attrs, 0);
}
public bubblearrowtextview(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.bubblearrowtextview);
mbgcolor = a.getcolor(r.styleable.bubblearrowtextview_bubblecolor, 0);
mstrokecolor = a.getcolor(r.styleable.bubblearrowtextview_bubblestrokecolor, 0);
mradius = a.getdimensionpixeloffset(r.styleable.bubblearrowtextview_bubbleradius, 0);
mstrokewidth = a.getdimensionpixeloffset(r.styleable.bubblearrowtextview_bubblestrokewidth, 0);
mtriangleheight = a.getdimensionpixeloffset(r.styleable.bubblearrowtextview_triangleheight,
densityutil.dip2px(context, 6));
mtrianglewidth = a.getdimensionpixeloffset(r.styleable.bubblearrowtextview_trianglewidth, densityutil.dip2px(context, 3.5f));
triangledirection = a.getint(r.styleable.bubblearrowtextview_triangledirection, 0);
a.recycle();
}
setgravity(gravity.center);
initpaint();
}
//初始化画笔
public void initpaint() {
mpaint = new paint();
mpaint.setantialias(true);
mpaint.setstyle(paint.style.fill);
mpaint.settextsize(getpaint().gettextsize());
mpaint.setdither(true);
}
//初始化边框线画笔
public void initstrokepaint() {
mstrokepaint = new paint();
mstrokepaint.setantialias(true);
mstrokepaint.setstyle(paint.style.fill);
mstrokepaint.setdither(true);
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
super.onmeasure(widthmeasurespec, heightmeasurespec);
mlabelheight = getfontheight() + getpaddingtop() + getpaddingbottom();
mtotalheight = mlabelheight + mtriangleheight * 2 + mstrokewidth * 2;
mtotalwidth = getpaddingleft() + getfontwidth() + getpaddingright() + mstrokewidth * 2;
setmeasureddimension(mtotalwidth, mtotalheight);
}
@override
protected void ondraw(canvas canvas) {
drawview(canvas);
super.ondraw(canvas);
}
//绘制气泡
private void drawview(canvas canvas) {
if (mstrokecolor != 0 && mstrokewidth != 0) {
initstrokepaint();
mstrokepaint.setcolor(mstrokecolor);
drawround(canvas, mstrokepaint, 0);
drawtriangle(canvas, mstrokepaint, 0);
}
if (mbgcolor != 0) {
mpaint.setcolor(mbgcolor);
drawround(canvas, mpaint, mstrokewidth);
drawtriangle(canvas, mpaint, mstrokewidth);
}
}
//绘制矩形
private void drawround(canvas canvas, paint paint, int strokewidth) {
canvas.drawroundrect(strokewidth, mtriangleheight + strokewidth,
mtotalwidth - strokewidth, mtotalheight - mtriangleheight - strokewidth,
mradius, mradius, paint);
}
//绘制三角形
private void drawtriangle(canvas canvas, paint paint, int strokewidth) {
path path = new path();
switch (triangledirection) {
//上
case triangle_direction_top:
path.moveto(mtotalwidth * 0.8f - mtrianglewidth / 2 + strokewidth / 2, mtriangleheight + strokewidth);
path.lineto(mtotalwidth * 0.8f, strokewidth + strokewidth / 2);
path.lineto(mtotalwidth * 0.8f + mtrianglewidth / 2 - strokewidth / 2, mtriangleheight + strokewidth);
break;
//下
case triangle_direction_bottom:
path.moveto(mtotalwidth * 0.8f - mtrianglewidth/2 + strokewidth / 2, mtotalheight - mtriangleheight
- strokewidth);
path.lineto(mtotalwidth * 0.8f, mtotalheight - strokewidth - strokewidth / 2);
path.lineto(mtotalwidth * 0.8f + mtrianglewidth/2 - strokewidth / 2, mtotalheight - mtriangleheight
- strokewidth);
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());
}
}
xml:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。