窗口背景随弹窗的出现而渐变

之前写都是点击弹窗的时候直接将背景变成半透明,但是客户要求进行渐变

一、使用Activity.getWindow().setAttributes()方法来实现

工具代码灵活使用

1、窗口渐变工具代码

实现渐变的代码如下

public class WindowsBgAlpha {
    private Activity mActivity;
    Handler handler = new Handler();

    public WindowsBgAlpha(Activity activity) {
        this.mActivity = activity;
    }

    /**
     * 设置添加屏幕的背景透明度,
     * 在给定的时间内渐变
     */
    private float currentAlpha ;
    private boolean isAdd = false;
    public void backgroundAlpha(float startBgAlpha, final float endBgAlpha, long time) {
        //计算,实现渐变过程
        int end = (int) (endBgAlpha * 10);
        int start = (int) (startBgAlpha * 10);
        final int num ; //时间分为几份
        if (start>end){
            isAdd = false;
            num = start - end;
        }else {
            isAdd = true;
            num = end - start;
        }
        final long mTime = (time / num);//每段变化使用的时长
        final WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        currentAlpha = startBgAlpha;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isAdd){
                    currentAlpha += 0.1;
                    if (currentAlpha>endBgAlpha){
                        handler.removeCallbacks(this);
                    }else {
                        handler.postDelayed(this,mTime);
                    }
                }else {
                    currentAlpha -= 0.1;
                    if (currentAlpha<endBgAlpha){
                        handler.removeCallbacks(this);
                    }else {
                        handler.postDelayed(this,mTime);
                    }
                }
                lp.alpha = currentAlpha; //0.0-1.0
                mActivity.getWindow().setAttributes(lp);
            }
        }, mTime);
    }
}

2、和弹窗一起使用

随弹窗的出现而进行渐变,主要就是这个方法backgroundAlpha(float startBgAlpha, final float endBgAlpha, long time)

private void showPopupWindow() {
        View showView = LayoutInflater.from(BarrierActivity.this).inflate(R.layout.sub_popup_window, null);
        popupWindow = new PopupWindow(showView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.AnimationPopup);
          //一定要在代码中setBackgroundDrawable,不然点击外面popupWindow是不会dismiss的
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.choose_sub_shape));
        View v = LayoutInflater.from(BarrierActivity.this).inflate(R.layout.activity_brkthough, null);
        ImageView menuDown = (ImageView) showView.findViewById(R.id.menu_down);
        menuDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        new WindowsBgAlpha(BarrierActivity.this).backgroundAlpha(1.0f, 0.6f, 200);
        popupWindow.setOnDismissListener(new poponDismissListener());
        RecyclerView recyclerView = (RecyclerView) showView.findViewById(R.id.recycler_view);
        TextView popupTime = (TextView) showView.findViewById(R.id.popup_time);
        long nowTime = clockUtils.getNowTime();
        clockUtils.stopTime();
        clock = new ClockUtils(nowTime, handler, popupTime);
        clock.startTime();
        ChooseSubjectAdapter adapter = new ChooseSubjectAdapter(BarrierActivity.this, mData, totalCount, new ChooseSubjectAdapter.ItemClickListener() {
            @Override
            public void onClick(View v) {
                int tag = (int) v.getTag();
                brkthoughActivityViewpager.setCurrentItem(tag, true);
                popupWindow.dismiss();
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
    }

    class poponDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            //这里的时间最好和窗口进出时动画执行时间一致
            new WindowsBgAlpha(BarrierActivity.this).backgroundAlpha(0.6f, 1.0f, 300);
            if (clock!=null){
                long nowTime = clock.getNowTime();
                clock.stopTime();
                if (nowTime==0){
                    bottomTime.setText("00:00");
                }else {
                    clockUtils.refreshTime(nowTime);
                }
            }
        }

    }

二 、 通过给Dialog设置style来实现渐变


    //color
    <color name="tram">#00000000</color>

    //animal
    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/abc_slide_in_bottom</item>
        <item name="android:windowExitAnimation">@anim/abc_slide_out_bottom</item>
    </style>

    <style name="dialog_style" parent="android:Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/tram</item>
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    </style>

abc_slide_in_bottom

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/decelerate_interpolator"
           android:fromYDelta="50%p" android:toYDelta="0"
           android:duration="@android:integer/config_mediumAnimTime"/>

abc_slide_out_bottom

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromYDelta="0" android:toYDelta="50%p"
           android:duration="@android:integer/config_mediumAnimTime"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值