From 82ccf66d80879314537ffe509f40921317b508dd Mon Sep 17 00:00:00 2001 From: 18401019693 Date: Wed, 21 Sep 2022 18:02:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=9A=87=E5=AD=97=E4=BD=93=E9=A2=9C?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yunbao/common/bean/IMLoginModel.java | 46 ++++++++++++++++ .../com/yunbao/live/bean/LiveChatBean.java | 36 +++++++++++++ .../yunbao/live/socket/SocketRyChatUtil.java | 11 +++- .../yunbao/live/socket/SocketRyClient.java | 7 +++ .../live/utils/LinearGradientFontSpan.java | 52 +++++++++++++++++++ .../com/yunbao/live/utils/LiveTextRender.java | 25 ++++----- 6 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 live/src/main/java/com/yunbao/live/utils/LinearGradientFontSpan.java diff --git a/common/src/main/java/com/yunbao/common/bean/IMLoginModel.java b/common/src/main/java/com/yunbao/common/bean/IMLoginModel.java index 11eebb93c..5bd325be8 100644 --- a/common/src/main/java/com/yunbao/common/bean/IMLoginModel.java +++ b/common/src/main/java/com/yunbao/common/bean/IMLoginModel.java @@ -213,6 +213,52 @@ public class IMLoginModel extends BaseModel { @SerializedName("mount_effect") private boolean mountEffect = false; + //超皇昵称渐变色上 + @SerializedName("startColor") + private String startColor = ""; + //超皇昵称渐变色下 + @SerializedName("endColor") + private String endColor = ""; + //超皇字体颜色 + @SerializedName("contentColor") + private String contentColor = ""; + + public String getNobleEndTime() { + return nobleEndTime; + } + + public IMLoginModel setNobleEndTime(String nobleEndTime) { + this.nobleEndTime = nobleEndTime; + return this; + } + + public String getStartColor() { + return startColor; + } + + public IMLoginModel setStartColor(String startColor) { + this.startColor = startColor; + return this; + } + + public String getEndColor() { + return endColor; + } + + public IMLoginModel setEndColor(String endColor) { + this.endColor = endColor; + return this; + } + + public String getContentColor() { + return contentColor; + } + + public IMLoginModel setContentColor(String contentColor) { + this.contentColor = contentColor; + return this; + } + public boolean isMountEffect() { return mountEffect; } diff --git a/live/src/main/java/com/yunbao/live/bean/LiveChatBean.java b/live/src/main/java/com/yunbao/live/bean/LiveChatBean.java index b1763c556..044c84de6 100644 --- a/live/src/main/java/com/yunbao/live/bean/LiveChatBean.java +++ b/live/src/main/java/com/yunbao/live/bean/LiveChatBean.java @@ -49,6 +49,42 @@ public class LiveChatBean { @SerializedName("bubbleSrc") private String bubbleSrc = ""; + //超皇昵称渐变色上 + @SerializedName("startColor") + private String startColor = ""; + //超皇昵称渐变色下 + @SerializedName("endColor") + private String endColor = ""; + //超皇字体颜色 + @SerializedName("contentColor") + private String contentColor = ""; + + public String getStartColor() { + return startColor; + } + + public LiveChatBean setStartColor(String startColor) { + this.startColor = startColor; + return this; + } + + public String getEndColor() { + return endColor; + } + + public LiveChatBean setEndColor(String endColor) { + this.endColor = endColor; + return this; + } + + public String getContentColor() { + return contentColor; + } + + public LiveChatBean setContentColor(String contentColor) { + this.contentColor = contentColor; + return this; + } public String getNobleId() { return nobleId; diff --git a/live/src/main/java/com/yunbao/live/socket/SocketRyChatUtil.java b/live/src/main/java/com/yunbao/live/socket/SocketRyChatUtil.java index c45b47fb3..65a9848fa 100644 --- a/live/src/main/java/com/yunbao/live/socket/SocketRyChatUtil.java +++ b/live/src/main/java/com/yunbao/live/socket/SocketRyChatUtil.java @@ -5,10 +5,13 @@ import android.util.Log; import com.adjust.sdk.Adjust; import com.adjust.sdk.AdjustEvent; import com.yunbao.common.CommonAppConfig; +import com.yunbao.common.CommonAppContext; import com.yunbao.common.Constants; +import com.yunbao.common.bean.IMLoginModel; import com.yunbao.common.bean.UserBean; import com.yunbao.common.http.CommonHttpUtil; import com.yunbao.common.http.HttpCallback; +import com.yunbao.common.manager.IMLoginManager; import com.yunbao.common.utils.WordUtil; import com.yunbao.live.R; import com.yunbao.live.http.LiveHttpUtil; @@ -79,6 +82,7 @@ public class SocketRyChatUtil { */ public static void sendChatMessage(String mLiveUid, String content, boolean isAnchor, int userType, int guardType) { UserBean u = CommonAppConfig.getInstance().getUserBean(); + IMLoginModel model = IMLoginManager.get(CommonAppContext.sInstance.getApplicationContext()).getUserInfo(); if (u == null) { return; } @@ -103,7 +107,12 @@ public class SocketRyChatUtil { .param("medal_name", u.getMedalName()) .param("medal_level", u.getMedalLevel()) .param("good_num", u.getGoodnum()) - .param("ct", content); + .param("ct", content) + .param("noble_id", String.valueOf(model.getNobleId())) + .param("contentColor", String.valueOf(model.getContentColor())) + .param("startColor", String.valueOf(model.getStartColor())) + .param("endColor", String.valueOf(model.getEndColor())) + ; msg.create(); String targetId = "g" + mLiveUid; diff --git a/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java b/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java index d2754005a..bb5cb7e94 100644 --- a/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java +++ b/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java @@ -605,6 +605,10 @@ public class SocketRyClient { chatBean.setVipType(map.getIntValue("vip_type")); chatBean.setMedal_honor(map.getString("medal_honor")); chatBean.setGood_nub(map.getString("good_num")); + chatBean.setNobleId(map.getString("noble_id"));//貴族ID + chatBean.setStartColor(map.getString("startColor"));//昵称开始颜色 + chatBean.setEndColor(map.getString("endColor"));//昵称结束颜色 + chatBean.setContentColor(map.getString("contentColor"));//文字内容颜色 if (!map.getString("guard_type").equals("undefined")) { chatBean.setGuardType(map.getIntValue("guard_type")); } @@ -660,6 +664,9 @@ public class SocketRyClient { chatBean.setContent(WordUtil.getString(R.string.live_enter_room)); chatBean.setGuardType(obj.getIntValue("guard_type")); chatBean.setNobleId(obj.getString("noble_id"));//貴族ID + chatBean.setStartColor(obj.getString("startColor"));//昵称开始颜色 + chatBean.setEndColor(obj.getString("endColor"));//昵称结束颜色 + chatBean.setContentColor(obj.getString("contentColor"));//文字内容颜色 chatBean.setNobleIcon(obj.getString("noble_icon"));//貴族圖標 chatBean.setBubbleSrc(obj.getString("bubbleSrc"));//氣泡 //增加粉丝徽章信息 diff --git a/live/src/main/java/com/yunbao/live/utils/LinearGradientFontSpan.java b/live/src/main/java/com/yunbao/live/utils/LinearGradientFontSpan.java new file mode 100644 index 000000000..5ea14439b --- /dev/null +++ b/live/src/main/java/com/yunbao/live/utils/LinearGradientFontSpan.java @@ -0,0 +1,52 @@ +package com.yunbao.live.utils; + +import android.graphics.Canvas; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.Shader; +import android.text.style.ReplacementSpan; + +/** + * 渐变色 + */ +public class LinearGradientFontSpan extends ReplacementSpan { + private int mSize; + private int startColor; + private int endColor; + + + public LinearGradientFontSpan(int startColor, int endColor) { + this.startColor = startColor; + this.endColor = endColor; + + } + + @Override + public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { + mSize = (int) (paint.measureText(text, start, end)); + + //这段不可以去掉,字体高度没设置,可能出现draw()方法没有被调用,如果你调用SpannableStringBuilder后append一个字符串,效果也是会出来,下面这段就不需要了 + // 原因详见https://stackoverflow.com/questions/20069537/replacementspans-draw-method-isnt-called + Paint.FontMetricsInt metrics = paint.getFontMetricsInt(); + if (fm != null) { + fm.top = metrics.top; + fm.ascent = metrics.ascent; + fm.descent = metrics.descent; + fm.bottom = metrics.bottom; + } + + return mSize; + } + + @Override + public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { + LinearGradient lg = new LinearGradient(0, 0, 0, paint.descent() - paint.ascent(), + startColor, + endColor, + Shader.TileMode.REPEAT); //从上到下渐变 + paint.setShader(lg); + + canvas.drawText(text, start, end, x, y, paint);//绘制文字 + } + +} 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 dfa750f5a..4ff1b43be 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveTextRender.java @@ -108,7 +108,7 @@ public class LiveTextRender { if (gzDrawables != null) {//贵族 builder.append(" "); - gzDrawables.setBounds(0, 0, DpUtil.dp2px(18), DpUtil.dp2px(18)); + gzDrawables.setBounds(0, 0, DpUtil.dp2px(32), DpUtil.dp2px(18)); builder.setSpan(new VerticalImageSpan(gzDrawables), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); index += 2; } @@ -159,15 +159,6 @@ public class LiveTextRender { } -// if (bean.getVipType() != 0) {//vip图标 -// Drawable vipDrawable = ContextCompat.getDrawable(CommonAppContext.sInstance, R.mipmap.icon_live_chat_vip); -// if (vipDrawable != null) { -// builder.append(" "); -// vipDrawable.setBounds(0, 0, DpUtil.dp2px(28), DpUtil.dp2px(14)); -// builder.setSpan(new VerticalImageSpan(vipDrawable), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); -// index += 2; -// } -// } if (bean.isManager()) {//直播间管理员图标 @@ -265,7 +256,7 @@ public class LiveTextRender { } public static void gz(final Context context, final TextView textView, final LiveChatBean bean) { - ImgLoader.displayDrawable(context, bean.getMedal(), new ImgLoader.DrawableCallback() { + ImgLoader.displayDrawable(context, bean.getMedal_new(), new ImgLoader.DrawableCallback() { @Override public void onLoadSuccess(Drawable drawable) { gzDrawables = null; @@ -344,8 +335,18 @@ public class LiveTextRender { name += ":"; } builder.append(name); - builder.setSpan(new ForegroundColorSpan(color), length, length + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + //超皇设置渐变色展示昵称 + if (TextUtils.equals("7",bean.getNobleId())){ + 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); + } + int lengthContent = builder.length(); builder.append(bean.getContent()); + //超皇设置渐变色展示昵称 + if (TextUtils.equals("7",bean.getNobleId())){ + 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) {