自定义Dialog实现弹幕发送框界面

做弹幕开发的时候有时候需要一个弹幕输入的界面是必不可少的;下面我们通过一个自定义Dialog快速实现这一功能,效果就像下面这样;



这种界面自己动手在xml布局里面很难搞,几乎弄不了,特别很难入手,一时也很懵逼,群里看到有人说用Dilaog来实现,就可以;果然使用Dialog很快就实现了;

思路是这样:

1,首先自定义一个Dialog ,Dialog里面包含EditText和Button

2,,实现里面的逻辑 点击发送的时候界面自动弹出且不全屏,点击Dialog会消失;

封装代码如下所示:

/**
 * Created by shion on 2017/9/26.
 * 弹幕发射的Dialog
 */
public class DanMaKuSendDialog extends Dialog implements
        DialogInterface.OnShowListener,
        DialogInterface.OnDismissListener,
        DialogInterface.OnCancelListener {
    private static final String TAG = DanMaKuSendDialog.class.getSimpleName();

    /**
     * @param context   上下文
     * @param theme     主题样式
     * @param width     Dilaog的宽度
     * @param height    Dialog的高度
     * @param resLayout Dialog 的布局layout资源id
     * @param gravity   Dialog在Window中的方向位置
     * @param animation Dialog展示和隐藏时候的动画效果
     */
    private DanMaKuSendDialog(Context context, int theme, int width, int height, int resLayout, int gravity, int animation) {
        super(context, theme);
        Window window = getWindow();
        if (window != null) {
            //设置布局
            setContentView(resLayout);
            //设置Dialog的其他属性
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = width;//赋值宽度
            lp.height = height;//赋值高度
            lp.gravity = gravity;//赋值方位
            window.setAttributes(lp);
            //设置Dialog的动画效果
            window.setWindowAnimations(animation);
        }
    }

    private InputMethodManager imm;
    private EditText mDialogEditText;
    private TextView mDialogTextView;

    public DanMaKuSendDialog(Context context) {
        this(context, R.style.dialog_style_send_danmu, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, R.layout.dialog_edit_view, Gravity.BOTTOM, 0);
        //初始化软键盘服务管理类
        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //设置点击Dialog外部会消失 ,下面两个设置效果会回调onCancel --onDismiss方法,我们在里面关闭一下软键盘
        setCanceledOnTouchOutside(true);
        //点返回键Diloag会消失
        setCancelable(true);

        //初始化布局UI
        mDialogTextView = (TextView) findViewById(R.id.tv_send);
        mDialogEditText = (EditText) findViewById(et_keywored);
        // 使软键盘不全屏显示,只占用一部分屏幕,对应xml标签 android:imeOptions="flagNoExtractUi
        mDialogEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

        //初始化Dialog的监听
        setOnShowListener(this);
        setOnDismissListener(this);
        setOnCancelListener(this);


    }

    //Dialog出现的时候监听 ,Dialog展示的时候就弹出软键盘
    @Override
    public void onShow(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onShow-----------");
        if (mDialogEditText != null) {
            mDialogEditText.post(new Runnable() {
                @Override
                public void run() {
                    if (imm != null) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
                    }
                    try {
                        Thread.sleep(60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    //Dialog消失的时候监听
    @Override
    public void onDismiss(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onDismiss-----------");
    }

    //取消Dialog的监听 取消的时候关闭软键盘
    @Override
    public void onCancel(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onCancel-----------");
        if (mDialogEditText != null) {
            mDialogEditText.post(new Runnable() {
                @Override
                public void run() {
                    if (imm != null) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    try {
                        Thread.sleep(60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

Dialog的主体style如下:

    <!--自定义Dialog的主体样式-->
    <style name="dialog_style_send_danmu" parent="@android:style/Theme.Dialog">
        <!--对话框无标题-->
        <item name="android:windowNoTitle">true</item>
        <!--对话框边框-->
        <item name="android:windowFrame">@null</item>
        <!--对话框是否有遮盖层-->
        <item name="android:windowContentOverlay">@null</item>
        <!--对话框是否透明-->
        <item name="android:windowIsTranslucent">true</item>
        <!--对话框是否浮动,不浮动的话,就会固定在某个位置,对话框布局里有EditText时候也不会动-->
        <item name="android:windowIsFloating">true</item>
        <!--Dialog是否让对话框背景变暗-->
        <item name="android:backgroundDimEnabled">false</item>
        <!--对话框的背景-->
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

当然自己也可以自定义Dialog的动画效果style:

    <!--定义Diloag的动画-->
    <style name="anim_style_danmu">
        <item name="android:windowEnterAnimation">@anim/dialog_danmu_in</item>
        <item name="android:windowExitAnimation">@anim/dialog_danmu_out</item>
    </style>

以上就是主要代码,不做解释了注释也很清楚,完整代码下载:

Android自定义Dialog--DanmakuFllame弹幕发送框界面的实现


    




猜你喜欢

转载自blog.csdn.net/eyishion/article/details/78106394