admin
2021-07-08 1764c1784a4cf1a6afd25fcf1a0eef6187a84218
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.tejia.lijin.app.ui.dialog;
 
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.tejia.lijin.app.R;
 
public class AnimationSwitchingDialog extends Dialog {
 
    public AnimationSwitchingDialog(Context context) {
        super(context);
    }
 
    public AnimationSwitchingDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public static class Builder {
        private Activity mContext;
        private String message;
        private int sex;
 
        private String positiveButtonText;
        private String negativeButtonText;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
 
        public Builder(Activity context) {
            this.mContext = context;
        }
 
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
 
        public Builder setSex(int sex) {
            this.sex = sex;
            return this;
        }
 
        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) mContext
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) mContext
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        AnimationSwitchingDialog dialog;
        TextView tv_switch_state;
 
        public AnimationSwitchingDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            WindowManager wm = (WindowManager) mContext
                    .getSystemService(Context.WINDOW_SERVICE);
 
            int width = wm.getDefaultDisplay().getWidth();
            dialog = new AnimationSwitchingDialog(mContext,
                    R.style.Dialog);
 
            View layout = inflater.inflate(R.layout.dialog_animation_switching,
                    null);
            final LinearLayout ll_img = layout.findViewById(R.id.ll_img);
            final ImageView iv_cover = layout.findViewById(R.id.iv_cover);
            final ImageView iv_basement = layout.findViewById(R.id.iv_basement);
            tv_switch_state = layout.findViewById(R.id.tv_switch_state);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
 
            Glide.with(mContext).load(sex == 0 ? R.drawable.ic_common : sex == 1 ?
                    R.drawable.ic_girl : R.drawable.ic_boy).into(iv_basement);
            Glide.with(mContext).load(sex == 0 ? R.drawable.ic_girl : sex == 1 ?
                    R.drawable.ic_boy : R.drawable.ic_common).into(iv_cover);
            tv_switch_state.setText(sex == 0 ? "正在跳转女生版" :
                    sex == 1 ? "正在跳转男生版" : "正在跳转默认版");
 
            final ObjectAnimator objectAnimator11 = ObjectAnimator.ofFloat(ll_img, "rotation", 180f);
            final ObjectAnimator objectAnimator12 = ObjectAnimator.ofFloat(iv_cover, "rotation", -180f);
            final ObjectAnimator objectAnimator13 = ObjectAnimator.ofFloat(iv_basement, "rotation", -180f);
            objectAnimator11.setDuration(1000);
            objectAnimator12.setDuration(1000);
            objectAnimator13.setDuration(1000);
 
            objectAnimator11.start();
            objectAnimator12.start();
            objectAnimator13.start();
 
            objectAnimator11.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    int toX = ll_img.getWidth() / 2 - iv_basement.getWidth() / 2;
                    secondAnimation2(iv_basement, iv_cover, toX);
                }
            });
 
            WindowManager.LayoutParams params = dialog.getWindow()
                    .getAttributes();
            params.gravity = Gravity.CENTER;
            params.width = (width * 3) / 5;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setAttributes(params);
            dialog.setCanceledOnTouchOutside(false);
            return dialog;
        }
 
        private void secondAnimation2(final ImageView iv_basement, final ImageView iv_cover, int toX) {
            final ObjectAnimator animation1 =
                    ObjectAnimator.ofFloat(iv_basement, "translationX", 0, toX);
            final ObjectAnimator animation2 =
                    ObjectAnimator.ofFloat(iv_cover, "translationX", 0, 0 - toX);
            animation1.setDuration(1000);
            animation2.setDuration(1000);
            animation1.start();
            animation2.start();
            animation2.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    tv_switch_state.setText(sex == 0 ? "已跳转到女生版" :
                            sex == 1 ? "已跳转到男生版" : "已跳转到默认版");
                    secondAnimation3(iv_cover);
                }
            });
        }
 
        private void secondAnimation3(final ImageView iv_cover) {
            ObjectAnimator toViewAnimatorX = ObjectAnimator.ofFloat(iv_cover, "scaleX", 1f, 1.2f);
            toViewAnimatorX.setDuration(1000);
            toViewAnimatorX.start();
            ObjectAnimator toViewAnimatorY = ObjectAnimator.ofFloat(iv_cover, "scaleY", 1f, 1.2f);
            toViewAnimatorY.setDuration(1000);
            toViewAnimatorY.start();
            toViewAnimatorX.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    dialog.dismiss();
                }
            });
        }
    }
}