From cb0772c9e357b29c050826a92e09baece972e6ae Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Wed, 5 Jun 2024 09:48:22 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=9B=B4=E6=96=B0=E5=BC=B9=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../live/activity/LiveAudienceActivity.java | 25 +++- .../yunbao/live/adapter/LiveChatAdapter.java | 3 +- .../com/yunbao/live/utils/LiveTextRender.java | 122 ++++++++++++------ live/src/main/res/layout/item_live_chat.xml | 2 + 4 files changed, 111 insertions(+), 41 deletions(-) diff --git a/live/src/main/java/com/yunbao/live/activity/LiveAudienceActivity.java b/live/src/main/java/com/yunbao/live/activity/LiveAudienceActivity.java index 6d51d42b2..d093665b1 100644 --- a/live/src/main/java/com/yunbao/live/activity/LiveAudienceActivity.java +++ b/live/src/main/java/com/yunbao/live/activity/LiveAudienceActivity.java @@ -15,6 +15,7 @@ import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ImageView; import androidx.annotation.IdRes; @@ -229,21 +230,37 @@ public class LiveAudienceActivity extends LiveActivity { fragment.show(((FragmentActivity) mContext).getSupportFragmentManager(), "YoursystemisolderDialog"); IMLoginManager.get(mContext).setXiaJBG(true); } - /*new Handler(Looper.getMainLooper()) + /* new Handler(Looper.getMainLooper()) .postDelayed(new Runnable() { @Override public void run() { - System.out.println("------------------------初始化弹窗------------------------"); DebugDialog.getInstance(new DebugDialog.DebugDialogRunnable() { @Override public void run(DebugDialog dialog) { - System.out.println("------------------------显示弹窗------------------------"); dialog.setShowPattern(ShowPattern.ALL_TIME); dialog.show(); + + dialog.setView("+",new Button(mContext),view -> LiveTextRender.textTemp++); + dialog.setView("-",new Button(mContext),view -> LiveTextRender.textTemp--); + dialog.setView("+",new Button(mContext),view -> LiveTextRender.textTemp++); + dialog.setView("中/英",new Button(mContext),view ->{ + if(LiveTextRender.userName.startsWith("中")){ + LiveTextRender.userName="英文34567890"; + }else{ + LiveTextRender.userName="中文测试中文测试中文"; + } + }); + dialog.setView("长/短",new Button(mContext),view ->{ + if(LiveTextRender.userName.length()>5){ + LiveTextRender.userName="abcdefg"; + }else{ + LiveTextRender.userName="中文中文"; + } + }); LiveTextRender.onItemClickListener = new OnItemClickListener() { @Override public void onItemClick(String bean, int position) { - dialog.setParams(bean.split("\\|")[0], bean.split("\\|")[1]); + dialog.setParams(bean.split("\\|")[0], bean.split("\\|")[1]+"|"+position); } }; } diff --git a/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java b/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java index 9d515497e..bac417ed9 100644 --- a/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java +++ b/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java @@ -210,7 +210,8 @@ public class LiveChatAdapter extends RecyclerView.Adapter { class Vh extends RecyclerView.ViewHolder { LinearLayout mBg, view_follow, view_action_game, view_zg, xydComplete; - TextView mTextView, tv_chat_active_into, tv_chat_avtive_name, tv_zg_anchorname, textTxt2, automatic_chat; + TextView tv_chat_active_into, tv_chat_avtive_name, tv_zg_anchorname, textTxt2, automatic_chat; + TextView mTextView; RoundedImageView avatar; View v_chat_active_close; RecyclerView rv_chat_active; 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 c99006b84..c1014fa45 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java @@ -6,6 +6,7 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Rect; import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; @@ -23,6 +24,7 @@ import android.view.View; import android.view.View.MeasureSpec; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; @@ -296,34 +298,34 @@ public class LiveTextRender { int index = 0; if (levelDrawable != null) { - builder.append(" "); + builder.append("\uFFFC "); 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(); } if (gzDrawablesMap.containsKey(bean)) {//贵族 - builder.append(" "); + builder.append("\uFFFC "); 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(); } if (ryxzDrawablesMap.containsKey(bean)) {//荣誉勋章 - builder.append(" "); + builder.append("\uFFFC "); 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(); } if (nhDrawablesMap.containsKey(bean)) {//靓号 - builder.append(" "); + builder.append("\uFFFC "); 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(); } if (drawable != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable.setBounds(0, 0, DpUtil.dp2px(34), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); @@ -333,7 +335,7 @@ public class LiveTextRender { && !bean.getMedalNmae().equals("null") && !bean.getMedalNmae().equals("(null)") && !bean.getMedalNmae().equals("")) {//粉丝徽章图标 Drawable drawable2 = getMedalImage(bean); if (drawable2 != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable2.setBounds(0, 0, DpUtil.dp2px(44), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(drawable2), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); @@ -344,7 +346,7 @@ public class LiveTextRender { if (bean.isManager()) {//直播间管理员图标 Drawable drawable3 = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_m); if (drawable3 != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable3.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(14)); builder.setSpan(new VerticalImageSpan(drawable3), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); @@ -354,7 +356,7 @@ public class LiveTextRender { if (!TextUtils.isEmpty(bean.getLiangName())) {//靓号图标 Drawable drawable4 = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_liang); if (drawable4 != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable4.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(14)); builder.setSpan(new VerticalImageSpan(drawable4), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // new add @@ -375,7 +377,7 @@ public class LiveTextRender { int index = 0; int prefixWidth = 0; if (levelDrawable != null) { - builder.append(" "); + builder.append("\uFFFC "); 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(); @@ -383,7 +385,7 @@ public class LiveTextRender { } if (gzDrawablesMap.containsKey(bean)) {//贵族 - builder.append(" "); + builder.append("\uFFFC "); 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(); @@ -391,7 +393,7 @@ public class LiveTextRender { } if (ryxzDrawablesMap.containsKey(bean)) {//荣誉勋章 - builder.append(" "); + builder.append("\uFFFC "); 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(); @@ -399,14 +401,14 @@ public class LiveTextRender { } if (nhDrawablesMap.containsKey(bean)) {//靓号 - builder.append(" "); + builder.append("\uFFFC "); 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 (drawable != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable.setBounds(0, 0, DpUtil.dp2px(34), DpUtil.dp2px(17)); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); @@ -417,7 +419,7 @@ public class LiveTextRender { && !bean.getMedalNmae().equals("null") && !bean.getMedalNmae().equals("(null)") && !bean.getMedalNmae().equals("")) {//粉丝徽章图标 drawable = getMedalImage(bean); if (drawable != null) { - builder.append(" "); + builder.append("\uFFFC "); 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(); @@ -429,7 +431,7 @@ public class LiveTextRender { if (bean.isManager()) {//直播间管理员图标 drawable = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_m); if (drawable != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(14)); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index = builder.length(); @@ -440,7 +442,7 @@ public class LiveTextRender { if (!TextUtils.isEmpty(bean.getLiangName())) {//靓号图标 drawable = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_liang); if (drawable != null) { - builder.append(" "); + builder.append("\uFFFC "); drawable.setBounds(0, 0, DpUtil.dp2px(17), DpUtil.dp2px(14)); builder.setSpan(new VerticalImageSpan(drawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // new add @@ -832,13 +834,15 @@ public class LiveTextRender { return mNewBitmap; } - public static OnItemClickListener onItemClickListener=new OnItemClickListener() { + public static OnItemClickListener onItemClickListener = new OnItemClickListener() { @Override public void onItemClick(String bean, int position) { } }; private static boolean isInit = false; + public static int textTemp = 0; + public static String userName = ""; /** * 渲染普通聊天消息 @@ -846,33 +850,48 @@ public class LiveTextRender { private SpannableStringBuilder renderChat(TextView textView, int color, SpannableStringBuilder builder, LiveChatBean bean, int prefixWidth) { int length = builder.length(); String name = bean.getUserNiceName(); + float prefixEmpty = textView.getPaint().measureText(builder, 0, builder.length()) / 2; + if (!StringUtil.isEmpty(userName)) { + // name = userName; + } if (bean.getType() != LiveChatBean.ENTER_ROOM) {//产品规定,进场消息不允许加冒号 name += ":"; } SpannableStringBuilder sb = new SpannableStringBuilder(); - sb.append(builder).append(name).append(" "); + sb.append(name); float measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; - int width = ((ViewGroup) textView.getParent()).getWidth(); - onItemClickListener.onItemClick("测算|" + measuredText, 0); - onItemClickListener.onItemClick("控件宽度|" + width, 0); - onItemClickListener.onItemClick("前缀预留宽度|" + prefixWidth, 0); + // Rect mRect = new Rect(); + // textView.getPaint().getTextBounds(sb.toString(), 0, sb.length(), mRect); + int width = (int) (((ViewGroup) textView.getParent()).getMeasuredWidth() - prefixEmpty); + String TAG = "文字渲染"; + // Log.i(TAG, "测算|" + measuredText + "|" + mRect.width() + "|" + sb.toString()); + //|810|810|810|786|786 + // Log.i(TAG, "控件宽度|" + width + "|" + ((ViewGroup) textView.getParent()).getWidth() + "|" + ((ViewGroup) textView.getParent()).getMeasuredWidth() + "|" + textView.getWidth() + "|" + textView.getMeasuredWidth()); + // Log.i(TAG, "前缀预留宽度|" + prefixWidth + "|" + prefixEmpty); + // Log.i(TAG, "renderChat: " + textView.getCompoundPaddingLeft() + "|" + textView.getCompoundPaddingRight() + "|"); //检测渲染后图标+名字是否超过父布局宽度,超过了就做换行处理 + String prefixName = ""; if (measuredText > width) { sb.clear(); - sb.append(builder); for (int i = 0; i < name.length(); i++) { sb.append(name.charAt(i)); measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; - onItemClickListener.onItemClick(i + "逐测算|" + measuredText, 0); - if (measuredText > width) { - name = name.substring(0, i - 1) + "\n" + name.substring(i -1); + Log.i(TAG, i + "逐测算|" + measuredText + "|" + textTemp); + if (measuredText > width) { + prefixName = name.substring(0, i + textTemp); + name = prefixName + "\n" + name.substring(i + textTemp); break; } } sb.clear(); + } else { + prefixName = name; } + sb.clear(); + sb.append(prefixName); // name=name.substring(0,7)+"\n"+name.substring(7); builder.append(name); + Log.i(TAG, "再测算|" + (textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth) + "|" + textTemp + "|" + sb.toString()); //超皇设置渐变色展示昵称 if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getStartColor())) { @@ -882,26 +901,56 @@ public class LiveTextRender { } int lengthContent = builder.length(); //这一段先别删,万一后面要改内容换行呢 - /* sb.clear(); - sb.append(builder).append(bean.getContent()); + // TODO 试试 “空Unicode” + /*sb.clear(); + TAG = "文字渲染2"; + String content = bean.getContent(); + Log.i(TAG, "renderChat: " + content); + sb.append(name).append(content); + SpannableStringBuilder csb = null; measuredText = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + sb.clear(); + sb.append(prefixName); + prefixEmpty = textView.getPaint().measureText(sb, 0, sb.length()) + prefixWidth; + StringBuilder stringBuilder = new StringBuilder(); 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; + sb.append(name); + float lineWidth = 0; + for (int i = 0; i < content.length(); i++) { + char charAt = content.charAt(i); + sb.append(charAt); + lineWidth += textView.getPaint().measureText(String.valueOf(charAt)) + prefixEmpty; + prefixEmpty = 0; + textView.setText(sb); + Log.i(TAG, "renderChat: TextView是否换行:" + textView.getText().toString().split("\n").length); + Log.i(TAG, "逐测算|" + lineWidth + " width|" + width + "|" + sb.toString()); + if (lineWidth > width) { + Log.i(TAG, "renderChat: 换行:" + charAt); + if (!stringBuilder.toString().endsWith("\n")) { + stringBuilder.append("\n"); + } + lineWidth = 0; + --i; + } else { + Log.i(TAG, "renderChat: 直接加入:" + charAt); + stringBuilder.append(content.charAt(i)); } + } + if (stringBuilder.toString().endsWith("\n")) { + Log.i(TAG, "需要删除回车"); + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + Log.i(TAG, "分行后内容:" + stringBuilder.toString()); + bean.setContent(stringBuilder.toString()); } */ + builder.append(bean.getContent()); //超皇设置渐变色展示昵称 if (TextUtils.equals("7", bean.getNobleId()) && !TextUtils.isEmpty(bean.getContentColor())) { + Log.i(TAG, "lengthContent: " + lengthContent + "|content: " + bean.getContent().length() + "|max: " + (lengthContent + bean.getContent().length())); builder.setSpan(new ForegroundColorSpan(Color.parseColor(bean.getContentColor())), lengthContent, lengthContent + bean.getContent().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (bean.getType() == LiveChatBean.LIGHT) { @@ -913,6 +962,7 @@ public class LiveTextRender { builder.setSpan(new VerticalImageSpan(heartDrawable), length - 1, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } + //textView.setText(""); return builder; } diff --git a/live/src/main/res/layout/item_live_chat.xml b/live/src/main/res/layout/item_live_chat.xml index da2996682..a6e5fddfb 100644 --- a/live/src/main/res/layout/item_live_chat.xml +++ b/live/src/main/res/layout/item_live_chat.xml @@ -16,7 +16,9 @@ android:layout_marginRight="9dp" android:paddingBottom="7dp" android:textColor="#fff" + android:breakStrategy="simple" android:textSize="12sp" + tools:text="aaaaaaaaa111111111111111111111111111111111111111111111111111111111" android:visibility="visible" tools:ignore="RtlHardcoded" />