pdlivexp/live/src/main/java/com/yunbao/live/views/LiveAudienceViewHolder.java
2022-08-17 14:12:31 +08:00

672 lines
27 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.yunbao.live.views;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.alibaba.fastjson.JSONObject;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;
import com.yunbao.common.CommonAppConfig;
import com.yunbao.common.Constants;
import com.yunbao.common.bean.MsgModel;
import com.yunbao.common.dialog.LiveTotalDialog;
import com.yunbao.common.event.MessageIMEvent;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.http.HttpCallback;
import com.yunbao.common.http.HttpClient;
import com.yunbao.common.manager.imrongcloud.MessageIMManager;
import com.yunbao.common.utils.Bus;
import com.yunbao.common.utils.DpUtil;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.views.weight.ViewClicksAntiShake;
import com.yunbao.live.R;
import com.yunbao.live.activity.LiveActivity;
import com.yunbao.live.activity.LiveAudienceActivity;
import com.yunbao.live.bean.LiveChatBean;
import com.yunbao.live.dialog.LiveHDDialogFragment;
import com.yunbao.live.dialog.LiveMicUserDialogFragment;
import com.yunbao.live.dialog.LivePromotionDialogFragment;
import com.yunbao.live.event.LiveAudienceEvent;
import com.yunbao.live.http.LiveHttpUtil;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/**
* Created by cxf on 2018/10/9.
* 观众直播间逻辑
*/
public class LiveAudienceViewHolder extends AbsLiveViewHolder {
private String mLiveUid;
private String mStream;
private String url;
private LinearLayout lt_trickery;
private TextView tv_trickery_time;
private View v_msg_redpoint, stationHornBanner;
private Activity context;
private SVGAImageView svga_new_user_gif, svga_new_user_double, svga_new_user_follow, svga_station_horn;
private ViewFlipper viewFlipper;
private TextView goToRomm;
private int icon = 0;
private String nobleName, nobleTtext;
public LiveAudienceViewHolder(Context context, ViewGroup parentView) {
super(context, parentView);
this.context = (Activity) context;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onUpdata(String str) {
if ("svga_new_user_gif".equals(str)) {
svga_new_user_gif();
} else if ("svga_new_user_double".equals(str)) {
svga_new_user_double();
} else if ("svga_new_user_follow".equals(str)) {
svga_new_user_follow();
} else if ("stop_svga_new_user_double".equals(str)) {
svga_new_user_double.setVisibility(View.GONE);
svga_new_user_double.stopAnimation();
svga_new_user_double.clear();
} else if ("stop_svga_new_user_follow".equals(str)) {
svga_new_user_follow.setVisibility(View.GONE);
svga_new_user_follow.stopAnimation();
svga_new_user_follow.clear();
} else if ("stop_svga_new_user_gif".equals(str)) {
svga_new_user_gif.setVisibility(View.GONE);
svga_new_user_gif.stopAnimation();
svga_new_user_gif.clear();
} else if ("name_true".equals(str)) {
if (LiveRoomViewHolder.mName.getText().length() <= 4) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(DpUtil.dp2px(116), DpUtil.dp2px(40));
layoutParams.setMargins(DpUtil.dp2px(80), DpUtil.dp2px(80), 0, 0);
svga_new_user_follow.setLayoutParams(layoutParams);
} else {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(DpUtil.dp2px(116), DpUtil.dp2px(40));
layoutParams.setMargins(DpUtil.dp2px(100), DpUtil.dp2px(80), 0, 0);
svga_new_user_follow.setLayoutParams(layoutParams);
}
}
}
private void svga_new_user_gif() {
new SVGAParser(mContext).decodeFromAssets("free_gift_tip.svga", new SVGAParser.ParseCompletion() {
@Override
public void onComplete(SVGAVideoEntity videoItem) {
SVGADrawable drawable = new SVGADrawable(videoItem);
svga_new_user_gif.setImageDrawable(drawable);
svga_new_user_gif.startAnimation();
}
@Override
public void onError() {
Log.e("errqs", "errl");
}
},null);
}
private void svga_new_user_double() {
new SVGAParser(mContext).decodeFromAssets("double_click_tip.svga", new SVGAParser.ParseCompletion() {
@Override
public void onComplete(SVGAVideoEntity videoItem) {
SVGADrawable drawable = new SVGADrawable(videoItem);
svga_new_user_double.setImageDrawable(drawable);
svga_new_user_double.setBackgroundResource(R.mipmap.black_bg);
svga_new_user_double.startAnimation();
}
@Override
public void onError() {
Log.e("errqs", "errl");
}
},null);
}
private void svga_new_user_follow() {
new SVGAParser(mContext).decodeFromAssets("live_follow_tip.svga", new SVGAParser.ParseCompletion() {
@Override
public void onComplete(SVGAVideoEntity videoItem) {
SVGADrawable drawable = new SVGADrawable(videoItem);
svga_new_user_follow.setImageDrawable(drawable);
svga_new_user_follow.startAnimation();
}
@Override
public void onError() {
Log.e("errqs", "errl");
}
},null);
}
@Override
protected int getLayoutId() {
return R.layout.view_live_audience;
}
@Override
public void init() {
super.init();
EventBus.getDefault().register(this);
findViewById(R.id.btn_close).setOnClickListener(this);
findViewById(R.id.btn_share).setOnClickListener(this);
findViewById(R.id.btn_red_pack).setOnClickListener(this);
findViewById(R.id.btn_gift).setOnClickListener(this);
findViewById(R.id.btn_zg).setOnClickListener(this);
svga_new_user_gif = (SVGAImageView) findViewById(R.id.svga_new_user_gif);
svga_new_user_double = (SVGAImageView) findViewById(R.id.svga_new_user_double);
svga_new_user_follow = (SVGAImageView) findViewById(R.id.svga_new_user_follow);
svga_station_horn = (SVGAImageView) findViewById(R.id.svga_station_horn);
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper_banner);
stationHornBanner = findViewById(R.id.station_horn_banner);
svga_new_user_double.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((LiveAudienceActivity) mContext).light();
}
});
findViewById(R.id.btn_mic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取房间连麦状态
HttpClient.getInstance().get("live.getDrLm", "live.getDrLm")
.params("uid", mLiveUid, true)
.execute(new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (code == 0) {
LiveMicUserDialogFragment fragment = new LiveMicUserDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(Constants.LIVE_UID, mLiveUid);
bundle.putString(Constants.STREAM, mStream);
bundle.putString("By", "1");
fragment.setArguments(bundle);
fragment.show(((LiveAudienceActivity) mContext).getSupportFragmentManager(), "LiveUserMoreDialogFragment");
LiveMicUserDialogFragment.activity = ((LiveAudienceActivity) mContext);
} else {
ToastUtil.show(R.string.no_mic_opn);
}
}
});
}
});
findViewById(R.id.btn_hd).setOnClickListener(new View.OnClickListener() {//游戏,活动按钮
@Override
public void onClick(View view) {
LivePromotionDialogFragment fragment = new LivePromotionDialogFragment();
fragment.show(((LiveAudienceActivity) mContext).getSupportFragmentManager(), "LivePromotionDialogFragment");
}
});
findViewById(R.id.btn_more).setOnClickListener(this);
lt_trickery = (LinearLayout) findViewById(R.id.lt_trickery);
tv_trickery_time = (TextView) findViewById(R.id.tv_trickery_time);
lt_trickery.setOnClickListener(this);
dialog = new Dialog(mContext, R.style.dialog);
v_msg_redpoint = (View) findViewById(R.id.v_msg_redpoint);
//获取系统未读消息
MessageIMManager.get(mContext).getSystemMessages();
//全站喇叭
svga_station_horn.setCallback(new SVGACallback() {
@Override
public void onPause() {
}
@Override
public void onFinished() {
}
@Override
public void onRepeat() {
}
@Override
public void onStep(int frame, double percentage) {
Log.e("SVGADynamicEntity", "frame" + frame);
Log.e("SVGADynamicEntity", "percentage" + percentage);
if ((int) (percentage * 100) == 5) {
viewFlipper.removeAllViews();
stationHornBanner.setVisibility(View.VISIBLE);
if ((nobleName + nobleTtext).length() > 15) {
for (int i = 0; i < 2; i++) {
//展示内容子布局
View childrenView = LayoutInflater.from(mContext).inflate(R.layout.view_whole_station_horn, null, false);
ImageView iconImg = childrenView.findViewById(R.id.icon_img);
TextView userName = childrenView.findViewById(R.id.rc_user_name);
TextView userText = childrenView.findViewById(R.id.rc_user_text);
userName.setText(nobleName);
userText.setText(" : " + nobleTtext);
ImgLoader.display2(mContext, icon, iconImg);
viewFlipper.addView(childrenView);
}
} else {
//展示内容子布局
View childrenView = LayoutInflater.from(mContext).inflate(R.layout.view_whole_station_horn, null, false);
ImageView iconImg = childrenView.findViewById(R.id.icon_img);
TextView userName = childrenView.findViewById(R.id.rc_user_name);
TextView userText = childrenView.findViewById(R.id.rc_user_text);
userName.setText(nobleName);
userText.setText(" : " + nobleTtext);
ImgLoader.display2(mContext, icon, iconImg);
viewFlipper.addView(childrenView);
}
}
if ((int) (percentage * 100) == 50 && viewFlipper.getChildCount() > 1) {
viewFlipper.showNext();
}
if ((int) (percentage * 100) == 92) {
stationHornBanner.setVisibility(View.GONE);
}
}
});
//底部改造
ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.total_image), new ViewClicksAntiShake.ViewClicksCallBack() {
@Override
public void onViewClicks() {
Bus.get().post(new LiveAudienceEvent()
.setType(LiveAudienceEvent.LiveAudienceType.BOTTOMCOLLECTION));
}
});
}
public static Handler handler = new Handler();
public static Runnable runnable;
public static Handler handler1 = new Handler();
public static Runnable runnable1;
public void setLiveInfo(String liveUid, String stream, String urls, int isattention) {
mLiveUid = liveUid;
mStream = stream;
url = urls;
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
if (isattention == 0) {
LiveChatBean lvBean = new LiveChatBean();
lvBean.setType(-1);
lvBean.setId(url);
LiveRoomViewHolder.insertChat(lvBean, 1);
}
LiveChatBean lvBean2 = new LiveChatBean();
lvBean2.setType(-2);
LiveRoomViewHolder.insertChat(lvBean2, 1);
}
}, 10000);//3秒后执行Runnable中的run方法
}
@Override
public void onClick(View v) {
if (!canClick()) {
return;
}
super.onClick(v);
int i = v.getId();
if (i == R.id.btn_close) {
close();
} else if (i == R.id.btn_share) {
((LiveAudienceActivity) mContext).light();
} else if (i == R.id.btn_red_pack) {
((LiveActivity) mContext).openRedPackSendWindow();
} else if (i == R.id.btn_gift) {
openGiftWindow();
//全屏
} else if (i == R.id.btn_more) {
((LiveActivity) mContext).openMoreWindow();
} else if (i == R.id.btn_zg) {
openZGListWindow();
} else if (i == R.id.lt_trickery) {
showTrickeryDialog();
}
}
/**
* 观众打开主播的整蛊惑单窗口
*/
public void openZGListWindow() {
String url = CommonAppConfig.HOST + "/index.php?g=Appapi&m=Turntable&a=tricky";
url += "&uid=" + CommonAppConfig.getInstance().getUid() + "&token="
+ CommonAppConfig.getInstance().getToken() + "&anchorUid=" + mLiveUid;
Log.i("tag", url);
Bundle bundle1 = new Bundle();
bundle1.putString("url", url);
LiveHDDialogFragment liveHDDialogFragment = new LiveHDDialogFragment();
liveHDDialogFragment.setArguments(bundle1);
liveHDDialogFragment.show(((LiveAudienceActivity) mContext).getSupportFragmentManager(), "LiveHDDialogFragment");
}
private Dialog dialog;
private void showTrickeryDialog() {
dialog.setContentView(R.layout.dialog_trickery);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
TextView tv_trickster_initiator, tv_trickster_content, tv_trickery_number_zuanshi, tv_username, tv_anchorname, tv_trickster_name;
Button bt_trickster_confirm;
LinearLayout lt_trickster_salvation, lt_trickster_top, lt_trickster, lt_trickster_suc;
lt_trickster_top = (LinearLayout) dialog.findViewById(R.id.lt_trickster_top);
lt_trickster = (LinearLayout) dialog.findViewById(R.id.lt_trickster);
lt_trickster_suc = (LinearLayout) dialog.findViewById(R.id.lt_trickster_suc);
tv_trickster_initiator = (TextView) dialog.findViewById(R.id.tv_trickster_initiator);
tv_trickster_content = (TextView) dialog.findViewById(R.id.tv_trickster_content);
tv_trickery_number_zuanshi = (TextView) dialog.findViewById(R.id.tv_trickery_number_zuanshi);
bt_trickster_confirm = (Button) dialog.findViewById(R.id.bt_trickster_confirm);
lt_trickster_salvation = (LinearLayout) dialog.findViewById(R.id.lt_trickster_salvation);
tv_trickster_initiator.setText(name);
tv_trickster_content.setText(content);
tv_trickery_number_zuanshi.setText(mContext.getResources().getString(R.string.consume) + nums);
tv_username = (TextView) dialog.findViewById(R.id.tv_username);
tv_anchorname = (TextView) dialog.findViewById(R.id.tv_anchorname);
tv_trickster_name = (TextView) dialog.findViewById(R.id.tv_trickster_name);
if (openType == 0) {
lt_trickster_top.setBackground(mContext.getResources().getDrawable(R.mipmap.img_pop_zhengjou));
lt_trickster_salvation.setVisibility(View.VISIBLE);
bt_trickster_confirm.setVisibility(View.GONE);
bt_trickster_confirm.setText(mContext.getResources().getString(R.string.confirm));
lt_trickster.setVisibility(View.VISIBLE);
lt_trickster_suc.setVisibility(View.GONE);
} else if (openType == 1) {
lt_trickster_top.setBackground(mContext.getResources().getDrawable(R.mipmap.img_pop_ing));
lt_trickster_salvation.setVisibility(View.GONE);
bt_trickster_confirm.setVisibility(View.VISIBLE);
lt_trickster.setVisibility(View.VISIBLE);
lt_trickster_suc.setVisibility(View.GONE);
} else if (openType == 2) {
lt_trickster_top.setBackground(mContext.getResources().getDrawable(R.mipmap.img_pop_chenggong));
lt_trickster_salvation.setVisibility(View.GONE);
bt_trickster_confirm.setVisibility(View.VISIBLE);
bt_trickster_confirm.setText(mContext.getResources().getString(R.string.trickster_thanks));
lt_trickster.setVisibility(View.GONE);
lt_trickster_suc.setVisibility(View.VISIBLE);
tv_username.setText(userNmae);
tv_anchorname.setText(anchorName);
tv_trickster_name.setText(tricksterName);
}
bt_trickster_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
lt_trickster_salvation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hopeRankTurntable();
}
});
dialog.show();
}
private String nums = "0", prankid = "", content = "", name = "";
private String userNmae = "", anchorName = "", tricksterName = "";
/**
* 打开或者关闭整蛊
*/
public void closeAndOpenTrickery(String msgtype, int time, JSONObject jsonObject) {
nowTime = time;
tv_trickery_time.setText("" + nowTime);
openType = 0;
if ("1".equals(msgtype)) {
nums = jsonObject.getString("nums");
prankid = jsonObject.getString("prankid");
content = jsonObject.getString("content");
name = jsonObject.getString("user_nicename");
lt_trickery.setVisibility(View.VISIBLE);
lt_trickery.setBackground(mContext.getResources().getDrawable(R.mipmap.img_buoy));
isClose = false;
starTimeloud(1, time);
} else {
userNmae = jsonObject.getString("hope_nicename");
anchorName = jsonObject.getString("anchor_nicename");
tricksterName = jsonObject.getString("content");
isClose = true;
if (dialog.isShowing()) {
dialog.dismiss();
}
openType = 2;
showTrickeryDialog();
}
}
public CountDownTimer countDownTimerTrickery = null;
private boolean isClose = false;
private int nowTime = 0;
private Handler handlerT = new Handler();
private int openType = 0;
private void starTimeloud(int type, int time) {
nowTime = time;
countDownTimerTrickery = new CountDownTimer(time * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
nowTime = nowTime - 1;
if (nowTime < 0) {
if (countDownTimerTrickery != null) {
countDownTimerTrickery.cancel();
}
nowTime = 0;
}
tv_trickery_time.setText("" + nowTime);
if (isClose) {
countDownTimerTrickery.cancel();
countDownTimerTrickery = null;
lt_trickery.setVisibility(View.GONE);
}
}
/**
*倒计时结束后调用的
*/
@Override
public void onFinish() {
tv_trickery_time.setText("0");
if (type == 1) {
lt_trickery.setBackground(mContext.getResources().getDrawable(R.mipmap.img_buoy2));
if (countDownTimerTrickery != null) {
countDownTimerTrickery.cancel();
countDownTimerTrickery = null;
}
nowTime = 0;
openType = 1;
if (dialog.isShowing()) {
dialog.dismiss();
}
starTimeloud(2, time);
} else {
lt_trickery.setVisibility(View.GONE);
timeOver();
}
}
};
countDownTimerTrickery.start();
}
public void hopeRankTurntable() {
LiveHttpUtil.userHopePrankTurntable(mLiveUid, prankid, new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (countDownTimerTrickery != null) {
}
}
});
}
public void timeOver() {
}
//消息中心
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onMessageIMEvent(MessageIMEvent event) {
//判断消息中心红点是否已经展示如果已经展示并且未读消息大于0则不再执行之后的逻辑
int number = event.getNumber();
if (v_msg_redpoint.getVisibility() == View.VISIBLE && number > 0) return;
if (number > 0) {
v_msg_redpoint.setVisibility(View.VISIBLE);
} else {
v_msg_redpoint.setVisibility(View.GONE);
}
}
/**
* 退出直播间
*/
private void close() {
handler.removeCallbacks(runnable);
handler1.removeCallbacks(runnable1);
((LiveAudienceActivity) mContext).onBackPressed();
}
/**
* 打开礼物窗口
*/
private void openGiftWindow() {
((LiveAudienceActivity) mContext).openGiftWindow();
}
/**
* 打开分享窗口
*/
private void openShareWindow() {
((LiveActivity) mContext).openShareWindow();
}
@Override
public void release() {
super.release();
EventBus.getDefault().unregister(this);
}
/**
* 绘制全站喇叭的样式
*
* @return
*/
@SuppressLint("SetTextI18n")
private Bitmap canvasBitmap(int icon, String name, String text) {
// Log.e("SVGADynamicEntity", "子布局宽度:" + name);
// Log.e("SVGADynamicEntity", "fu布局宽度" + text);
//计算设备分辨率
WindowManager manager = ((Activity) mContext).getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
View view = LayoutInflater.from(mContext).inflate(R.layout.view_whole_station_horn_layout, null, false);
ViewFlipper viewFlipper = view.findViewById(R.id.viewflipper_banner);
//展示内容子布局
View childrenView = LayoutInflater.from(mContext).inflate(R.layout.view_whole_station_horn, null, false);
ImageView iconImg = childrenView.findViewById(R.id.icon_img);
TextView userName = childrenView.findViewById(R.id.rc_user_name);
TextView userText = childrenView.findViewById(R.id.rc_user_text);
userName.setText(name);
userText.setText(" : " + text);
ImgLoader.display2(mContext, icon, iconImg);
viewFlipper.addView(childrenView);
//测量使得view指定大小
int measureWidth = View.MeasureSpec.makeMeasureSpec(width / 3 * 2 - 30, View.MeasureSpec.AT_MOST);
int measureHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
view.measure(measureWidth, measureHeight);
//调用layout方法布局后可以得到view的尺寸
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// Log.e("SVGADynamicEntity", "子布局宽度:" + childrenView.getWidth());
// Log.e("SVGADynamicEntity", "fu布局宽度" + (width / 3 * 2 - 30));
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(bitmap);
bitmapCanvas.drawColor(Color.TRANSPARENT);
view.draw(bitmapCanvas);
return bitmap;
}
/**
* 全站喇叭
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMsgModelEvent(MsgModel model) {
nobleName = model.getUserName();
nobleTtext = model.getTrumpetMsg();
if (!svga_station_horn.isAnimating()) {
svga_station_horn.setLoops(1);
String backPath = "";
if (model.getNobleId() == 5) {
backPath = "icon_livemsgbg_guowang.svga";
icon = R.mipmap.imng_guowang;
} else if (model.getNobleId() == 6) {
backPath = "icon_livemsgbg_huangdi.svga";
icon = R.mipmap.img_huangdi;
} else {
backPath = "icon_livemsgbg_chaohuang.svga";
icon = R.mipmap.img_chaohuang;
}
new SVGAParser(mContext).decodeFromAssets(backPath, new SVGAParser.ParseCompletion() {
@Override
public void onComplete(SVGAVideoEntity videoItem) {
SVGADrawable drawable = new SVGADrawable(videoItem);
svga_station_horn.setImageDrawable(drawable);
svga_station_horn.startAnimation();
}
@Override
public void onError() {
Log.e("errqs", "errl");
}
},null);
}
}
}