Android 自定聊天气泡 消息气泡 BubbleView

一、BubbleView介绍

BubbleView 是一个类似微信聊天气泡但功能非常强大的控件(确切的说是一个容器)。
1.可以设置指示器(即箭头,后面统称指示器)的方向、位置、大小,如效果图所示。
2.可以自定义指示器样式(默认为等腰三角形)。
3.可以设置气泡Z轴方向高度和阴影颜色(有高度才会有阴影)。
4.可以设置子控件是否填充到指示器中(PC版微信中图片效果)。
5.可以设置气泡类型为边框线类型。

demo地址 现在访问github比百度网盘还坑,能不能进的看运气了,hosts文件都改了无数遍了,更新一次提交搞了我半个小时

效果图

二、BubbleView使用

1、添加依赖

1.Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

2.Add the dependency

    dependencies {
            implementation 'com.github.xiaohaozi9825:BubbleView:1.01'
    }

2、在layout文件中添加BubbleView

    <pw.xiaohaozi.bubbleview.BubbleView
        android:id="@+id/bv_defaule"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认样式气泡"
            android:textColor="@color/colorPrimary"
            android:textSize="24sp"
            android:textStyle="bold" />
    </pw.xiaohaozi.bubbleview.BubbleView>

ok,一个普通的气泡就添加完成了,
默认效果是,白色底,指示器指向左,位置居中,无阴影。

3、BubbleView属性介绍

//Z轴方向高度
 app:bubbleElevation="dimension"

//子view是否填充到指示器中
 app:bubbleFillIndicator="boolean"

//指示器方向,枚举值:左上右下
 app:bubbleIndicatorDirection="left|top|right|bottom"

//指示器的位置
//centre:居中,start:上边或左边,end:下边或右边
//float:float类型范围在0-1之间,0对应start,0.5对应centre,1对应end
//dimension 0:中间,正数:从开始位置偏移,负数:从结束位置开始偏移
 app:bubbleIndicatorLocation="start|centre|end|float|dimension"

//指示器宽度和高度,指示器与圆角矩形平行的切边平行的边为宽,垂直的边为高
 app:bubbleIndicatorWidth="dimension"
 app:bubbleIndicatorHeight="dimension"

//气泡颜色
 app:bubbleColor="color"

//阴影颜色
 app:bubbleShadowColor="color"

//边线宽度
 app:bubbleStrokeWidth="dimension"

//圆角矩形圆角半径
app:bubbleRadius="dimension"

4、自定义指示器

如果你觉得默认的等腰三角形指示器不能满足你的需求,可以自定义一个指示器。

 class MyDrawIndicator implements BubbleView.DrawIndicator {
    @Override
    public void drawLeft(Path path, int left, int top, int right, int bottom) {
        int w = right - left;
        int h = bottom - top;
        path.moveTo(right, top + h / 2);
        path.arcTo(left, top - h * 3 / 2, right + h, bottom - h *2/ 3, 90, 72, false);
        path.arcTo(left, top - h, right + h, bottom, 180, -90, false);
        path.lineTo(right, bottom);
    }
    @Override
    public void drawTop(Path path, int left, int top, int right, int bottom) { }
    @Override
    public void drawRight(Path path, int left, int top, int right, int bottom) {
        int w = right - left;
        int h = bottom - top;
        path.moveTo(left, top + h / 2);
        path.arcTo(left - h, top - h * 3 / 2, right, bottom - h * 2/3, 90, -72, false);
        path.arcTo(left - h, top - h, right, bottom, 0, 90, false);
        path.lineTo(left, bottom);
    }
    @Override
    public void drawBottom(Path path, int left, int top, int right, int bottom) {  }
}

三、demo代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="16dp"
            tools:context=".MainActivity">

            <pw.xiaohaozi.bubbleview.BubbleView
                android:id="@+id/bv_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                app:bubbleElevation="8dp"
                app:bubbleFillIndicator="true"
                app:bubbleIndicatorDirection="left"
                app:bubbleIndicatorHeight="8dp"
                app:bubbleIndicatorLocation="start"
                app:bubbleIndicatorWidth="16dp"
                app:bubbleShadowColor="#ff0000"
                app:bubbleStrokeWidth="1dp">

                <ImageView
                    android:layout_width="120dp"
                    android:layout_height="200dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/test" />

            </pw.xiaohaozi.bubbleview.BubbleView>

            <pw.xiaohaozi.bubbleview.BubbleView
                android:id="@+id/bv_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_margin="8dp"
                app:bubbleColor="#00ff00"
                app:bubbleElevation="8dp"
                app:bubbleFillIndicator="true"
                app:bubbleIndicatorDirection="right"
                app:bubbleIndicatorHeight="8dp"
                app:bubbleIndicatorLocation="start"
                app:bubbleIndicatorWidth="16dp"
                app:bubbleShadowColor="#ff0000"
                app:bubbleStrokeWidth="1dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#00ff00"
                    android:padding="8dp"
                    android:text="
        自定义指示器\n
        app:bubbleElevation=&quot;8dp&quot;\n
        app:bubbleShadowColor=&quot;#ff0000&quot;\n
        app:bubbleFillIndicator=&quot;false&quot;\n
        app:bubbleIndicatorHeight=&quot;8dp&quot;\n
        app:bubbleIndicatorWidth=&quot;8dp&quot;\n
        app:bubbleIndicatorDirection=&quot;left&quot;\n
        app:bubbleStrokeWidth=&quot;1dp&quot;\n
        app:bubbleIndicatorLocation=&quot;start&quot;"
                    android:textColor="@android:color/white" />
            </pw.xiaohaozi.bubbleview.BubbleView>

            <pw.xiaohaozi.bubbleview.BubbleView
                android:id="@+id/bv_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="8dp"
                app:bubbleColor="@android:color/black"
                app:bubbleElevation="8dp"
                app:bubbleIndicatorDirection="bottom"
                app:bubbleIndicatorLocation="0.8"
                app:bubbleShadowColor="#ff0000"
                app:bubbleStrokeWidth="2dp">

                <TextView
                    android:id="@+id/tv_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:gravity="center"
                    android:text="
阴影颜色:红色\n
指示器方向:向下\n
指示器位置:居中\n
边线宽度:1dp\n
高度:8dp\n
下面这个气泡将内容填充到了指示器里\n默认不填充
"
                    android:textColor="@color/colorPrimary" />
            </pw.xiaohaozi.bubbleview.BubbleView>

        </LinearLayout>

        <pw.xiaohaozi.bubbleview.BubbleView
            android:id="@+id/bv_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:padding="8dp"
            app:bubbleColor="#464646"
            app:bubbleIndicatorDirection="top"
            app:bubbleIndicatorHeight="4dp"
            app:bubbleIndicatorLocation="end"
            app:bubbleIndicatorWidth="8dp"
            tools:ignore="DuplicateIds">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="发起群聊"
                    android:textColor="@android:color/white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="添加朋友"
                    android:textColor="@android:color/white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="扫一扫"
                    android:textColor="@android:color/white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="收付款"
                    android:textColor="@android:color/white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="帮助与反馈"
                    android:textColor="@android:color/white" />
            </LinearLayout>
        </pw.xiaohaozi.bubbleview.BubbleView>
    </FrameLayout>
</androidx.core.widget.NestedScrollView>

MainActivity.java

package pw.xiaohaozitest.bubbleview;

import androidx.appcompat.app.AppCompatActivity;
import pw.xiaohaozi.bubbleview.BubbleView;

import android.graphics.Path;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private BubbleView mBv1;
    private BubbleView mBv2;
    private BubbleView mBv3;
    private BubbleView mBv4;
    private MyDrawIndicator mMyDrawIndicator = new MyDrawIndicator();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBv1 = findViewById(R.id.bv_1);
        mBv2 = findViewById(R.id.bv_2);
        mBv3 = findViewById(R.id.bv_3);
        mBv4 = findViewById(R.id.bv_4);

        mBv2.setDrawIndicator(mMyDrawIndicator);
        mBv3.setDrawIndicator(mMyDrawIndicator);
    }

class MyDrawIndicator implements BubbleView.DrawIndicator {

    @Override
    public void drawLeft(Path path, int left, int top, int right, int bottom) {
        int w = right - left;
        int h = bottom - top;
        path.moveTo(right, top + h / 2);
//                path.lineTo(left, top);
        path.arcTo(left, top - h * 3 / 2, right + h, bottom - h *2/ 3, 90, 72, false);
        path.arcTo(left, top - h, right + h, bottom, 180, -90, false);
        path.lineTo(right, bottom);

    }

    @Override
    public void drawTop(Path path, int left, int top, int right, int bottom) {
    }

    @Override
    public void drawRight(Path path, int left, int top, int right, int bottom) {
        int w = right - left;
        int h = bottom - top;
        path.moveTo(left, top + h / 2);
//                path.lineTo(right, top);
        path.arcTo(left - h, top - h * 3 / 2, right, bottom - h * 2/3, 90, -72, false);
        path.arcTo(left - h, top - h, right, bottom, 0, 90, false);
        path.lineTo(left, bottom);
    }

    @Override
    public void drawBottom(Path path, int left, int top, int right, int bottom) {
    }
}
}

2021年5月17日
增加了drawRight();
修复了重绘后出现多个指示器的bug

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,635评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,543评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,083评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,640评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,640评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,262评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,833评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,736评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,280评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,369评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,503评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,870评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,340评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,460评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,909评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,512评论 2 359