From 7c6189db11d7a359a49f4ae9e7f8e332d9939a7a Mon Sep 17 00:00:00 2001 From: ningwenqiang Date: Wed, 23 Oct 2024 10:16:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8Epd=E6=8B=B7=E8=B4=9D=20=201=E3=80=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9B=B4=E6=92=AD=E9=97=B4=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E9=97=AE=E9=A2=98=5F2=E3=80=81PK=E8=BF=9E?= =?UTF-8?q?=E9=BA=A6=5F=E7=BB=93=E6=9D=9F=E5=9B=BE=E6=A0=87=EF=BC=88?= =?UTF-8?q?=E8=8B=B1=E6=96=87=EF=BC=89=E6=9B=B4=E6=8D=A2=20=E5=9B=BE?= =?UTF-8?q?=EF=BC=8C=E5=9B=A0=E4=B8=BA=E7=BC=A9=E7=95=A5=E5=9B=BE=E5=92=8C?= =?UTF-8?q?=E4=B8=BB=E5=9B=BE=E5=8A=A0=E8=BD=BD=E5=AE=8C=E9=83=BD=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=EF=BC=8C=E4=BC=9A=E5=AF=BC=E8=87=B4=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=9A=84=E4=BB=A3=E7=A0=81=E5=92=8C=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E8=BD=ACDrawable=E7=9A=84=E4=BB=A3=E7=A0=81=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=A4=9A=E6=AC=A1=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yunbao/live/utils/LiveTextRender.java | 85 ++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) 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 11c1b5c7b..a55bd54eb 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; @@ -725,7 +726,7 @@ public class LiveTextRender { builder = renderGift(color, builder, bean); break; default: - builder = renderChat(textView, color, builder, bean, prefixWidth); + builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -750,7 +751,7 @@ public class LiveTextRender { builder = renderGift(color, builder, bean); break; default: - builder = renderChat(textView, color, builder, bean, prefixWidth); + builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -844,9 +845,89 @@ 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) { + int length = builder.length(); + + // availableWidth textView 可用宽度 prefixWidth 图片宽度 + int availableWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); +// Log.e("renderChatNew", "availableWidth: " + availableWidth); +// Log.e("renderChatNew", "textView.getPaddingLeft(): " + textView.getPaddingLeft()); +// Log.e("renderChatNew", "textView.getPaddingRight(): " + textView.getPaddingRight()); + + StringBuilder sb = new StringBuilder(); + String name = bean.getUserNiceName(); + if (bean.getType() != LiveChatBean.ENTER_ROOM) { // 产品规定,进场消息不允许加冒号 + name += ":"; + } + + int lengthContent = length + name.length(); + + sb.append(name).append(bean.getContent()); + + TextPaint paint = textView.getPaint(); + float nameWidth = paint.measureText(sb.toString()); +// Log.e("renderChatNew", "nameWidth: " + nameWidth + " prefixWidth: " + prefixWidth); + + if (nameWidth + prefixWidth > availableWidth) { + StringBuilder sb_tmp = new StringBuilder(); + StringBuilder saveContentSB = new StringBuilder(); + int initImageWidth = prefixWidth; + + for (int i = 0; i < sb.length(); i++) { + char content = sb.charAt(i); + sb_tmp.append(content); + float measuredTextWidth = paint.measureText(sb_tmp.toString()) + initImageWidth; +// Log.e("renderChatNew", "文字:" + sb_tmp.toString() + " measuredTextWidth: " + measuredTextWidth + " initImageWidth:" + initImageWidth + " availableWidth: " + availableWidth); + if (measuredTextWidth > availableWidth) { + initImageWidth = 0; + saveContentSB.append("\n"); +// Log.e("renderChatNew", "sb_tmp.toString():" + sb_tmp.toString()); + sb_tmp.setLength(0); + sb_tmp.append(content); + } + saveContentSB.append(content); +// Log.e("renderChatNew", "saveContentSB.toString():" + saveContentSB.toString()); + } + builder.append(saveContentSB); +// Log.e("renderChatNew", "builder.toString():" + builder.toString()); + + } else { + builder.append(sb); + } + + + //超皇设置渐变色展示昵称 + if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getStartColor())) { + builder.setSpan(new LinearGradientFontSpan(Color.parseColor(bean.getStartColor()), Color.parseColor(bean.getEndColor())), length, length + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } else { + builder.setSpan(new ForegroundColorSpan(color), length, length + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + //超皇设置渐变色展示昵称 + if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getContentColor())) { + builder.setSpan(new ForegroundColorSpan(Color.parseColor(bean.getContentColor())), lengthContent, lengthContent + bean.getContent().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if (bean.getType() == LiveChatBean.LIGHT) { + Drawable heartDrawable = ContextCompat.getDrawable(CommonAppContext.sInstance, LiveIconUtil.getLiveLightIcon(bean.getHeart())); + if (heartDrawable != null) { + builder.append(" "); + heartDrawable.setBounds(0, 0, DpUtil.dp2px(16), DpUtil.dp2px(16)); + length = builder.length(); + builder.setSpan(new VerticalImageSpan(heartDrawable), length - 1, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + + return builder; + } + + /** * 渲染普通聊天消息 + * 使用renderChatNew */ + @Deprecated private SpannableStringBuilder renderChat(TextView textView, int color, SpannableStringBuilder builder, LiveChatBean bean, int prefixWidth) { int length = builder.length(); String name = bean.getUserNiceName();