DanmuUtil.java
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.cnlive.goldenline.util;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.danmu.DanmuControl;
import com.cnlive.goldenline.model.DamakuViewinfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import master.flame.danmaku.ui.widget.DanmakuView;
/**
* Created by Lynn on 4/25/2017.
*/
public class DanmuUtil implements RadioGroup.OnCheckedChangeListener {
// @Nullable
// @BindView(R.id.danmuview)
public DanmakuView danmuview;
private EditText danmuEdit;
public PopupWindow danmuPopupWindow;
private LinearLayout danmuController;
private RadioGroup danmuPos;
private RadioGroup danmuColorLayout;
public boolean isPressed = false;
private Context mContext;
private DamakuViewinfo danmu;
public DanmuControl mDanmuControl;
public int color = 0;
public int postiton = 0;
private DanmuVisibleListener l;
public DanmuUtil(Context context, DanmakuView danmuview) {
this.mContext = context;
mDanmuControl = new DanmuControl(mContext);
danmu = new DamakuViewinfo();
this.danmuview = danmuview;
initDanmu();
}
public void setDanmuListener(DanmuVisibleListener listener){
l = listener;
}
private void initDanmu() {
mDanmuControl.setDanmakuView(danmuview);
}
public void showDanMuInput(View view) {
if(l != null)l.onDanmuShow();
View contentView = LayoutInflater.from(mContext).inflate(R.layout.danmu_controller_layout, new RelativeLayout(mContext), false);
danmuController = (LinearLayout) contentView.findViewById(R.id.danmu_controll);
danmuPos = (RadioGroup) contentView.findViewById(R.id.danmu_pos);
danmuPos.setOnCheckedChangeListener(this);
danmuColorLayout = (RadioGroup) contentView.findViewById(R.id.danmu_color);
danmuColorLayout.setOnCheckedChangeListener(this);
contentView.findViewById(R.id.danmu_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
danmuEdit.setText("");
danmuPopupWindow.dismiss();
if(l != null)l.onDanmuHide();
}
});
contentView.findViewById(R.id.danmu_setting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPressed) {
danmuController.setVisibility(View.GONE);
// showSoftInput();
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
danmuEdit.requestFocus();
imm.showSoftInput(danmuEdit, 0);
}
isPressed = false;
} else {
// hideSoftInput();
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
inputMethodManager.hideSoftInputFromWindow(danmuEdit.getWindowToken(), 0);
}
danmuController.setVisibility(View.VISIBLE);
isPressed = true;
}
}
});
danmuEdit = (EditText) contentView.findViewById(R.id.input_danmu);
danmuEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isPressed = false;
danmuController.setVisibility(View.GONE);
}
});
danmuEdit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
inputMethodManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
//TODO 发送弹幕
send();
danmuEdit.setText("");
danmuPopupWindow.dismiss();
if(l != null)l.onDanmuHide();
return true;
}
return false;
}
});
if (danmuPopupWindow != null) danmuPopupWindow.dismiss();
danmuPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
danmuPopupWindow.setTouchable(true);
danmuPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
danmuPopupWindow.dismiss();
if(l != null)l.onDanmuHide();
}
return false;
}
});
// 设置以下两个属性后 点击popupWindow之外的地方 就会让其消失
// 设置popupWindow 点击popupWindow外的地方 生效
danmuPopupWindow.setOutsideTouchable(true);
// 设置popupWindow 背景
danmuPopupWindow.setBackgroundDrawable(new BitmapDrawable());
danmuPopupWindow.setFocusable(true); //设置获取焦点
danmuPopupWindow.setAnimationStyle(R.style.animation);
danmuPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
danmuPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
danmuPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
danmuPopupWindow.update();
danmuEdit.requestFocus();
danmuPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
danmuPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
danmuEdit.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
InputMethodManager imm = (InputMethodManager) mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(500, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 发送
*/
public void send() {
setData();//设置数据
// danmuEdit.setText("");//发送之后输入框设置为空
// danmuInput.setVisibility(View.GONE);
// danmuLayout.setVisibility(View.GONE);
// danmuControll.setVisibility(View.GONE);
//设置弹幕默认颜色,位置
danmu.setTextColor(0);
color = 0;
danmu.setVerticalPos(0);
postiton = 0;
}
/**
* 设置弹幕数据,文本内容
*/
public void setData() {
String text = danmuEdit.getText().toString();
List<DamakuViewinfo> danmus = new ArrayList<>();
DamakuViewinfo danmu = new DamakuViewinfo(color, text, postiton);
danmu.setText(text);
danmus.add(danmu);
Collections.shuffle(danmus);
//发送之后清除设置的位置,字体颜色
danmuPos.clearCheck();
danmuColorLayout.clearCheck();
if (TextUtils.isEmpty(danmuEdit.getText())) {
ToastUtil.showToast("请输入内容");
} else {
if (NetworkUtils.isNetworkAvaliable(mContext) == true) {
mDanmuControl.addDanmuList(danmus);
} else {
ToastUtil.showToast("网络跑路了,请连接网络");
}
}
}
/**
* 设置弹幕显示位置,字体颜色
*/
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (group.getCheckedRadioButtonId()) {
case R.id.danmu_top://置顶
postiton = 1;
danmu.setVerticalPos(1);
break;
case R.id.danmu_center://滚动
postiton = 2;
danmu.setVerticalPos(2);
break;
case R.id.danmu_bottom://置底
postiton = 3;
danmu.setVerticalPos(3);
break;
//字体颜色设置
case R.id.danmu_color_white:
color = 1;
danmu.setTextColor(1);
break;
case R.id.danmu_color_orange:
color = 2;
danmu.setTextColor(2);
break;
case R.id.danmu_color_lightyellow:
color = 3;
danmu.setTextColor(3);
break;
case R.id.danmu_color_yellow:
color = 4;
danmu.setTextColor(4);
break;
case R.id.danmu_color_green:
color = 5;
danmu.setTextColor(5);
break;
case R.id.danmu_color_skybulue:
color = 6;
danmu.setTextColor(6);
break;
case R.id.danmu_color_bule:
color = 7;
danmu.setTextColor(7);
break;
case R.id.danmu_color_purple:
color = 8;
danmu.setTextColor(8);
break;
}
}
public interface DanmuVisibleListener {
void onDanmuShow();
void onDanmuHide();
}
/**
* 软键盘隐藏
*/
// public void hideSoftInput() {
// //1.得到InputMethodManager对象
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// if (imm != null) {
// //2.调用hideSoftInputFromWindow方法隐藏软键盘
// imm.hideSoftInputFromWindow(inputDanmu.getWindowToken(), 0); //强制制隐藏键盘
// }
// }
/**
* 软键盘显示
*/
// public void showSoftInput() {
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// if (imm != null) {
// inputDanmu.requestFocus();
// imm.showSoftInput(inputDanmu, 0);
// }
// }
}