android 带箭头的按钮,android自定义带箭头对话框

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 气泡框代码示例: 首先,创建一个自定义的布局文件 `custom_tooltip.xml`,包含一个 LinearLayout 和一个 TextView: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tooltip_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tooltip_background" android:orientation="horizontal" android:padding="8dp"> <TextView android:id="@+id/tooltip_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" /> </LinearLayout> ``` 然后,在你的 Activity 中,使用 PopupWindow 来显示气泡框。示例代码如下: ``` // inflate the custom layout View tooltipView = LayoutInflater.from(this).inflate(R.layout.custom_tooltip, null); // set the text of the TextView TextView tooltipText = tooltipView.findViewById(R.id.tooltip_text); tooltipText.setText("Hello, world!"); // create a new PopupWindow PopupWindow tooltip = new PopupWindow(tooltipView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // set the PopupWindow to be focusable tooltip.setFocusable(true); // show the PopupWindow at a specified location int[] location = new int[2]; targetView.getLocationOnScreen(location); tooltip.showAtLocation(targetView, Gravity.NO_GRAVITY, location[0], location[1] - tooltip.getHeight()); ``` 这里,`targetView` 是你想要在其上显示气泡框的 View。`location` 数组包含 `targetView` 在屏幕上的 x 和 y 坐标。我们使用 `showAtLocation` 方法来显示气泡框,并将其放置在 `targetView` 的下方。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值