Android Dialog 封装及常用动画

项目开发中我们经常用到 dialog 来弹窗,为了不重复写,封装一个简单的基类 dialog。

直接上代码:

/**
 * @desciption: Dialog 基类
 */
public class BaseDialog extends AppCompatDialog {

    private static BaseDialog mBaseDialog;
    private Builder mBuilder;

    private BaseDialog(Builder builder) {
        this(builder, R.style.BaseDialogStyle);
    }

    private BaseDialog(Builder builder, int theme) {
        super(builder.mContext, theme);
        mBuilder = builder;
    }

    /**
     * 取消Dialog
     */
    public void dismissDialog() {
        mBaseDialog.dismiss();
        mBaseDialog = null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mBuilder.mView);
        setCanceledOnTouchOutside(mBuilder.cancelTouchout);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            if (mBuilder.width > 0) {
                params.width = mBuilder.width;
            }
            if (mBuilder.height > 0) {
                params.height = mBuilder.height;
            }
            params.gravity = mBuilder.gravity;
            window.setAttributes(params);
        }
    }

    @Override
    public void show() {
        if (!mBaseDialog.isShowing()) {
            super.show();
        }
    }

    public static final class Builder {
        private Context mContext;
        private boolean cancelTouchout = false;
        private int width, height;
        private int gravity;
        private View mView;
        private int resStyle = -1;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setView(int layoutId) {
            mView = LayoutInflater.from(mContext).inflate(layoutId, null);
            return this;
        }

        public Builder setView(View view) {
            mView = view;
            return this;
        }

        public Builder setGravity(int gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder setWidthdp(int val) {
            width = UiUtils.getInstance().dip2px(val);
            return this;
        }

        public Builder setHeightdp(int val) {
            height = UiUtils.getInstance().dip2px(val);
            return this;
        }

        public Builder setWidthDimenRes(int dimenRes) {
            width = UiUtils.getInstance().getDimens(dimenRes);
            return this;
        }

        public Builder setHeightDimenRes(int dimenRes) {
            height = UiUtils.getInstance().getDimens(dimenRes);
            return this;
        }

        public Builder setStyle(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder setCancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnClick(int viewRes, View.OnClickListener listener) {
            mView.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public  T findViewById(int id) {
            return mView.findViewById(id);
        }

        /**
         * 防止Dialog show两次
         */
        public BaseDialog build() {
            if (mBaseDialog == null) {
                if (resStyle != -1) {
                    mBaseDialog = new BaseDialog(this, resStyle);
                } else {
                    mBaseDialog = new BaseDialog(this);
                }
            }
            return mBaseDialog;
        }
    }
}

工具类 UiUtils 常用工具类

使用方式:

/**
 * @desciption:
 */
public class DialogUtils {
    private static BaseDialog dialog;

    public static void showTipsSingleDialog(Context context, String content, final View.OnClickListener listener) {
        BaseDialog.Builder builder = new BaseDialog.Builder(context);
        dialog = builder
                .setView(R.layout.dialog_common_tips)
                .addViewOnClick(R.id.btn_positive, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismissDialog();
                        listener.onClick(v);
                    }
                })
                .build();
        builder.findViewById(R.id.btn_negative).setVisibility(View.GONE);
        AppCompatTextView txtContent = builder.findViewById(R.id.txt_content);
        txtContent.setText(content);
        dialog.show();
    }

    public static void showTipsTwoDialog(Context context, String content, String negative, String positive, final View.OnClickListener listener) {
        BaseDialog.Builder builder = new BaseDialog.Builder(context);
        dialog = builder
                .setView(R.layout.dialog_common_tips)
                .addViewOnClick(R.id.btn_positive, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismissDialog();
                        listener.onClick(v);
                    }
                })
                .addViewOnClick(R.id.btn_negative, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismissDialog();

                    }
                })
                .build();
        AppCompatTextView txtContent = builder.findViewById(R.id.txt_content);
        AppCompatTextView btnNegative = builder.findViewById(R.id.btn_negative);
        AppCompatTextView btnPositive = builder.findViewById(R.id.btn_positive);
        txtContent.setText(content);
        btnNegative.setText(negative);
        btnPositive.setText(positive);
        dialog.show();
    }
}

下面是dialog 常用动画:

在styles.xml文件中写入如下style


    
    

    
    

    
    

    
    

    
    

    
    

缩放动画

//push_scale_in.xml



    
    
    

    


//push_scale_out.xml


    

    

ios 动画

//push_ios_in.xml


    
    


//push_ios_out.xml


    
    

顶部弹出动画

//push_top_in.xml


    


//push_top_out.xml


    

底部弹出动画

//push_bottom_in.xml


    


//push_bottom_out.xml


    

左边弹出动画

//push_left_in.xml


    
    
    
    


//push_left_out.xml


    
    
    
    

右边弹出动画

//push_right_in.xml


    
    
    
    


//push_right_out.xml


    
    
    
    

你可能感兴趣的:(Android Dialog 封装及常用动画)