修复直播间 用户等级+超皇+粉丝牌+图标+名字(10字)会丢失名字前面图标的问题

This commit is contained in:
zlzw 2024-05-30 15:52:34 +08:00
parent c33975fe67
commit 5f3ebb4235
3 changed files with 67 additions and 24 deletions

View File

@ -4,7 +4,7 @@ ext {
buildToolsVersion: "29.0.2", buildToolsVersion: "29.0.2",
minSdkVersion : 21, minSdkVersion : 21,
targetSdkVersion : 33, targetSdkVersion : 33,
versionCode : 473, versionCode : 474,
versionName : "6.6.8" versionName : "6.6.8"
] ]
manifestPlaceholders = [ manifestPlaceholders = [

View File

@ -21,6 +21,7 @@ import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.MeasureSpec; import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -263,7 +264,7 @@ public class LiveTextRender {
} }
public interface CreatePrefixCallback { public interface CreatePrefixCallback {
void onPrefixCallback(SpannableStringBuilder builder); void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth);
} }
/** /**
@ -353,19 +354,20 @@ public class LiveTextRender {
index = builder.length(); index = builder.length();
} }
} }
createPrefixCallback.onPrefixCallback(builder); createPrefixCallback.onPrefixCallback(builder, 0);
} }
@Override @Override
public void onLoadFailed() { public void onLoadFailed() {
SpannableStringBuilder builder = new SpannableStringBuilder(); SpannableStringBuilder builder = new SpannableStringBuilder();
int index = 0; int index = 0;
int prefixWidth = 0;
if (levelDrawable != null) { if (levelDrawable != null) {
builder.append(" "); builder.append(" ");
levelDrawable.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(17)); levelDrawable.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(17));
builder.setSpan(new VerticalImageSpan(levelDrawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(new VerticalImageSpan(levelDrawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index = builder.length(); index = builder.length();
prefixWidth += levelDrawable.getBounds().width();
} }
if (gzDrawablesMap.containsKey(bean)) {//贵族 if (gzDrawablesMap.containsKey(bean)) {//贵族
@ -373,6 +375,7 @@ public class LiveTextRender {
gzDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(35), DpUtil.dp2px(20)); 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); builder.setSpan(new VerticalImageSpan(gzDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index = builder.length(); index = builder.length();
prefixWidth += gzDrawablesMap.get(bean).getBounds().width();
} }
if (ryxzDrawablesMap.containsKey(bean)) {//荣誉勋章 if (ryxzDrawablesMap.containsKey(bean)) {//荣誉勋章
@ -380,6 +383,7 @@ public class LiveTextRender {
ryxzDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(18), DpUtil.dp2px(18)); 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); builder.setSpan(new VerticalImageSpan(ryxzDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index = builder.length(); index = builder.length();
prefixWidth += ryxzDrawablesMap.get(bean).getBounds().width();
} }
if (nhDrawablesMap.containsKey(bean)) {//靓号 if (nhDrawablesMap.containsKey(bean)) {//靓号
@ -387,6 +391,7 @@ public class LiveTextRender {
nhDrawablesMap.get(bean).setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(17)); 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); builder.setSpan(new VerticalImageSpan(nhDrawablesMap.get(bean)), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index = builder.length(); index = builder.length();
prefixWidth += nhDrawablesMap.get(bean).getBounds().width();
} }
//在这里添加粉丝徽章的图片 //在这里添加粉丝徽章的图片
if (!TextUtils.isEmpty(bean.getMedalNmae()) && !TextUtils.isEmpty(bean.getMedalLevelImageUrl()) 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)); drawable.setBounds(0, 0, DpUtil.dp2px(44), DpUtil.dp2px(17));
builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index = builder.length(); index = builder.length();
prefixWidth += drawable.getBounds().width();
} }
} }
@ -421,7 +427,7 @@ public class LiveTextRender {
index = builder.length(); 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) { public void getGuardImage(Context mContext, int guardType, ImgLoader.DrawableCallback callback) {
if (guardType==0){ if (guardType == 0) {
callback.onLoadFailed(); callback.onLoadFailed();
}else { } else {
if (mContext instanceof Activity) { if (mContext instanceof Activity) {
if (((Activity) mContext).isDestroyed()) { if (((Activity) mContext).isDestroyed()) {
mContext = CommonAppContext.getTopActivity(); mContext = CommonAppContext.getTopActivity();
@ -684,7 +690,7 @@ public class LiveTextRender {
createPrefix(drawable, bean, new CreatePrefixCallback() { createPrefix(drawable, bean, new CreatePrefixCallback() {
@Override @Override
public void onPrefixCallback(SpannableStringBuilder builder) { public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) {
int color = Color.parseColor("#68F1F4"); int color = Color.parseColor("#68F1F4");
if (bean.isAnchor()) { if (bean.isAnchor()) {
color = Color.parseColor("#FBEABF"); color = Color.parseColor("#FBEABF");
@ -696,20 +702,20 @@ public class LiveTextRender {
builder = renderGift(color, builder, bean); builder = renderGift(color, builder, bean);
break; break;
default: default:
builder = renderChat(color, builder, bean); builder = renderChat(textView, color, builder, bean, prefixWidth);
break; break;
} }
textView.setText(builder); textView.setText(builder);
} }
}); });
} }// 123123123123123123123123
@Override @Override
public void onLoadFailed() { public void onLoadFailed() {
createPrefix(null, bean, new CreatePrefixCallback() { createPrefix(null, bean, new CreatePrefixCallback() {
@Override @Override
public void onPrefixCallback(SpannableStringBuilder builder) { public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) {
int color = 0; int color = 0;
if (bean.isAnchor()) { if (bean.isAnchor()) {
color = 0xffffdd00; color = 0xffffdd00;
@ -721,7 +727,7 @@ public class LiveTextRender {
builder = renderGift(color, builder, bean); builder = renderGift(color, builder, bean);
break; break;
default: default:
builder = renderChat(color, builder, bean); builder = renderChat(textView, color, builder, bean, prefixWidth);
break; break;
} }
textView.setText(builder); 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(); int length = builder.length();
String name = bean.getUserNiceName(); String name = bean.getUserNiceName();
if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定进场消息不允许加冒号 if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定进场消息不允许加冒号
name += ""; 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); builder.append(name);
//超皇设置渐变色展示昵称 //超皇设置渐变色展示昵称
if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getStartColor())) { 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); builder.setSpan(new ForegroundColorSpan(color), length, length + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
int lengthContent = builder.length(); 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()); builder.append(bean.getContent());
//超皇设置渐变色展示昵称 //超皇设置渐变色展示昵称
if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getContentColor())) { if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getContentColor())) {
@ -889,7 +932,7 @@ public class LiveTextRender {
if (textView != null) { if (textView != null) {
createPrefix(drawable, bean, new CreatePrefixCallback() { createPrefix(drawable, bean, new CreatePrefixCallback() {
@Override @Override
public void onPrefixCallback(SpannableStringBuilder builder) { public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) {
int start = builder.length(); int start = builder.length();
String name = bean.getUserNiceName() + " "; String name = bean.getUserNiceName() + " ";
builder.append(name); builder.append(name);
@ -906,7 +949,7 @@ public class LiveTextRender {
if (textView != null) { if (textView != null) {
createPrefix(null, bean, new CreatePrefixCallback() { createPrefix(null, bean, new CreatePrefixCallback() {
@Override @Override
public void onPrefixCallback(SpannableStringBuilder builder) { public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) {
int start = builder.length(); int start = builder.length();
String name = bean.getUserNiceName() + " "; String name = bean.getUserNiceName() + " ";
builder.append(name); builder.append(name);

View File

@ -10,7 +10,7 @@
<TextView <TextView
android:id="@+id/text_txt" android:id="@+id/text_txt"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="9dp" android:layout_marginLeft="9dp"
android:layout_marginRight="9dp" android:layout_marginRight="9dp"