From 5f3ebb4235e8a32bb3c25cea5489c92cbacf62a5 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Thu, 30 May 2024 15:52:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=B4=E6=92=AD=E9=97=B4?= =?UTF-8?q?=20=E7=94=A8=E6=88=B7=E7=AD=89=E7=BA=A7+=E8=B6=85=E7=9A=87+?= =?UTF-8?q?=E7=B2=89=E4=B8=9D=E7=89=8C+=E5=9B=BE=E6=A0=87+=E5=90=8D?= =?UTF-8?q?=E5=AD=97=EF=BC=8810=E5=AD=97=EF=BC=89=E4=BC=9A=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E5=90=8D=E5=AD=97=E5=89=8D=E9=9D=A2=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.gradle | 2 +- .../com/yunbao/live/utils/LiveTextRender.java | 87 ++++++++++++++----- live/src/main/res/layout/item_live_chat.xml | 2 +- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/config.gradle b/config.gradle index fd33bfeaf..882f01bd3 100644 --- a/config.gradle +++ b/config.gradle @@ -4,7 +4,7 @@ ext { buildToolsVersion: "29.0.2", minSdkVersion : 21, targetSdkVersion : 33, - versionCode : 473, + versionCode : 474, versionName : "6.6.8" ] manifestPlaceholders = [ 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 b220d8f07..f9f67b5ba 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java @@ -21,6 +21,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.MeasureSpec; +import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; @@ -263,7 +264,7 @@ public class LiveTextRender { } public interface CreatePrefixCallback { - void onPrefixCallback(SpannableStringBuilder builder); + void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth); } /** @@ -353,19 +354,20 @@ public class LiveTextRender { index = builder.length(); } } - createPrefixCallback.onPrefixCallback(builder); + createPrefixCallback.onPrefixCallback(builder, 0); } @Override public void onLoadFailed() { SpannableStringBuilder builder = new SpannableStringBuilder(); int index = 0; - + int prefixWidth = 0; if (levelDrawable != null) { builder.append(" "); levelDrawable.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(levelDrawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); + prefixWidth += levelDrawable.getBounds().width(); } if (gzDrawablesMap.containsKey(bean)) {//贵族 @@ -373,6 +375,7 @@ public class LiveTextRender { gzDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(35), DpUtil.dp2px(20)); builder.setSpan(new VerticalImageSpan(gzDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); + prefixWidth += gzDrawablesMap.get(bean).getBounds().width(); } if (ryxzDrawablesMap.containsKey(bean)) {//荣誉勋章 @@ -380,6 +383,7 @@ public class LiveTextRender { ryxzDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(18), DpUtil.dp2px(18)); builder.setSpan(new VerticalImageSpan(ryxzDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); + prefixWidth += ryxzDrawablesMap.get(bean).getBounds().width(); } if (nhDrawablesMap.containsKey(bean)) {//靓号 @@ -387,6 +391,7 @@ public class LiveTextRender { nhDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(nhDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); + prefixWidth += nhDrawablesMap.get(bean).getBounds().width(); } //在这里添加粉丝徽章的图片 if (!TextUtils.isEmpty(bean.getMedalNmae()) && !TextUtils.isEmpty(bean.getMedalLevelImageUrl()) @@ -397,6 +402,7 @@ public class LiveTextRender { drawable.setBounds(0, 0, DpUtil.dp2px(44), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); + prefixWidth += drawable.getBounds().width(); } } @@ -421,7 +427,7 @@ public class LiveTextRender { index = builder.length(); } } - createPrefixCallback.onPrefixCallback(builder); + createPrefixCallback.onPrefixCallback(builder, prefixWidth); } }); @@ -515,9 +521,9 @@ public class LiveTextRender { } public void getGuardImage(Context mContext, int guardType, ImgLoader.DrawableCallback callback) { - if (guardType==0){ + if (guardType == 0) { callback.onLoadFailed(); - }else { + } else { if (mContext instanceof Activity) { if (((Activity) mContext).isDestroyed()) { mContext = CommonAppContext.getTopActivity(); @@ -684,7 +690,7 @@ public class LiveTextRender { createPrefix(drawable, bean, new CreatePrefixCallback() { @Override - public void onPrefixCallback(SpannableStringBuilder builder) { + public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int color = Color.parseColor("#68F1F4"); if (bean.isAnchor()) { color = Color.parseColor("#FBEABF"); @@ -696,20 +702,20 @@ public class LiveTextRender { builder = renderGift(color, builder, bean); break; default: - builder = renderChat(color, builder, bean); + builder = renderChat(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); } }); - } + }// 123123123123123123123123 @Override public void onLoadFailed() { createPrefix(null, bean, new CreatePrefixCallback() { @Override - public void onPrefixCallback(SpannableStringBuilder builder) { + public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int color = 0; if (bean.isAnchor()) { color = 0xffffdd00; @@ -721,7 +727,7 @@ public class LiveTextRender { builder = renderGift(color, builder, bean); break; default: - builder = renderChat(color, builder, bean); + builder = renderChat(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -808,12 +814,31 @@ public class LiveTextRender { /** * 渲染普通聊天消息 */ - private SpannableStringBuilder renderChat(int color, SpannableStringBuilder builder, LiveChatBean bean) { + private SpannableStringBuilder renderChat(TextView textView, int color, SpannableStringBuilder builder, LiveChatBean bean, int prefixWidth) { int length = builder.length(); String name = bean.getUserNiceName(); if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定,进场消息不允许加冒号 name += ":"; } + SpannableStringBuilder sb = new SpannableStringBuilder(); + sb.append(builder).append(name).append(" "); + float measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + int width = ((ViewGroup) textView.getParent()).getWidth(); + //检测渲染后图标+名字是否超过父布局宽度,超过了就做换行处理 + if (measuredText > width) { + sb.clear(); + sb.append(builder); + for (int i = 0; i < name.length(); i++) { + sb.append(name.charAt(i)).append(" "); + measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + if (measuredText + 1 > width) { + name = name.substring(0, i+1) + "\n" + name.substring(i+1); + break; + } + } + sb.clear(); + } + // name=name.substring(0,7)+"\n"+name.substring(7); builder.append(name); //超皇设置渐变色展示昵称 if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getStartColor())) { @@ -823,6 +848,24 @@ public class LiveTextRender { builder.setSpan(new ForegroundColorSpan(color), length, length + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } int lengthContent = builder.length(); + //这一段先别删,万一后面要改内容换行呢 + /* sb.clear(); + sb.append(builder).append(bean.getContent()); + measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + if (measuredText > width) { + sb.clear(); + sb.append(builder); + for (int i = 0; i < bean.getContent().length(); i++) { + sb.append(bean.getContent().charAt(i)); + measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + if (measuredText + 3 > width) { + bean.setContent(bean.getContent().substring(0, i) + "\n"+ bean.getContent().substring(i)); + bean.setContent(""); + break; + } + } + } +*/ builder.append(bean.getContent()); //超皇设置渐变色展示昵称 if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getContentColor())) { @@ -889,7 +932,7 @@ public class LiveTextRender { if (textView != null) { createPrefix(drawable, bean, new CreatePrefixCallback() { @Override - public void onPrefixCallback(SpannableStringBuilder builder) { + public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int start = builder.length(); String name = bean.getUserNiceName() + " "; builder.append(name); @@ -904,15 +947,15 @@ public class LiveTextRender { @Override public void onLoadFailed() { if (textView != null) { - createPrefix(null, bean, new CreatePrefixCallback() { - @Override - public void onPrefixCallback(SpannableStringBuilder builder) { - int start = builder.length(); - String name = bean.getUserNiceName() + " "; - builder.append(name); - textView.setText(builder); - } - }); + createPrefix(null, bean, new CreatePrefixCallback() { + @Override + public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { + int start = builder.length(); + String name = bean.getUserNiceName() + " "; + builder.append(name); + textView.setText(builder); + } + }); } } diff --git a/live/src/main/res/layout/item_live_chat.xml b/live/src/main/res/layout/item_live_chat.xml index c4d360d8c..da2996682 100644 --- a/live/src/main/res/layout/item_live_chat.xml +++ b/live/src/main/res/layout/item_live_chat.xml @@ -10,7 +10,7 @@