package com.yeshi.ec.rebate.myapplication.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.yeshi.ec.rebate.myapplication.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();
|
}
|
});
|
}
|
}
|
}
|