admin
2021-06-11 ae4dc86b64bd8ef85bc832106741fb98e8d516da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.tejia.lijin.app.util;
 
import android.app.AlertDialog;
import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;
 
import com.tejia.lijin.app.R;
 
/**
 * Toast Dialog 弹窗提醒
 */
public class Toast_Dialog {
    private CountDownTimer timer;//计时器
    private AlertDialog.Builder alterDiaglog;
    private View view;
    private AlertDialog dialog;
 
    public Toast_Dialog(Context context) {
        alterDiaglog = new AlertDialog.Builder(context, R.style.dialog_activationinvitecode);
        view = View.inflate(context, R.layout.dialog_activationinvite1, null);
        alterDiaglog.setView(view);//加载进去布局视图
        dialog = alterDiaglog.create();
    }
 
    /**
     * @param str 提示语
     * @param boo 是否 点击dialog外部消失 true不消失 false消失
     */
    public void MyDialog(String str, boolean boo) {
        if (dialog != null) {
                //显示
                dialog.show();
            ((TextView) dialog.findViewById(R.id.dialog_activationinvitetxt)).setText(str);
            if (str != null && boo) {
                dialog.setCanceledOnTouchOutside(false);//点击不消失
                dialogShouw(dialog, true);//计时器 关闭 dialog
            } else { // 失败提示
                dialogShouw(dialog, false);//计时器 关闭 dialog
//            startShakeByView(view, 0.5f, 1f, 1.7f, 8L);//抖动动画
            }
        }
    }
 
 
    /**
     * 倒计时
     *
     * @param dialog
     * @param b      是否触摸外面关闭dialog
     */
    private void dialogShouw(final AlertDialog dialog, final boolean b) {
        if (timer != null) {
            timer.cancel();
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                dialog.cancel();
            }
        }
        if (dialog != null) {
            //显示
            if (!dialog.isShowing()) {
                dialog.show();
            }
            timer = new CountDownTimer(1015, 1000) {
                public void onTick(long millisUntilFinished) {
                }
 
                public void onFinish() {
                    if (b) {
                        dialog.setCanceledOnTouchOutside(true);
                    }
                    dialog_dismiss();//关闭
                }
            }.start();
        }
    }
 
 
    /**
     * 抖动效果的实现
     *
     * @param view
     * @param scaleSmall
     * @param scaleLarge
     * @param shakeDegrees
     * @param duration
     */
    private void startShakeByView(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
        //由小变大
        Animation scaleAnim = new ScaleAnimation(scaleSmall, scaleLarge, scaleSmall, scaleLarge);
        //从左向右
        Animation rotateAnim = new RotateAnimation(-shakeDegrees, shakeDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
 
        scaleAnim.setDuration(duration);
        rotateAnim.setDuration(duration / 10);
        rotateAnim.setRepeatMode(Animation.REVERSE);
        rotateAnim.setRepeatCount(10);
 
        AnimationSet smallAnimationSet = new AnimationSet(false);
        smallAnimationSet.addAnimation(scaleAnim);
        smallAnimationSet.addAnimation(rotateAnim);
 
        view.startAnimation(smallAnimationSet);
    }
 
    /**
     * 关闭dialog
     */
    public void dialog_dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog.cancel();
        }
        if (timer != null) {
            timer.cancel();
        }
///  如果报错注释掉
//        timer = null;//计时器
//        dialog = null;
//        alterDiaglog = null;
//        view = null;
    }
}