自定义dialog中设置弹出消失动画

自定义dialog  setContentView 根不同场景放在不同生命周期地方: dialog生命周期

方法一给dialog中 设置窗口属性  window.setWindowAnimations

1-1 给dialog 设置主题   res/values/styles

<resources>
<!--    设置dialog 主题-->
    <style name="RequestDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>
<!--    设置dialog进出场动画-->
    <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
    </style>
</resources>

1-2 动画文件 res/anim 

dialog_enter_anim:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="400"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        />
    <scale
        android:duration="400"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        />
</set>

dialog_exit_anim:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:zAdjustment="top"
    android:fillEnabled="true"
    android:fillAfter="true"
    android:duration="200">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        />
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.5"
        android:fromYScale="1.0"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        />
</set>

1-3 初始化的地方 配置进出场动画

public class LocationPermissionGuideDialog extends Dialog {
    private OnButtonClickListener onButtonClickListener;
    private Context mContext;
    View rootView;

    public void setOnButtonClick(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public LocationPermissionGuideDialog(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public LocationPermissionGuideDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        this.getWindow().setSoftInputMode(16);

        initView(context);
        Window window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明
        WindowManager.LayoutParams wl = window.getAttributes();
        //根据x,y坐标设置窗口需要显示的位置
//        wl.x = x; //x小于0左移,大于0右移
//        wl.y = y; //y小于0上移,大于0下移
//        wl.alpha = 0.6f; //设置透明度
//        wl.gravity = Gravity.BOTTOM; //设置重力
        window.setAttributes(wl);
    }

    private void initView(final Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_home_location, null);
        setContentView(rootView);
        findViewById(R.id.tv_go_setting).setOnClickListener(v -> {
            super.dismiss();
            if (onButtonClickListener != null) {
                onButtonClickListener.onPositiveButtonClick();
            }
        });
        findViewById(R.id.iv_close).setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onNegativeButtonClick();
            }
        });
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(mContext.getResources().getColor(R.color.white));
        findViewById(R.id.iv_bottom_bg).setBackground(gradientDrawable);
    }


    public void show() {
        super.show();
    }

    public interface OnButtonClickListener {
        void onPositiveButtonClick();

        void onNegativeButtonClick();
    }
}

优点: dialog 布局比较复杂 也不会显得卡顿

缺点: 不能监听动画生命周期    做根据动画生命周期做额外操作

方法二  view动画 中补间动画 animation

AnimationUtils.loadAnimation  加载anim中的动画     合适的时机设置给view

优点: 可以针对动画添加  动画的生命周期监听

缺点: 本身不能添加延时开启动画, 需要借助延时器

public class LocationPermissionGuideDialog extends Dialog implements AnimationListener {

    Animation mAnimationOut;
    Animation mAnimationIn;
    private OnButtonClickListener onButtonClickListener;
    private Context mContext;
    View rootView;

    public void setOnButtonClick(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public LocationPermissionGuideDialog(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public LocationPermissionGuideDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        this.getWindow().setSoftInputMode(16);
        this.mAnimationOut = AnimationUtils.loadAnimation(this.getContext(), R.anim.dialog_exit_anim);
        this.mAnimationOut.setAnimationListener(this);
        this.mAnimationIn = AnimationUtils.loadAnimation(this.getContext(), R.anim.dialog_enter_anim);
        this.mAnimationIn.setAnimationListener(this);
        Log.e("TAG","----GuideDialog 构造");
//        initView(context);
        setContentView(R.layout.dialog_home_location);
        findViewById(R.id.tv_go_setting).setOnClickListener(v -> {
           dismiss();
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("TAG","----GuideDialog onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("TAG","----GuideDialog onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("TAG","----GuideDialog onStop");
    }

    @Override
    public void cancel() {
        super.cancel();
        Log.e("TAG","----GuideDialog cancel");
    }

    private void initView(final Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_home_location, null);
        setContentView(rootView);
        findViewById(R.id.tv_go_setting).setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onPositiveButtonClick();
            }
        });
        findViewById(R.id.iv_close).setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onNegativeButtonClick();
            }
        });
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(mContext.getResources().getColor(R.color.white));
        findViewById(R.id.iv_bottom_bg).setBackground(gradientDrawable);
    }

    public void show() {
        super.show();
        this.mAnimationIn.reset();
        if (this.isShowing()) {
            this.findViewById(R.id.location_dialog_layout).startAnimation(this.mAnimationIn);
        } else {
            this.findViewById(R.id.location_dialog_layout).setAnimation(this.mAnimationIn);
        }
        Log.e("TAG","----GuideDialog show");
    }

    public void dismiss() {
        if (this.animationIsEnd()) {
            this.mAnimationOut.reset();
            findViewById(R.id.location_dialog_layout).startAnimation(this.mAnimationOut);
        }
        Log.e("TAG","----GuideDialog dismiss");
    }

    public void onAnimationStart(Animation animation) {
    }

    public void onAnimationEnd(Animation animation) {
        if (animation == this.mAnimationOut) {
            Context context = ((ContextWrapper) this.getContext()).getBaseContext();
            if (context instanceof Activity) {
                if (Build.VERSION.SDK_INT >= 17) {
                    Activity activity = (Activity) context;
                    if (!activity.isFinishing() && !activity.isDestroyed()) {
                        super.dismiss();
                    }
                } else {
                    try {
                        super.dismiss();
                    } catch (Exception var4) {
                    }
                }
            } else {
                super.dismiss();
            }
        }
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public boolean animationIsEnd() {
        Animation animation = findViewById(R.id.location_dialog_layout).getAnimation();
        return animation == null || animation.hasEnded();
    }

    public interface OnButtonClickListener {
        void onPositiveButtonClick();

        void onNegativeButtonClick();
    }
}

三  属性动画    优化:多个属性动画 一起执行  最好使用ObjectAnimator.ofPropertyValuesHolder   减少创建动画对象

优点:1 可以监听动画的生命周期   在动画结束 做下一步事情    2 可以延时执行动画

 

public class LocationPermissionGuideDialog extends Dialog implements Animator.AnimatorListener {

    private OnButtonClickListener onButtonClickListener;
    private Context mContext;
    View rootView;
    // 是否执行动画reverse
    public boolean isReverse;
    ObjectAnimator animator;

    public void setOnButtonClick(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public LocationPermissionGuideDialog(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public LocationPermissionGuideDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        this.getWindow().setSoftInputMode(16);


        Log.e("TAG", "----GuideDialog 构造");
//        initView(context);
        setContentView(R.layout.dialog_home_location);
        findViewById(R.id.tv_go_setting).setOnClickListener(v -> {
            dismiss();
        });

        PropertyValuesHolder animatorAlpha = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
        PropertyValuesHolder animatorX = PropertyValuesHolder.ofFloat("scaleX", 0f, 1f);
        PropertyValuesHolder animatorY = PropertyValuesHolder.ofFloat("scaleY", 0f, 1f);
        animator = ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.location_dialog_layout), animatorAlpha, animatorX, animatorY);
        animator.setDuration(400);
        // 延时开启动画
//        animator.setStartDelay(1000);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("TAG", "----GuideDialog onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("TAG", "----GuideDialog onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("TAG", "----GuideDialog onStop");
    }

    @Override
    public void cancel() {
        super.cancel();
        Log.e("TAG", "----GuideDialog cancel");
    }

    private void initView(final Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_home_location, null);
        setContentView(rootView);
        findViewById(R.id.tv_go_setting).setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onPositiveButtonClick();
            }
        });
        findViewById(R.id.iv_close).setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onNegativeButtonClick();
            }
        });
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(mContext.getResources().getColor(R.color.white));
        findViewById(R.id.iv_bottom_bg).setBackground(gradientDrawable);
    }

    public void playAnimAndShow() {
        isReverse = false;
        animator.start();
    }

    @Override
    public void onAnimationStart(Animator animation) {
        if (!isReverse) {
            // 进场动画开始  show
            show();
        }
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (isReverse) {
            Context context = ((ContextWrapper) this.getContext()).getBaseContext();
            if (context instanceof Activity) {
                if (Build.VERSION.SDK_INT >= 17) {
                    Activity activity = (Activity) context;
                    if (!activity.isFinishing() && !activity.isDestroyed()) {
                        super.dismiss();
                    }
                } else {
                    try {
                        super.dismiss();
                    } catch (Exception var4) {
                    }
                }
            } else {
                super.dismiss();
            }
            Log.e("TAG", "----GuideDialog reverse执行动画结束");
        } else {
            Log.e("TAG", "----GuideDialog 顺序执行动画结束");
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    public void show() {
        super.show();
        Log.e("TAG", "----GuideDialog show");
    }

    public void dismiss() {
        isReverse = true;
        animator.reverse();
        Log.e("TAG", "----GuideDialog dismiss");
    }

    public interface OnButtonClickListener {
        void onPositiveButtonClick();

        void onNegativeButtonClick();
    }
}

 

 

 

popupwindow 进出场动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值