android自定义带箭头对话框
本文实例为大家分享了android自定义带箭头对话框的具体代码,供大家参考,具体内容如下
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.Path;
importandroid.support.annotation.Nullable;
importandroid.util.AttributeSet;
importandroid.view.Gravity;
importcom.sankuai.shangou.stone.util.DensityUtil;
importcom.sankuai.waimai.store.search.R;
/**
*CreatedbyAndroidStudio.User:liangyongyaoDate:2021/3/7Des:带倒三角的气泡
*/
publicclassBubbleArrowTextViewextendsandroid.support.v7.widget.AppCompatTextView{
privatefinalstaticintTRIANGLE_DIRECTION_TOP=1;
privatefinalstaticintTRIANGLE_DIRECTION_BOTTOM=2;
privatefinalstaticintTRIANGLE_DIRECTION_LEFT=1;
privatefinalstaticintTRIANGLE_DIRECTION_RIGHT=2;
privatePaintmPaint;
privatePaintmStrokePaint;
privateintmBgColor;
privateintmStrokeColor;
privateintmStrokeWidth;
privateintmTotalHeight;
privateintmTotalWidth;
privateintmLabelHeight;
privateintmTriangleHeight;
privateintmTriangleWidth;
privateintmRadius;
privateinttriangleDirection;
publicBubbleArrowTextView(Contextcontext){
this(context,null);
}
publicBubbleArrowTextView(Contextcontext,
@NullableAttributeSetattrs){
this(context,attrs,0);
}
publicBubbleArrowTextView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
init(context,attrs,defStyleAttr);
}
publicvoidinit(Contextcontext,AttributeSetattrs,intdefStyleAttr){
if(attrs!=null){
TypedArraya=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();
}
//初始化画笔
publicvoidinitPaint(){
mPaint=newPaint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(getPaint().getTextSize());
mPaint.setDither(true);
}
//初始化边框线画笔
publicvoidinitStrokePaint(){
mStrokePaint=newPaint();
mStrokePaint.setAntiAlias(true);
mStrokePaint.setStyle(Paint.Style.FILL);
mStrokePaint.setDither(true);
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
mLabelHeight=getFontHeight()+getPaddingTop()+getPaddingBottom();
mTotalHeight=mLabelHeight+mTriangleHeight*2+mStrokeWidth*2;
mTotalWidth=getPaddingLeft()+getFontWidth()+getPaddingRight()+mStrokeWidth*2;
setMeasuredDimension(mTotalWidth,mTotalHeight);
}
@Override
protectedvoidonDraw(Canvascanvas){
drawView(canvas);
super.onDraw(canvas);
}
//绘制气泡
privatevoiddrawView(Canvascanvas){
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);
}
}
//绘制矩形
privatevoiddrawRound(Canvascanvas,Paintpaint,intstrokeWidth){
canvas.drawRoundRect(strokeWidth,mTriangleHeight+strokeWidth,
mTotalWidth-strokeWidth,mTotalHeight-mTriangleHeight-strokeWidth,
mRadius,mRadius,paint);
}
//绘制三角形
privatevoiddrawTriangle(Canvascanvas,Paintpaint,intstrokeWidth){
Pathpath=newPath();
switch(triangleDirection){
//上
caseTRIANGLE_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;
//下
caseTRIANGLE_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);
}
//根据字号求字体高度
privateintgetFontHeight(){
Paint.FontMetricsfontMetrics=mPaint.getFontMetrics();
returnMath.round(fontMetrics.descent-fontMetrics.ascent);
}
//根据字号求字体宽度
privateintgetFontWidth(){
return(int)mPaint.measureText(getText().toString());
}
}
xml:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。