CustomDialog.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. package com.kongzue.dialogx.dialogs;
  2. import android.animation.ValueAnimator;
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.ViewTreeObserver;
  9. import android.view.animation.Animation;
  10. import android.view.animation.AnimationUtils;
  11. import android.view.animation.DecelerateInterpolator;
  12. import android.widget.RelativeLayout;
  13. import androidx.annotation.ColorInt;
  14. import com.kongzue.dialogx.DialogX;
  15. import com.kongzue.dialogx.R;
  16. import com.kongzue.dialogx.interfaces.BaseDialog;
  17. import com.kongzue.dialogx.interfaces.DialogConvertViewInterface;
  18. import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
  19. import com.kongzue.dialogx.interfaces.DialogXStyle;
  20. import com.kongzue.dialogx.interfaces.OnBackPressedListener;
  21. import com.kongzue.dialogx.interfaces.OnBackgroundMaskClickListener;
  22. import com.kongzue.dialogx.interfaces.OnBindView;
  23. import com.kongzue.dialogx.util.views.DialogXBaseRelativeLayout;
  24. import com.kongzue.dialogx.util.views.MaxRelativeLayout;
  25. import java.lang.ref.WeakReference;
  26. /**
  27. * @author: Kongzue
  28. * @github: https://github.com/kongzue/
  29. * @homepage: http://kongzue.com/
  30. * @mail: myzcxhh@live.cn
  31. * @createTime: 2020/10/20 11:59
  32. */
  33. public class CustomDialog extends BaseDialog {
  34. public static int overrideEnterDuration = -1;
  35. public static int overrideExitDuration = -1;
  36. public static int overrideEnterAnimRes = 0;
  37. public static int overrideExitAnimRes = 0;
  38. public static int overrideMaskEnterAnimRes = R.anim.anim_dialogx_default_alpha_enter;
  39. public static int overrideMaskExitAnimRes = R.anim.anim_dialogx_default_exit;
  40. public static BOOLEAN overrideCancelable;
  41. protected OnBindView<CustomDialog> onBindView;
  42. protected DialogLifecycleCallback<CustomDialog> dialogLifecycleCallback;
  43. protected CustomDialog me = this;
  44. protected DialogImpl dialogImpl;
  45. protected int enterAnimResId = R.anim.anim_dialogx_default_enter;
  46. protected int exitAnimResId = R.anim.anim_dialogx_default_exit;
  47. protected ALIGN align = ALIGN.CENTER;
  48. protected boolean autoUnsafePlacePadding = true;
  49. private View dialogView;
  50. protected int maskColor = Color.TRANSPARENT;
  51. protected BOOLEAN privateCancelable;
  52. protected boolean bkgInterceptTouch = true;
  53. protected OnBackgroundMaskClickListener<CustomDialog> onBackgroundMaskClickListener;
  54. protected View baseView;
  55. protected int alignViewGravity = -1; //指定菜单相对 baseView 的位置
  56. protected int width = -1; //指定菜单宽度
  57. protected int height = -1; //指定菜单高度
  58. protected int[] baseViewLoc;
  59. protected int[] marginRelativeBaseView = new int[4];
  60. public enum ALIGN {
  61. CENTER,
  62. TOP,
  63. BOTTOM,
  64. LEFT,
  65. RIGHT
  66. }
  67. protected CustomDialog() {
  68. super();
  69. }
  70. public static CustomDialog build() {
  71. return new CustomDialog();
  72. }
  73. public static CustomDialog build(OnBindView<CustomDialog> onBindView) {
  74. return new CustomDialog().setCustomView(onBindView);
  75. }
  76. public CustomDialog(OnBindView<CustomDialog> onBindView) {
  77. this.onBindView = onBindView;
  78. }
  79. public static CustomDialog show(OnBindView<CustomDialog> onBindView) {
  80. CustomDialog customDialog = new CustomDialog(onBindView);
  81. customDialog.show();
  82. return customDialog;
  83. }
  84. public static CustomDialog show(OnBindView<CustomDialog> onBindView, ALIGN align) {
  85. CustomDialog customDialog = new CustomDialog(onBindView);
  86. customDialog.align = align;
  87. customDialog.show();
  88. return customDialog;
  89. }
  90. public void show() {
  91. super.beforeShow();
  92. if (getDialogView() == null) {
  93. dialogView = createView(R.layout.layout_dialogx_custom);
  94. dialogImpl = new DialogImpl(dialogView);
  95. if (dialogView != null) dialogView.setTag(me);
  96. }
  97. show(dialogView);
  98. }
  99. public void show(Activity activity) {
  100. super.beforeShow();
  101. if (getDialogView() == null) {
  102. dialogView = createView(R.layout.layout_dialogx_custom);
  103. dialogImpl = new DialogImpl(dialogView);
  104. if (dialogView != null) dialogView.setTag(me);
  105. }
  106. show(activity, dialogView);
  107. }
  108. public class DialogImpl implements DialogConvertViewInterface {
  109. public DialogXBaseRelativeLayout boxRoot;
  110. public MaxRelativeLayout boxCustom;
  111. public DialogImpl(View convertView) {
  112. if (convertView == null) return;
  113. boxRoot = convertView.findViewById(R.id.box_root);
  114. boxCustom = convertView.findViewById(R.id.box_custom);
  115. init();
  116. dialogImpl = this;
  117. refreshView();
  118. }
  119. @Override
  120. public void init() {
  121. boxRoot.setParentDialog(me);
  122. boxRoot.setOnLifecycleCallBack(new DialogXBaseRelativeLayout.OnLifecycleCallBack() {
  123. @Override
  124. public void onShow() {
  125. isShow = true;
  126. getDialogLifecycleCallback().onShow(me);
  127. boxCustom.setVisibility(View.GONE);
  128. }
  129. @Override
  130. public void onDismiss() {
  131. isShow = false;
  132. getDialogLifecycleCallback().onDismiss(me);
  133. dialogImpl = null;
  134. dialogLifecycleCallback = null;
  135. System.gc();
  136. }
  137. });
  138. boxRoot.setOnBackPressedListener(new OnBackPressedListener() {
  139. @Override
  140. public boolean onBackPressed() {
  141. if (onBackPressedListener != null) {
  142. if (onBackPressedListener.onBackPressed()) {
  143. dismiss();
  144. }
  145. return false;
  146. }
  147. if (isCancelable()) {
  148. dismiss();
  149. }
  150. return false;
  151. }
  152. });
  153. boxRoot.post(new Runnable() {
  154. @Override
  155. public void run() {
  156. Animation enterAnim;
  157. if (enterAnimResId == R.anim.anim_dialogx_default_enter &&
  158. exitAnimResId == R.anim.anim_dialogx_default_exit &&
  159. baseView == null) {
  160. switch (align) {
  161. case TOP:
  162. enterAnimResId = R.anim.anim_dialogx_top_enter;
  163. exitAnimResId = R.anim.anim_dialogx_top_exit;
  164. break;
  165. case BOTTOM:
  166. enterAnimResId = R.anim.anim_dialogx_bottom_enter;
  167. exitAnimResId = R.anim.anim_dialogx_bottom_exit;
  168. break;
  169. case LEFT:
  170. enterAnimResId = R.anim.anim_dialogx_left_enter;
  171. exitAnimResId = R.anim.anim_dialogx_left_exit;
  172. break;
  173. case RIGHT:
  174. enterAnimResId = R.anim.anim_dialogx_right_enter;
  175. exitAnimResId = R.anim.anim_dialogx_right_exit;
  176. break;
  177. }
  178. enterAnim = AnimationUtils.loadAnimation(getTopActivity(), enterAnimResId);
  179. enterAnim.setInterpolator(new DecelerateInterpolator(2f));
  180. } else {
  181. int enterAnimResIdTemp = R.anim.anim_dialogx_default_enter;
  182. if (overrideEnterAnimRes != 0) {
  183. enterAnimResIdTemp = overrideEnterAnimRes;
  184. }
  185. if (enterAnimResId != 0) {
  186. enterAnimResIdTemp = enterAnimResId;
  187. }
  188. enterAnim = AnimationUtils.loadAnimation(getTopActivity(), enterAnimResIdTemp);
  189. }
  190. long enterAnimDurationTemp = enterAnim.getDuration();
  191. if (overrideEnterDuration >= 0) {
  192. enterAnimDurationTemp = overrideEnterDuration;
  193. }
  194. if (enterAnimDuration >= 0) {
  195. enterAnimDurationTemp = enterAnimDuration;
  196. }
  197. enterAnim.setDuration(enterAnimDurationTemp);
  198. boxCustom.setVisibility(View.VISIBLE);
  199. boxCustom.startAnimation(enterAnim);
  200. boxRoot.setBackgroundColor(maskColor);
  201. if (overrideMaskEnterAnimRes != 0) {
  202. Animation maskEnterAnim = AnimationUtils.loadAnimation(getTopActivity(), overrideMaskEnterAnimRes);
  203. maskEnterAnim.setInterpolator(new DecelerateInterpolator(2f));
  204. maskEnterAnim.setDuration(enterAnimDurationTemp);
  205. boxRoot.startAnimation(maskEnterAnim);
  206. }
  207. ValueAnimator bkgAlpha = ValueAnimator.ofFloat(0f, 1f);
  208. bkgAlpha.setDuration(enterAnimDurationTemp);
  209. bkgAlpha.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  210. @Override
  211. public void onAnimationUpdate(ValueAnimator animation) {
  212. float value = (float) animation.getAnimatedValue();
  213. boxRoot.setBkgAlpha(value);
  214. }
  215. });
  216. bkgAlpha.start();
  217. }
  218. });
  219. }
  220. boolean initSetCustomViewLayoutListener = false;
  221. @Override
  222. public void refreshView() {
  223. if (boxRoot == null || getTopActivity() == null) {
  224. return;
  225. }
  226. if (baseView != null) {
  227. if (!initSetCustomViewLayoutListener) {
  228. RelativeLayout.LayoutParams rlp;
  229. rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  230. boxCustom.setLayoutParams(rlp);
  231. Runnable onLayoutChangeRunnable = new Runnable() {
  232. @Override
  233. public void run() {
  234. int baseViewLeft = baseViewLoc[0];
  235. int baseViewTop = baseViewLoc[1];
  236. int calX = 0, calY = 0;
  237. if (alignViewGravity != -1) {
  238. if (isAlignBaseViewGravity(Gravity.CENTER_VERTICAL)) {
  239. calY = (baseViewTop + baseView.getMeasuredHeight() / 2 - boxCustom.getHeight() / 2);
  240. }
  241. if (isAlignBaseViewGravity(Gravity.CENTER_HORIZONTAL)) {
  242. calX = (baseViewLeft + baseView.getMeasuredWidth() / 2 - boxCustom.getWidth() / 2);
  243. }
  244. if (isAlignBaseViewGravity(Gravity.CENTER)) {
  245. calX = (baseViewLeft + baseView.getMeasuredWidth() / 2 - boxCustom.getWidth() / 2);
  246. calY = (baseViewTop + baseView.getMeasuredHeight() / 2 - boxCustom.getHeight() / 2);
  247. }
  248. if (isAlignBaseViewGravity(Gravity.TOP)) {
  249. calY = baseViewTop - boxCustom.getHeight() - marginRelativeBaseView[3];
  250. }
  251. if (isAlignBaseViewGravity(Gravity.LEFT)) {
  252. calX = baseViewLeft - boxCustom.getWidth() - marginRelativeBaseView[2];
  253. }
  254. if (isAlignBaseViewGravity(Gravity.RIGHT)) {
  255. calX = baseViewLeft + baseView.getWidth() + marginRelativeBaseView[0];
  256. }
  257. if (isAlignBaseViewGravity(Gravity.BOTTOM)) {
  258. calY = baseViewTop + baseView.getHeight() + marginRelativeBaseView[1];
  259. }
  260. if (calX != 0) boxCustom.setX(calX);
  261. if (calY != 0) boxCustom.setY(calY);
  262. }
  263. }
  264. };
  265. boxCustom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  266. @Override
  267. public void onGlobalLayout() {
  268. onLayoutChangeRunnable.run();
  269. }
  270. });
  271. initSetCustomViewLayoutListener = true;
  272. }
  273. } else {
  274. RelativeLayout.LayoutParams rlp;
  275. rlp = ((RelativeLayout.LayoutParams) boxCustom.getLayoutParams());
  276. if (rlp == null) {
  277. rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  278. }
  279. switch (align) {
  280. case TOP:
  281. rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
  282. rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  283. break;
  284. case BOTTOM:
  285. rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
  286. rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  287. break;
  288. case CENTER:
  289. rlp.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
  290. rlp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  291. rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
  292. break;
  293. case LEFT:
  294. rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
  295. rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  296. rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
  297. break;
  298. case RIGHT:
  299. rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
  300. rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  301. rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  302. break;
  303. }
  304. boxCustom.setLayoutParams(rlp);
  305. }
  306. boxRoot.setAutoUnsafePlacePadding(autoUnsafePlacePadding);
  307. if (bkgInterceptTouch) {
  308. if (isCancelable()) {
  309. boxRoot.setOnClickListener(new View.OnClickListener() {
  310. @Override
  311. public void onClick(View v) {
  312. if (onBackgroundMaskClickListener == null || !onBackgroundMaskClickListener.onClick(me, v)) {
  313. doDismiss(v);
  314. }
  315. }
  316. });
  317. } else {
  318. boxRoot.setOnClickListener(null);
  319. }
  320. } else {
  321. boxRoot.setClickable(false);
  322. }
  323. if (onBindView != null && onBindView.getCustomView() != null) {
  324. onBindView.bindParent(boxCustom, me);
  325. }
  326. if (width != -1) {
  327. boxCustom.setMaxWidth(width);
  328. boxCustom.setMinimumWidth(width);
  329. }
  330. if (height != -1) {
  331. boxCustom.setMaxHeight(height);
  332. boxCustom.setMinimumHeight(height);
  333. }
  334. }
  335. @Override
  336. public void doDismiss(View v) {
  337. if (v != null) v.setEnabled(false);
  338. if (!dismissAnimFlag) {
  339. dismissAnimFlag = true;
  340. boxCustom.post(new Runnable() {
  341. @Override
  342. public void run() {
  343. int exitAnimResIdTemp = R.anim.anim_dialogx_default_exit;
  344. if (overrideExitAnimRes != 0) {
  345. exitAnimResIdTemp = overrideExitAnimRes;
  346. }
  347. if (exitAnimResId != 0) {
  348. exitAnimResIdTemp = exitAnimResId;
  349. }
  350. Animation exitAnim = AnimationUtils.loadAnimation(getTopActivity() == null ? boxCustom.getContext() : getTopActivity(), exitAnimResIdTemp);
  351. long exitAnimDurationTemp = exitAnim.getDuration();
  352. if (overrideExitDuration >= 0) {
  353. exitAnimDurationTemp = overrideExitDuration;
  354. }
  355. if (exitAnimDuration >= 0) {
  356. exitAnimDurationTemp = exitAnimDuration;
  357. }
  358. exitAnim.setDuration(exitAnimDurationTemp);
  359. exitAnim.setAnimationListener(new Animation.AnimationListener() {
  360. @Override
  361. public void onAnimationStart(Animation animation) {
  362. }
  363. @Override
  364. public void onAnimationEnd(Animation animation) {
  365. dismiss(dialogView);
  366. }
  367. @Override
  368. public void onAnimationRepeat(Animation animation) {
  369. }
  370. });
  371. boxCustom.startAnimation(exitAnim);
  372. if (overrideMaskExitAnimRes != 0) {
  373. Animation maskExitAnim = AnimationUtils.loadAnimation(getTopActivity(), overrideMaskExitAnimRes);
  374. maskExitAnim.setDuration(exitAnimDurationTemp);
  375. maskExitAnim.setInterpolator(new DecelerateInterpolator(2f));
  376. boxRoot.startAnimation(maskExitAnim);
  377. }
  378. ValueAnimator bkgAlpha = ValueAnimator.ofFloat(1f, 0f);
  379. bkgAlpha.setDuration(exitAnimDurationTemp);
  380. bkgAlpha.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  381. @Override
  382. public void onAnimationUpdate(ValueAnimator animation) {
  383. if (boxRoot != null) {
  384. float value = (float) animation.getAnimatedValue();
  385. boxRoot.setBkgAlpha(value);
  386. if (value == 0) boxRoot.setVisibility(View.GONE);
  387. }
  388. }
  389. });
  390. bkgAlpha.start();
  391. }
  392. });
  393. }
  394. }
  395. }
  396. @Override
  397. public String dialogKey() {
  398. return getClass().getSimpleName() + "(" + Integer.toHexString(hashCode()) + ")";
  399. }
  400. public void refreshUI() {
  401. if (getDialogImpl() == null) return;
  402. runOnMain(new Runnable() {
  403. @Override
  404. public void run() {
  405. if (dialogImpl != null) dialogImpl.refreshView();
  406. }
  407. });
  408. }
  409. public void dismiss() {
  410. runOnMain(new Runnable() {
  411. @Override
  412. public void run() {
  413. if (dialogImpl == null) return;
  414. dialogImpl.doDismiss(null);
  415. }
  416. });
  417. }
  418. public DialogLifecycleCallback<CustomDialog> getDialogLifecycleCallback() {
  419. return dialogLifecycleCallback == null ? new DialogLifecycleCallback<CustomDialog>() {
  420. } : dialogLifecycleCallback;
  421. }
  422. public CustomDialog setDialogLifecycleCallback(DialogLifecycleCallback<CustomDialog> dialogLifecycleCallback) {
  423. this.dialogLifecycleCallback = dialogLifecycleCallback;
  424. if (isShow) dialogLifecycleCallback.onShow(me);
  425. return this;
  426. }
  427. public OnBackPressedListener getOnBackPressedListener() {
  428. return onBackPressedListener;
  429. }
  430. public CustomDialog setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
  431. this.onBackPressedListener = onBackPressedListener;
  432. refreshUI();
  433. return this;
  434. }
  435. public CustomDialog setStyle(DialogXStyle style) {
  436. this.style = style;
  437. return this;
  438. }
  439. public CustomDialog setTheme(DialogX.THEME theme) {
  440. this.theme = theme;
  441. return this;
  442. }
  443. public boolean isCancelable() {
  444. if (privateCancelable != null) {
  445. return privateCancelable == BOOLEAN.TRUE;
  446. }
  447. if (overrideCancelable != null) {
  448. return overrideCancelable == BOOLEAN.TRUE;
  449. }
  450. return cancelable;
  451. }
  452. public CustomDialog setCancelable(boolean cancelable) {
  453. this.privateCancelable = cancelable ? BOOLEAN.TRUE : BOOLEAN.FALSE;
  454. refreshUI();
  455. return this;
  456. }
  457. public CustomDialog.DialogImpl getDialogImpl() {
  458. return dialogImpl;
  459. }
  460. public CustomDialog setCustomView(OnBindView<CustomDialog> onBindView) {
  461. this.onBindView = onBindView;
  462. refreshUI();
  463. return this;
  464. }
  465. public View getCustomView() {
  466. if (onBindView == null) return null;
  467. return onBindView.getCustomView();
  468. }
  469. public CustomDialog removeCustomView() {
  470. this.onBindView.clean();
  471. refreshUI();
  472. return this;
  473. }
  474. public int getEnterAnimResId() {
  475. return enterAnimResId;
  476. }
  477. public CustomDialog setEnterAnimResId(int enterAnimResId) {
  478. this.enterAnimResId = enterAnimResId;
  479. return this;
  480. }
  481. public int getExitAnimResId() {
  482. return exitAnimResId;
  483. }
  484. public CustomDialog setExitAnimResId(int exitAnimResId) {
  485. this.exitAnimResId = exitAnimResId;
  486. return this;
  487. }
  488. public CustomDialog setAnimResId(int enterAnimResId, int exitAnimResId) {
  489. this.enterAnimResId = enterAnimResId;
  490. this.exitAnimResId = exitAnimResId;
  491. return this;
  492. }
  493. public ALIGN getAlign() {
  494. return align;
  495. }
  496. public CustomDialog setAlign(ALIGN align) {
  497. this.align = align;
  498. return this;
  499. }
  500. public boolean isAutoUnsafePlacePadding() {
  501. return autoUnsafePlacePadding;
  502. }
  503. public CustomDialog setAutoUnsafePlacePadding(boolean autoUnsafePlacePadding) {
  504. this.autoUnsafePlacePadding = autoUnsafePlacePadding;
  505. refreshUI();
  506. return this;
  507. }
  508. public CustomDialog setFullScreen(boolean fullscreen) {
  509. this.autoUnsafePlacePadding = !autoUnsafePlacePadding;
  510. refreshUI();
  511. return this;
  512. }
  513. public CustomDialog setMaskColor(@ColorInt int maskColor) {
  514. this.maskColor = maskColor;
  515. refreshUI();
  516. return this;
  517. }
  518. public long getEnterAnimDuration() {
  519. return enterAnimDuration;
  520. }
  521. public CustomDialog setEnterAnimDuration(long enterAnimDuration) {
  522. this.enterAnimDuration = enterAnimDuration;
  523. return this;
  524. }
  525. public long getExitAnimDuration() {
  526. return exitAnimDuration;
  527. }
  528. public CustomDialog setExitAnimDuration(long exitAnimDuration) {
  529. this.exitAnimDuration = exitAnimDuration;
  530. return this;
  531. }
  532. @Override
  533. public void restartDialog() {
  534. if (dialogView != null) {
  535. dismiss(dialogView);
  536. isShow = false;
  537. }
  538. if (getDialogImpl().boxCustom != null) {
  539. getDialogImpl().boxCustom.removeAllViews();
  540. }
  541. enterAnimDuration = 0;
  542. dialogView = createView(R.layout.layout_dialogx_custom);
  543. dialogImpl = new DialogImpl(dialogView);
  544. if (dialogView != null) dialogView.setTag(me);
  545. show(dialogView);
  546. }
  547. public void hide() {
  548. if (getDialogView() != null) {
  549. getDialogView().setVisibility(View.GONE);
  550. }
  551. }
  552. @Override
  553. protected void shutdown() {
  554. dismiss();
  555. }
  556. public CustomDialog setDialogImplMode(DialogX.IMPL_MODE dialogImplMode) {
  557. this.dialogImplMode = dialogImplMode;
  558. return this;
  559. }
  560. public boolean isBkgInterceptTouch() {
  561. return bkgInterceptTouch;
  562. }
  563. public CustomDialog setBkgInterceptTouch(boolean bkgInterceptTouch) {
  564. this.bkgInterceptTouch = bkgInterceptTouch;
  565. return this;
  566. }
  567. public int getAlignBaseViewGravity() {
  568. return alignViewGravity;
  569. }
  570. /**
  571. * 判断是否有设置对应的位置关系
  572. *
  573. * @param gravity 位置关系
  574. * @return 是否具备位置关系
  575. */
  576. public boolean isAlignBaseViewGravity(int gravity) {
  577. return (alignViewGravity & gravity) == gravity;
  578. }
  579. public CustomDialog setAlignBaseViewGravity(View baseView, int alignGravity) {
  580. this.baseView = baseView;
  581. this.alignViewGravity = alignGravity;
  582. baseViewLoc = new int[2];
  583. baseView.getLocationOnScreen(baseViewLoc);
  584. setFullScreen(true);
  585. return this;
  586. }
  587. public CustomDialog setAlignBaseViewGravity(View baseView, int alignGravity, int marginLeft,
  588. int marginTop, int marginRight, int marginBottom) {
  589. this.marginRelativeBaseView = new int[]{marginLeft, marginTop, marginRight, marginBottom};
  590. return setAlignBaseViewGravity(baseView, alignGravity);
  591. }
  592. public int[] getBaseViewMargin() {
  593. return marginRelativeBaseView;
  594. }
  595. public CustomDialog setBaseViewMargin(int[] marginRelativeBaseView) {
  596. this.marginRelativeBaseView = marginRelativeBaseView;
  597. return this;
  598. }
  599. public CustomDialog setBaseViewMargin(int marginLeft, int marginTop,
  600. int marginRight, int marginBottom) {
  601. this.marginRelativeBaseView = new int[]{marginLeft, marginTop, marginRight, marginBottom};
  602. return this;
  603. }
  604. public CustomDialog setBaseViewMarginLeft(int marginLeft){
  605. this.marginRelativeBaseView[0] = marginLeft;
  606. return this;
  607. }
  608. public CustomDialog setBaseViewMarginTop(int marginTop){
  609. this.marginRelativeBaseView[1] = marginTop;
  610. return this;
  611. }
  612. public CustomDialog setBaseViewMarginRight(int marginRight){
  613. this.marginRelativeBaseView[2] = marginRight;
  614. return this;
  615. }
  616. public CustomDialog setBaseViewMarginBottom(int marginBottom){
  617. this.marginRelativeBaseView[3] = marginBottom;
  618. return this;
  619. }
  620. public int getBaseViewMarginLeft(int marginLeft){
  621. return this.marginRelativeBaseView[0];
  622. }
  623. public int getBaseViewMarginTop(int marginLeft){
  624. return this.marginRelativeBaseView[1];
  625. }
  626. public int getBaseViewMarginRight(int marginLeft){
  627. return this.marginRelativeBaseView[2];
  628. }
  629. public int getBaseViewMarginBottom(int marginLeft){
  630. return this.marginRelativeBaseView[3];
  631. }
  632. public View getBaseView() {
  633. return baseView;
  634. }
  635. public int getWidth() {
  636. return width;
  637. }
  638. /**
  639. * 设置对话框 UI 宽度(单位:像素)
  640. *
  641. * @param width 宽度(像素)
  642. * @return CustomDialog实例
  643. */
  644. public CustomDialog setWidth(int width) {
  645. this.width = width;
  646. refreshUI();
  647. return this;
  648. }
  649. public int getHeight() {
  650. return height;
  651. }
  652. /**
  653. * 设置对话框 UI 高度(单位:像素)
  654. *
  655. * @param height 高度(像素)
  656. * @return CustomDialog实例
  657. */
  658. public CustomDialog setHeight(int height) {
  659. this.height = height;
  660. refreshUI();
  661. return this;
  662. }
  663. public OnBackgroundMaskClickListener<CustomDialog> getOnBackgroundMaskClickListener() {
  664. return onBackgroundMaskClickListener;
  665. }
  666. public CustomDialog setOnBackgroundMaskClickListener(OnBackgroundMaskClickListener<CustomDialog> onBackgroundMaskClickListener) {
  667. this.onBackgroundMaskClickListener = onBackgroundMaskClickListener;
  668. return this;
  669. }
  670. }