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 b0458c685..81bf31316 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java @@ -52,6 +52,8 @@ import com.yunbao.common.utils.WordUtil; import com.yunbao.live.R; import com.yunbao.live.bean.LiveChatBean; +import net.lucode.hackware.magicindicator.buildins.UIUtil; + import java.util.HashMap; import java.util.List; import java.util.Map; @@ -282,7 +284,6 @@ public class LiveTextRender { */ private void createPrefix(Drawable levelDrawable, LiveChatBean bean, CreatePrefixCallback createPrefixCallback) { - // if (bean.getGuardType() != Constants.GUARD_TYPE_NONE) {//守护图标 // Drawable drawable; // if (bean.getGuardType() == Constants.GUARD_TYPE_MONTH) { @@ -458,6 +459,103 @@ public class LiveTextRender { } + private void createPrefixNew(Drawable levelDrawable, LiveChatBean bean,TextView textView, CreatePrefixCallback createPrefixCallback) { + + + getGuardImage(CommonAppContext.sInstance, bean.getGuardType(), new ImgLoader.DrawableCallback() { + @Override + public void onLoadSuccess(Drawable drawable) { + show(drawable); + } + + @Override + public void onLoadFailed() { + show(null); + } + + void show(Drawable drawable) { + SpannableStringBuilder builder = new SpannableStringBuilder(); + int[] prefixWidthHolder = {0}; // 用于累加 prefixWidth + + // 定义一个辅助方法来添加 Drawable 并累加宽度 + // 见上述 appendDrawable 方法定义 + + // 添加 levelDrawable + if (levelDrawable != null) { + appendDrawable(builder, levelDrawable, 17, 17, textView, prefixWidthHolder); + } + + // 添加贵族图标 + if (gzDrawablesMap.containsKey(bean)) { + Drawable gzDrawable = gzDrawablesMap.get(bean); + appendDrawable(builder, gzDrawable, 35, 20, textView, prefixWidthHolder); + } + + // 添加荣誉勋章 + if (ryxzDrawablesMap.containsKey(bean)) { + Drawable ryxzDrawable = ryxzDrawablesMap.get(bean); + appendDrawable(builder, ryxzDrawable, 18, 18, textView, prefixWidthHolder); + } + + // 添加靓号图标 + if (nhDrawablesMap.containsKey(bean)) { + Drawable nhDrawable = nhDrawablesMap.get(bean); + appendDrawable(builder, nhDrawable, 17, 17, textView, prefixWidthHolder); + } + + // 添加其他 Drawable + if (drawable != null) { + appendDrawable(builder, drawable, 34, 17, textView, prefixWidthHolder); + } + + // 添加粉丝徽章 + if (!TextUtils.isEmpty(bean.getMedalNmae()) && !TextUtils.isEmpty(bean.getMedalLevelImageUrl()) + && !bean.getMedalNmae().equals("null") && !bean.getMedalNmae().equals("(null)") && !bean.getMedalNmae().equals("")) { + Drawable medalDrawable = getMedalImage(bean); + if (medalDrawable != null) { + appendDrawable(builder, medalDrawable, 44, 17, textView, prefixWidthHolder); + } + } + + // 添加直播间管理员图标 + if (bean.isManager()) { + Drawable managerDrawable = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_m); + if (managerDrawable != null) { + appendDrawable(builder, managerDrawable, 17, 14, textView, prefixWidthHolder); + } + } + + // 添加靓号图标 + if (!TextUtils.isEmpty(bean.getLiangName())) { + Drawable liangDrawable = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_liang); + if (liangDrawable != null) { + appendDrawable(builder, liangDrawable, 17, 14, textView, prefixWidthHolder); + } + } + + // 获取最终的 prefixWidth + int prefixWidth = prefixWidthHolder[0]; + createPrefixCallback.onPrefixCallback(builder, prefixWidth); + } + }); + } + + + private void appendDrawable(SpannableStringBuilder builder, Drawable drawable, int widthDp, int heightDp, TextView textView, int[] prefixWidthHolder) { + if (drawable != null) { + int index = builder.length(); + builder.append("\uFFFC "); + drawable.setBounds(0, 0, DpUtil.dp2px(widthDp), DpUtil.dp2px(heightDp)); + builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + prefixWidthHolder[0] += drawable.getBounds().width(); + + // 计算空格的宽度并累加 + TextPaint paint = textView.getPaint(); + float spaceWidth = paint.measureText(" "); + prefixWidthHolder[0] += (int) spaceWidth; + } + } + private Drawable getMedalImage(final LiveChatBean bean) { View view = LayoutInflater.from(CommonAppContext.sInstance).inflate(R.layout.live_medal_icon, null, false); @@ -501,6 +599,7 @@ public class LiveTextRender { @Override public void onLoadFailed() { gz(context, textView, bean); + } }); } @@ -707,13 +806,11 @@ public class LiveTextRender { private void renderTMP(Context context, final TextView textView, final LiveChatBean bean) { - if (textView != null) { getLiveLevelImage(context, bean.getLevel(), false, new ImgLoader.DrawableCallback() { @Override public void onLoadSuccess(Drawable drawable) { - - createPrefix(drawable, bean, new CreatePrefixCallback() { + createPrefixNew(drawable, bean,textView, new CreatePrefixCallback() { @Override public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int color = Color.parseColor("#68F1F4"); @@ -727,8 +824,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); + builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -739,7 +835,7 @@ public class LiveTextRender { @Override public void onLoadFailed() { - createPrefix(null, bean, new CreatePrefixCallback() { + createPrefixNew(null, bean,textView, new CreatePrefixCallback() { @Override public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int color = 0; @@ -753,8 +849,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); + builder = renderChatNew(textView, color, builder, bean, prefixWidth); break; } textView.setText(builder); @@ -849,50 +944,72 @@ public class LiveTextRender { public static String userName = ""; private SpannableStringBuilder renderChatNew(TextView textView, int color, SpannableStringBuilder builder, LiveChatBean bean, int prefixWidth) { - // prefixWidth 图片title 的宽度 + int length = builder.length(); - // 1、计算textview的可用宽度 + // availableWidth textView 可用宽度 prefixWidth 图片宽度 int availableWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); - Log.e("renderChatNew", "availableWidth: " + availableWidth); - +// Log.e("renderChatNew", "availableWidth: " + availableWidth); StringBuilder sb = new StringBuilder(); String name = bean.getUserNiceName(); - if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定,进场消息不允许加冒号 + if (bean.getType() != LiveChatBean.ENTER_ROOM) { // 产品规定,进场消息不允许加冒号 name += ":"; } + + int lengthContent = length + name.length(); + sb.append(name).append(bean.getContent()); - // 2、计算名称➕内容 的宽度 TextPaint paint = textView.getPaint(); float nameWidth = paint.measureText(sb.toString()); - Log.e("renderChatNew", "nameWidth: " + nameWidth + " prefixWidth: " + prefixWidth); +// Log.e("renderChatNew", "nameWidth: " + nameWidth + " prefixWidth: " + prefixWidth); - if (nameWidth + prefixWidth > availableWidth){ - // 4、如果名称➕内容 的宽度大于textview的可用宽度,则计算字符后插入换行符 + if (nameWidth + prefixWidth > availableWidth) { 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); + float measuredText = paint.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.deleteCharAt(sb_tmp.length() - 1); sb_tmp.append("\n"); sb_tmp.append(content); - Log.e("renderChatNew", "sb_tmp.toString():" + sb_tmp.toString()); +// Log.e("renderChatNew", "sb_tmp.toString():" + sb_tmp.toString()); } } builder.append(sb_tmp); - Log.e("renderChatNew", "builder.toString():" + builder.toString()); +// Log.e("renderChatNew", "builder.toString():" + builder.toString()); - }else { + } 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; } @@ -1071,7 +1188,7 @@ public class LiveTextRender { @Override public void onLoadSuccess(Drawable drawable) { if (textView != null) { - createPrefix(drawable, bean, new CreatePrefixCallback() { + createPrefixNew(drawable, bean,textView, new CreatePrefixCallback() { @Override public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int start = builder.length(); @@ -1088,7 +1205,7 @@ public class LiveTextRender { @Override public void onLoadFailed() { if (textView != null) { - createPrefix(null, bean, new CreatePrefixCallback() { + createPrefixNew(null, bean,textView, new CreatePrefixCallback() { @Override public void onPrefixCallback(SpannableStringBuilder builder, int prefixWidth) { int start = builder.length();