自定义一些常见的Dialog效果

来源:互联网 发布:美国先锋集团 知乎 编辑:程序博客网 时间:2024/05/16 14:58

(感谢简书作者:Alex_Cin的分享

自定义一些常见的Dialog效果,居中显示、顶部显示、仿IOS版淘宝、回弹效果、宽度和高度占屏比等,先看效果。



1、SimpleDialog

package org.alex.dialog.base;import android.content.Context;import android.view.Gravity;import android.view.View;import android.view.Window;import org.alex.alexlibrary.R;import org.alex.dialog.annotation.AnimType;@SuppressWarnings("all")public abstract class SimpleDialog<D extends SimpleDialog> extends BaseDialog<D> implements View.OnClickListener {    public SimpleDialog(Context context) {        super(context,R.style.alex_dialog_base_light_style);    }    public SimpleDialog(Context context, int theme) {        super(context, theme);    }    /**     * 显示对话框,无动画     */    @Override    public void show() {        if (Gravity.BOTTOM == gravity) {            show(AnimType.BOTTOM_2_TOP);        } else if (Gravity.TOP == gravity) {            show(AnimType.TOP_2_BOTTOM);        } else if (Gravity.CENTER == gravity) {            show(animType);        } else {            super.show();        }    }    /**     * 显示对话框,强制转换对话框的动画类型     */    public void show(@AnimType int animType) {        Window window = getWindow();        /*如果根据  AnimType 的类型,强制选择Dialog出现的位置*/        if (AnimType.BOTTOM_2_TOP == animType) {            setGravity(Gravity.BOTTOM);            window.setWindowAnimations(R.style.alex_dialog_anim_bottom2top);        } else if (AnimType.TOP_2_BOTTOM == animType) {            setGravity(Gravity.TOP);            window.setWindowAnimations(R.style.alex_dialog_anim_top2bottom);        } else if (AnimType.CENTER_SCALE == animType) {            setGravity(Gravity.CENTER);            window.setWindowAnimations(R.style.alex_dialog_anim_scale);        } else if (AnimType.CENTER_NORMAL == animType) {            setGravity(Gravity.CENTER);            window.setWindowAnimations(R.style.alex_dialog_anim_alpha);        }        super.show();    }}
2、<span style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 24.2857px; background-color: rgb(245, 245, 245);">Bottom2TopDialog</span>

package org.alex.dialog.base;import android.content.Context;import org.alex.alexlibrary.R;import org.alex.dialog.annotation.AnimType;public abstract class Bottom2TopDialog<D extends Bottom2TopDialog> extends SimpleDialog<D> {    public Bottom2TopDialog(Context context) {        super(context, R.style.alex_dialog_base_light_style);    }    /**     * 显示对话框,无动画     */    @Override    public void show() {        show(AnimType.BOTTOM_2_TOP);    }    /**     * 显示对话框,强制转换对话框的动画类型     *     * @param animType     */    @Override    public void show(@AnimType int animType) {        super.show(animType);    }}
3、TaoBaoDialog
package org.alex.dialog.base;import android.animation.ObjectAnimator;import android.content.Context;import android.graphics.Color;import android.os.Handler;import android.os.Message;import android.view.View;import org.alex.dialog.anim.BaseAnimatorSet;import org.alex.dialog.annotation.AnimType;public abstract class TaoBaoDialog<D extends TaoBaoDialog> extends SimpleDialog<D> {    private BaseAnimatorSet inAnimSet;    private BaseAnimatorSet outAnimSet;    private View rootView;    protected int duration;    protected int backgroundColor;    private View parentView;    public TaoBaoDialog(Context context, View rootView, int theme) {        super(context, theme);        this.rootView = rootView;        initView();    }    protected void initView() {        duration = 300;        backgroundColor = Color.parseColor("#111111");        parentView = (View) rootView.getParent();        parentView.setBackgroundColor(backgroundColor);        inAnimSet = new WindowsInAs();        outAnimSet = new WindowsOutAs();    }    /**     * 显示对话框,强制转换对话框的动画类型     */    @Override    public void show() {        show(AnimType.BOTTOM_2_TOP);    }    /**     * 显示对话框,强制转换对话框的动画类型     *     * @param animType     */    @Override    public void show(@AnimType int animType) {        super.show(AnimType.BOTTOM_2_TOP);        parentView.setBackgroundColor(backgroundColor);        if (rootView != null && inAnimSet != null) {            inAnimSet.duration(duration).playOn(rootView);        }    }    @Override    public void dismiss() {        super.dismiss();        new Handler(){            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                parentView.setBackgroundColor(0);            }        }.sendEmptyMessageDelayed(100, duration);        if (rootView != null && outAnimSet != null) {            outAnimSet.duration(duration).playOn(rootView);        }    }    private final class WindowsInAs extends BaseAnimatorSet {        @Override        public void setAnimation(View view) {            ObjectAnimator rotationX = ObjectAnimator.ofFloat(view, "rotationX", 10, 0f).setDuration(150);            rotationX.setStartDelay(200);            animatorSet.playTogether(                    ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 0.8f).setDuration(350),                    ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 0.8f).setDuration(350),                    ObjectAnimator.ofFloat(view, "rotationX", 0f, 10).setDuration(200),                    rotationX,                    ObjectAnimator.ofFloat(view, "translationY", 0, -0.1f * displayMetrics.heightPixels).setDuration(350)            );        }    }    private final class WindowsOutAs extends BaseAnimatorSet {        @Override        public void setAnimation(View view) {            ObjectAnimator rotationX = ObjectAnimator.ofFloat(view, "rotationX", 10, 0f).setDuration(150);            rotationX.setStartDelay(200);            animatorSet.playTogether(                    ObjectAnimator.ofFloat(view, "scaleX", 0.8f, 1.0f).setDuration(350),                    ObjectAnimator.ofFloat(view, "scaleY", 0.8f, 1.0f).setDuration(350),                    ObjectAnimator.ofFloat(view, "rotationX", 0f, 10).setDuration(200),                    rotationX,                    ObjectAnimator.ofFloat(view, "translationY", -0.1f * displayMetrics.heightPixels, 0).setDuration(350)            );        }    }}/
4、<span style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 24.2857px; background-color: rgb(245, 245, 245);">点击事件 怎么写?</span>
package com.alex.mvpapp.dialog;import android.content.Context;import android.view.View;import com.alex.mvpapp.R;import org.alex.dialog.annotation.ClickPosition;import org.alex.dialog.base.Top2BottomDialog;public class NoticeDialog extends Top2BottomDialog<NoticeDialog> {    public NoticeDialog(Context context) {        super(context);    }    @Override    public int getLayoutRes() {        return R.layout.dialog_notice;    }    @Override    public void onCreateDialog() {        setOnCilckListener(R.id.tv_submit,R.id.tv_cancel);    }    @Override    public void onClick(View v) {        if(R.id.tv_submit == v.getId()){           onDialogClickListener(ClickPosition.SUBMIT);        }else  if(R.id.tv_cancel == v.getId()){            onDialogClickListener(ClickPosition.CANCEL);        }    }}
5、点击事件 怎么响应?
package com.alex.mvpapp.ui.testdialog;public class DialogActivity extends BaseActivity {    /**     * 执行在 onCreateView 中     * 通过 findView 初始主视图化控件     * 初始化所有基础数据,     */    @Override    public void onCreateData() {        super.onCreateData();        MyOnDialogClickListener onDialogClickListener = new MyOnDialogClickListener();        loadingDialog = new LoadingDialog(this);//.setOnKeyListener(SimpleOnKeyListener.dismissNotKillActivity(this));        oneButtonDialog = new OneButtonDialog(this).setOnDialogClickListener(onDialogClickListener);        selectedProductDialog = new SelectedProductDialog(this, findViewById(Window.ID_ANDROID_CONTENT)).setOnDialogClickListener(onDialogClickListener);        noticeDialog = new NoticeDialog(this).tag("noticeDialog").setOnDialogClickListener(onDialogClickListener);        setOnClickListener(R.id.tv_loadingDialog, R.id.tv_oneButtonDialog, R.id.tv_taobao, R.id.tv_notice);    }    private final class MyOnDialogClickListener implements OnDialogClickListener<SimpleDialog> {        @Override        public void onDialogClick(SimpleDialog dialog, @ClickPosition String clickPosition) {            LogUtil.e("tag = " + dialog.tag + " clickPosition =" + clickPosition);            dialog.dismiss();        }    }}

博客地址 http://www.jianshu.com/p/d893ba8608ae

源码 https://github.com/Alex-Cin/Dialog

demo https://github.com/Alex-Cin/DialogApp




0 0