DialogFragment以及PopupWindow的对话框使用简述

DialogFragment以及PopupWindow的对话框使用简述

FragmentDialog

一般的写法:

都是继承DialogFragment重写onCreateView方法,也可以重写onCreateDialog方法,这个方法就类似于AlertDialog了

/**
 * @author qzx
 * @time 2018/8/3 17:07
 */
public class SelectPenDialogFragment extends DialogFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置DIM
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //设置外部触摸消失
        getDialog().setCanceledOnTouchOutside(true);
        //设置取消标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        //设置Dialog动画
        getDialog().getWindow().setGravity(Gravity.CENTER);
        getDialog().getWindow().setWindowAnimations(R.style.dialogStyle);


        View view = inflater.inflate(R.layout.select_pen_layout, container, false);

       ......

        return view;
    }
}
对话框样式为:
<!--这个用于弹出动画样式-->
  <style name="dialogStyle">
        <item name="android:windowEnterAnimation">@anim/pop_left_in</item>
        <item name="android:windowExitAnimation">@anim/pop_left_out</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

    <!--这个是对话框背景黯淡的设置样式 一般放置在onCreate的设置Style中,不过关于背景的设置有点奇怪,和布局中的文字,背景都有反差-->
    <style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:backgroundDimEnabled">false</item>
    </style>
//-------------------------------Activity中点击按钮弹出
        final PopupWindow popupWindow = new PopupWindow();
        View view = LayoutInflater.from(this).inflate(R.layout.xxx, null);
       ......
        popupWindow.setContentView(view);

        //设置宽高
        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        //设置背景和外部可触摸
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        popupWindow.setOutsideTouchable(true);

        //弹出动画
        popupWindow.setAnimationStyle(R.style.popupAnimation);

//        popupWindow.showAsDropDown(v);
//        int screenWidth = getResources().getDisplayMetrics().widthPixels;
//        int screenHeight = getResources().getDisplayMetrics().heightPixels;
//
//        int viewWidth = view.getMeasuredWidth();
//        int viewHeight = view.getMeasuredHeight();
//        Log.d("zbv", "width=" + viewWidth + ";height=" + viewHeight);

//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f;
//        getWindow().setAttributes(lp);


        DimForPopup();

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

                DimForPopup();

            }
        });

        //显示
        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER,
                0, 0);

    private boolean toBright = false;

    private void DimForPopup() {
        utilsDimBG.setDatas(1.0f, 0.5f, 500);
        utilsDimBG.setUpdateInterface(this);
        utilsDimBG.startAnimatiion();
    }
 //使用动画实现逐渐变暗的过程
    @Override
    public void updateResult(float result) {

        //1.0-0.5
        float alpha = toBright ? (1.5f - result) : result;

        Log.d("zbv", "toBright=" + toBright + ";result=" + result + ";alpha=" + alpha);

        changeBGAlpha(alpha);
    }

    @Override
    public void updateFinished(Animator animator) {

        toBright = true;
    }

    private void changeBGAlpha(float value) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = value;
        getWindow().setAttributes(lp);
    }
使用属性动画实现明暗渐变
package com.ahtelit.zbv.vphandler.dialogOrpopup;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.view.animation.LinearInterpolator;

/**
 * @author qzx
 * @time 2018/8/6 10:02
 * <br/>
 * 属性动画
 */
public class AnimUtilsDimBG {

    float start;
    float end;
    long duration;

    public void setDatas(float start, float end, long duration) {
        this.start = start;
        this.end = end;
        this.duration = duration;
    }

    public void startAnimatiion() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(start, end);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setDuration(duration);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                if (updateInterface != null) {
                    float result = (Float) animation.getAnimatedValue();

                    updateInterface.updateResult(result);
                }
            }
        });

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

                if (updateInterface != null) {
                    updateInterface.updateFinished(animation);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        valueAnimator.start();

    }

    private UpdateInterface updateInterface;

    public void setUpdateInterface(UpdateInterface updateInterface) {
        this.updateInterface = updateInterface;
    }

    public interface UpdateInterface {
        void updateResult(float result);

        void updateFinished(Animator animator);
    }
}
动画的xml(从上面滑下到中心,退出从中心下滑到下面消失)
//pop_left_in.xml在res/anim/
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="-55%p"
        android:toYDelta="0%"
        android:fromXDelta="0"
        android:toXDelta="0"
        android:duration="500"
        ></translate>
</set>
//pop_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="0%"
        android:toYDelta="55%p"
        android:fromXDelta="0"
        android:toXDelta="0"
        android:duration="500"
        ></translate>
</set>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值