DanmuUtil.java 10.8 KB
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);
//        }
//    }
}