文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

android自定义带箭头对话框

2024-04-02 19:55

关注

本文实例为大家分享了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;
 

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:


<declare-styleable name="BubbleArrowTextView">
  <attr name="bubbleColor" format="reference|color" />
  <attr name="bubbleStrokeColor" format="reference|color" />
  <attr name="bubbleStrokeWidth" format="dimension" />
  <attr name="triangleHeight" format="dimension" />
  <attr name="triangleWidth" format="dimension" />
  <attr name="bubbleRadius" format="dimension" />
  <attr name="triangleDirection">
   <flag name="top" value="1" />
   <flag name="bottom" value="2" />
  </attr>
</declare-styleable>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯