自定义Android聊天气泡ChatView。仿微信聊天气泡,能自定义边框,颜色,点击特效。

11

自定义Android聊天气泡ChatView。仿微信聊天气泡,能自定义边框,颜色,点击特效。

自定义Android聊天气泡ChatView

最近公司准备做一款即时通讯的APP,就照着微信的功能模块做。于是我在网上找了很多聊天气泡BubbleView,要不就是样式太丑,要不就是对交互点击不太友好。所以小编就下定决心,自己画一个满足要求的自定义ChatView。

仿微信聊天气泡

首先,我们需要实现一个基本的聊天气泡布局。我们可以使用LinearLayout作为根视图,然后添加两个TextView来显示发送者和接收者的信息。

```xml

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="40dp"

android:layout_height="40dp"

android:src="@drawable/ic_user" />

android:layout_weight="1"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="16sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="16sp" />

```

自定义边框

为了实现自定义边框,我们可以使用ShapeDrawable来绘制一个圆角矩形。

```java// ChatView.javapublic class ChatView extends LinearLayout {

public ChatView(Context context, AttributeSet attrs) {

super(context, attrs);

// 设置背景色和圆角半径 setBackgroundResource(R.drawable.bubble_background);

}

// ...

}

```

```xml

android:color="FF0000" />

```

自定义颜色

为了实现自定义颜色,我们可以使用ColorStateList来绘制一个渐变效果。

```java// ChatView.javapublic class ChatView extends LinearLayout {

public ChatView(Context context, AttributeSet attrs) {

super(context, attrs);

// 设置背景色和圆角半径 setBackgroundResource(R.drawable.bubble_background);

}

// ...

}

```

```xml

android:color="@color/bubble_color" />

```

```xml

FF0000

```

点击特效

为了实现点击特效,我们可以使用Animation来绘制一个弹出效果。

```java// ChatView.javapublic class ChatView extends LinearLayout {

public ChatView(Context context, AttributeSet attrs) {

super(context, attrs);

// 设置背景色和圆角半径 setBackgroundResource(R.drawable.bubble_background);

}

@Override public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

// 开始弹出动画 startAnimation(anim_pop_up);

} else if (event.getAction() == MotionEvent.ACTION_UP) {

// 结束弹出动画 clearAnimation();

}

return true;

}

private Animation anim_pop_up = new Animation() {

@Override public void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

if (interpolatedTime < 0.5f) {

// 开始弹出动画 setTranslationY(-100);

} else {

// 结束弹出动画 setTranslationY(0);

}

}

};

}

```

通过以上的实现,我们可以自定义一个Android聊天气泡ChatView,仿微信聊天气泡,能自定义边框,颜色,点击特效。

聊天即时通讯Android自定义View聊天气泡仿微信气泡

版权声明:除非特别标注,否则均为网络文章,侵权请联系站长删除。

上一篇 微信聊天内容制作生成器微信小程序源码/支持多种制作生成

下一篇 企业微信-会话内容存档 实时拉取企业微信聊天记录java版SDK