直播间聊天IM换行问题,没有解决

This commit is contained in:
老皮 2024-10-10 17:16:26 +08:00
parent 096ee7ee68
commit 92228b64bb

View File

@ -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;
}
/**
* 渲染普通聊天消息
*/