自定义dialog和弹出dialog的动画

##补充

设置 位置必须在setContentView()之前;设置大小必须在setContentView()之后

public class AdressSelectDialog extends Dialog {
    public AdressSelectDialog(Context context,int width,int height,View view,int themeResId){
        super(context,themeResId);
        Window window = getWindow();

        WindowManager.LayoutParams params = window.getAttributes();

        //新坐标起始位置
//        params.x = 0;
//        params.y = 400;

        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
        setContentView(view);
        Window window1 = getWindow();

        WindowManager.LayoutParams params1 = window1.getAttributes();
        params1.width = width;
        params1.height = height;
        window1.setAttributes(params1);



    }

    
}

##自定义dialog

  final Dialog dialog = new Dialog(ChartVisit.this);
                LayoutInflater inflater=getLayoutInflater();
                final View dialogview=inflater.inflate(R.layout.layout_fragment_chart_visit_screen_dialog, null);
                Window dialogWindow = dialog.getWindow();
                dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);//设置dialog的位置
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的默认标题   
 //必须填写上面找到的View ,不然会再找一遍view,导致里面的点击事件不反应              
//dialog.setContentView(R.layout.layout_fragment_chart_visit_screen_dialog);
                dialog.setContentView(dialogview);
                dialog.setCanceledOnTouchOutside(false);//点击其他位置可以取消dialog

                TextView textView= (TextView) dialogview.findViewById(R.id.textview_back);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                LinearLayout linearLayout= (LinearLayout) dialogview.findViewById(R.id.linearlayout);
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MyDialog myDialog=new MyDialog(ChartVisit.this,R.style.NoDialogTitle);
                        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        myDialog.showDialog(R.layout.layout_fragment_chart_visit_screen2,0,0);

//                        Toast.makeText(ChartVisit.this, "我是dialog", Toast.LENGTH_SHORT).show();
//                        Dialog dialog1=new Dialog(ChartVisit.this);
//                        LayoutInflater inflater=getLayoutInflater();
//                        final View dialogview1=inflater.inflate(R.layout.layout_fragment_chart_visit_screen2, null);
//                        Window dialogWindow = dialog1.getWindow();
//                        dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);//设置dialog的位置
//                        /**
//                         * 对dialog的设置必须放在setContenView之前
//                         */
//                        dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的默认标题
//                        dialog1.setCanceledOnTouchOutside(false);//点击其他位置可以取消dialog
//                        dialog1.setContentView(dialogview1);
//                        //添加动画
//                        TranslateAnimation animation=new TranslateAnimation(dialogview.getWidth(),0,0,0);
//                        animation.setDuration(2000);
//                        dialogview1.setAnimation(animation);
//                        animation.start();
//
//                        dialog1.show();
//                        WindowManager windowManager = getWindowManager();
//                        Display display = windowManager.getDefaultDisplay();
//                        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
//                        lp.width = (int)(display.getWidth()); //设置宽度
//                        (lp).height=(int)dialogview.getHeight();//设置高度
//                        dialog1.getWindow().setAttributes(lp);
                    }
                });
                dialog.show();
                /**
                 * 设置dialog的大小必须在show()的后面,不然不起作用
                 */
                WindowManager windowManager = getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = (int)(display.getWidth());
                //(lp).height=(int)dialogview.getHeight();//设置宽度
                dialog.getWindow().setAttributes(lp);

##弹出dialog动画
####MyDialog

public class MyDialog extends Dialog {
    private Window window = null;

    public MyDialog(Context context)
    {
        super(context);
    }
    //这个构造函数可以设置dialog的属性
    public MyDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void showDialog(int layoutResID, int x, int y){
        setContentView(layoutResID);

        windowDeploy(x, y);

        //设置触摸对话框意外的地方取消对话框
        setCanceledOnTouchOutside(true);
        show();
    }

    //设置窗口显示
    public void windowDeploy(int x, int y){
        window = getWindow(); //得到对话框
        window.setGravity(Gravity.BOTTOM|Gravity.CENTER);
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        //window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明
        WindowManager.LayoutParams wl = window.getAttributes();
        //根据x,y坐标设置窗口需要显示的位置
        wl.x = x; //x小于0左移,大于0右移
        wl.y = y; //y小于0上移,大于0下移
        wl.width=window.getWindowManager().getDefaultDisplay().getWidth();//设置dialog的款
//        wl.width= AppApplication.getApplication().getWallpaperDesiredMinimumWidth();
        wl.height=350;//设置dialog的高
//            wl.alpha = 0.6f; //设置透明度
//            wl.gravity = Gravity.BOTTOM; //设置重力
        window.setAttributes(wl);
    }
}

先在res中创建anim在中创建animator



    
        
        
        
        
        
        
        
        
        
    

然后在styles中添加动画



    
    
    //dialog的属性
    
    //dialog的动画
    


自定义dialog和弹出dialog的动画_第1张图片
如果在new Dialog();时,不加R.style…dialog不会全屏即使你在设置dialog的宽的时候为全屏,因为dialog本身就有一定的属性
如图:
自定义dialog和弹出dialog的动画_第2张图片

你可能感兴趣的:(自定义dialog和弹出dialog的动画)