From 92228b64bb1896d4ef986ea69f6e56b647c90b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E7=9A=AE?= <14840170+lao-pi123@user.noreply.gitee.com> Date: Thu, 10 Oct 2024 17:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E9=97=B4=E8=81=8A=E5=A4=A9IM?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E9=97=AE=E9=A2=98=EF=BC=8C=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yunbao/live/utils/LiveTextRender.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java index 7d4212813..b0458c685 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java @@ -13,6 +13,7 @@ import android.graphics.drawable.Drawable; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; +import android.text.TextPaint; import android.text.TextUtils; import android.text.style.AbsoluteSizeSpan; import android.text.style.ForegroundColorSpan; @@ -727,6 +728,7 @@ public class LiveTextRender { break; default: builder = renderChat(textView, color, builder, bean, prefixWidth); +// builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -752,6 +754,7 @@ public class LiveTextRender { break; default: builder = renderChat(textView, color, builder, bean, prefixWidth); +// builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -845,6 +848,56 @@ public class LiveTextRender { public static int textTemp = 0; public static String userName = ""; + private SpannableStringBuilder renderChatNew(TextView textView, int color, SpannableStringBuilder builder, LiveChatBean bean, int prefixWidth) { + // prefixWidth 图片title 的宽度 + + // 1、计算textview的可用宽度 + int availableWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); + Log.e("renderChatNew", "availableWidth: " + availableWidth); + + + StringBuilder sb = new StringBuilder(); + String name = bean.getUserNiceName(); + if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定,进场消息不允许加冒号 + name += ":"; + } + sb.append(name).append(bean.getContent()); + + // 2、计算名称➕内容 的宽度 + TextPaint paint = textView.getPaint(); + float nameWidth = paint.measureText(sb.toString()); + Log.e("renderChatNew", "nameWidth: " + nameWidth + " prefixWidth: " + prefixWidth); + + if (nameWidth + prefixWidth > availableWidth){ + // 4、如果名称➕内容 的宽度大于textview的可用宽度,则计算字符后插入换行符 + StringBuilder sb_tmp = new StringBuilder(); + + int initImageWidth = prefixWidth; + + for (int i = 0; i < sb.length(); i++) { + char content = sb.charAt(i); + sb_tmp.append(content); + float measuredText = textView.getPaint().measureText(sb_tmp.toString()) + initImageWidth; + Log.e("renderChatNew", "measuredText: " + measuredText + " availableWidth: " + availableWidth); + if (measuredText > availableWidth) { + initImageWidth = 0; + sb_tmp.delete(sb_tmp.length() -1, sb_tmp.length()); + sb_tmp.append("\n"); + sb_tmp.append(content); + Log.e("renderChatNew", "sb_tmp.toString():" + sb_tmp.toString()); + } + } + builder.append(sb_tmp); + Log.e("renderChatNew", "builder.toString():" + builder.toString()); + + }else { + builder.append(sb); + } + return builder; + } + + + /** * 渲染普通聊天消息 */