从pd拷贝 1、优化直播间聊天换行问题_2、PK连麦_结束图标(英文)更换

图,因为缩略图和主图加载完都回调,会导致计算换行的代码和之后转Drawable的代码重复多次调用
This commit is contained in:
ningwenqiang 2024-10-23 10:16:36 +08:00
parent c166d0a476
commit 7c6189db11

View File

@ -13,6 +13,7 @@ import android.graphics.drawable.Drawable;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan; import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
@ -725,7 +726,7 @@ public class LiveTextRender {
builder = renderGift(color, builder, bean); builder = renderGift(color, builder, bean);
break; break;
default: default:
builder = renderChat(textView, color, builder, bean, prefixWidth); builder = renderChatNew(textView, color, builder, bean, prefixWidth);
break; break;
} }
textView.setText(builder); textView.setText(builder);
@ -750,7 +751,7 @@ public class LiveTextRender {
builder = renderGift(color, builder, bean); builder = renderGift(color, builder, bean);
break; break;
default: default:
builder = renderChat(textView, color, builder, bean, prefixWidth); builder = renderChatNew(textView, color, builder, bean, prefixWidth);
break; break;
} }
textView.setText(builder); textView.setText(builder);
@ -844,9 +845,89 @@ public class LiveTextRender {
public static int textTemp = 0; public static int textTemp = 0;
public static String userName = ""; 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) { 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();