content: package com.cy.dialog;
  
  import android.app.Dialog;
  import android.content.Context;
  import android.graphics.drawable.GradientDrawable;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  
  import androidx.activity.ComponentActivity;
  import androidx.annotation.LayoutRes;
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  
  
  /**
   * Created by lenovo on 2017/7/17.
   */
  
  public class BaseDialog extends Dialog {
      private ComponentActivity componentActivity;
  
      public BaseDialog(ComponentActivity componentActivity) {
          this(componentActivity, 0);
      }
  
      public BaseDialog(ComponentActivity componentActivity, int themeResId) {
          super(componentActivity, themeResId);
          this.componentActivity = componentActivity;
  
          requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除对话框的标题
          GradientDrawable gradientDrawable = new GradientDrawable();
          gradientDrawable.setColor(0x00000000);
          getWindow().setBackgroundDrawable(gradientDrawable);//设置对话框边框背景,必须在代码中设置对话框背景，不然对话框背景是黑色的
  
          dimAmount(0.2f);
      }
  
      @Override
      public void show() {
          if (componentActivity.isDestroyed()) return;
          super.show();
      }
  
      public BaseDialog contentView(@LayoutRes int layoutResID) {
          getWindow().setContentView(layoutResID);
          return this;
      }
  
  
      public BaseDialog contentView(@NonNull View view) {
          getWindow().setContentView(view);
          return this;
      }
  
      public BaseDialog contentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
          getWindow().setContentView(view, params);
          return this;
      }
  
      public BaseDialog layoutParams(@Nullable ViewGroup.LayoutParams params) {
          getWindow().setLayout(params.width, params.height);
          return this;
      }
  
  
      /**
       * 点击外面是否能dissmiss
       *
       * @param canceledOnTouchOutside
       * @return
       */
      public BaseDialog canceledOnTouchOutside(boolean canceledOnTouchOutside) {
          setCanceledOnTouchOutside(canceledOnTouchOutside);
          return this;
      }
  
      /**
       * 位置
       *
       * @param gravity
       * @return
       */
      public BaseDialog gravity(int gravity) {
  
          getWindow().setGravity(gravity);
  
          return this;
  
      }
  
      /**
       * 偏移
       *
       * @param x
       * @param y
       * @return
       */
      public BaseDialog offset(int x, int y) {
          WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
          layoutParams.x = x;
          layoutParams.y = y;
  
          return this;
      }
  
      /*
         设置背景阴影,必须setContentView之后调用才生效
          */
      public BaseDialog dimAmount(float dimAmount) {
  
          WindowManager.LayoutParams lp = getWindow().getAttributes();
          lp.dimAmount = dimAmount;
          return this;
      }
  
  
      /*
     动画类型
      */
      public BaseDialog animType(BaseDialog.AnimInType animInType) {
  
  
          switch (animInType.getIntType()) {
              case 0:
                  getWindow().setWindowAnimations(R.style.dialog_zoom);
  
                  break;
              case 1:
                  getWindow().setWindowAnimations(R.style.dialog_anim_left);
  
                  break;
              case 2:
                  getWindow().setWindowAnimations(R.style.dialog_anim_top);
  
                  break;
              case 3:
                  getWindow().setWindowAnimations(R.style.dialog_anim_right);
  
                  break;
              case 4:
                  getWindow().setWindowAnimations(R.style.dialog_anim_bottom);
  
                  break;
          }
          return this;
      }
  
  
      /*
      动画类型
       */
      public enum AnimInType {
          CENTER(0),
          LEFT(1),
          TOP(2),
          RIGHT(3),
          BOTTOM(4);
  
          AnimInType(int n) {
              intType = n;
          }
  
          final int intType;
  
          public int getIntType() {
              return intType;
          }
      }
  }
  