通用 PopupWindow,幾行代碼搞定 PopupWindow 彈窗(續)

一 、前言

前面爲了在項目中使用Popupwindow簡單方便,本身簡易封裝了一個PopupWindow,能夠在項目很方便的使用。尚未看過的能夠去看一下文章介紹,通用PopupWindow,幾行代碼搞定PopupWindow彈窗,前段時間看到有留言說彈出PopupWindow同時使背景變暗這個功能沒有,能不能加上?想着這是個比較常見的需求,所以把它加到了這個庫中,本篇文章就簡單談談怎麼實現這個功能。
項目地址:github.com/pinguo-zhou…java

最終效果圖:android

popWindow.gif

二 、彈出PopupWindow 同時背景變暗

咱們知道,一個Activity包含了一個Window對象,Activity是不直接顯示View,View 被綁定到Window上來得以呈現,也就是說View必須是依附Window而存在的,一樣PopupWindow也是如此。所以要控制整個屏幕的背景的變暗和變亮,咱們只須要控制當前Activity的window的亮度,經過alpha屬性的值來控制就好了。git

咱們先來寫個例子測試一下:經過一個seekbar來控制alpha值來改變屏幕的亮度。github

1, xml 文件以下:ide

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <android.support.v7.widget.AppCompatSeekBar
       android:id="@+id/seek_bar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
     android:layout_centerInParent="true"
       android:layout_marginLeft="10dp"
       android:layout_marginRight="10dp"
       android:layout_marginBottom="20dp"
       />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變當前window亮度:"
        android:textColor="@android:color/darker_gray"
        android:textSize="16sp"
        android:layout_above="@+id/seek_bar"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        />
</RelativeLayout>複製代碼

2, 代碼以下:測試

mAppCompatSeekBar = (AppCompatSeekBar) findViewById(R.id.seek_bar);
        mAppCompatSeekBar.setMax(100);
        mAppCompatSeekBar.setProgress(100);
        mAppCompatSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               float alpha = seekBar.getProgress() * 1.0f / 100 ;
                if(alpha < 0.2){
                    alpha = 0.2f;
                }
               Window mWindow = getWindow();
                WindowManager.LayoutParams params = mWindow.getAttributes();
                params.alpha = alpha;
                mWindow.setAttributes(params);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });複製代碼

效果以下:
動畫

更改當前背景度.gif

看到沒?隨着拖動seekBar,屏幕的亮度就跟着改變了,這不就是咱們想要的效果嘛。ui

PopupWindow 彈出和消失時改變當前window的alpha 來控制屏幕亮度。this

有了上面的測試,咱們就可讓PopupWindow彈出時,同時屏幕變暗了。在build()方法中添加以下代碼:spa

// 獲取當前Activity的window
        Activity activity = (Activity) mContentView.getContext();
        if(activity!=null && mIsBackgroundDark){
            //若是設置的值在0 - 1的範圍內,則用設置的值,不然用默認值
            final  float alpha = (mBackgroundDrakValue > 0 && mBackgroundDrakValue < 1) ? mBackgroundDrakValue : DEFAULT_ALPHA;
            mWindow = activity.getWindow();
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = alpha;
            mWindow.setAttributes(params);
        }複製代碼

加了上面的代碼,在彈出PopupWindow的時候,屏幕就會變暗了,可是你會發現一個問題,PopupWindow消失以後,屏幕仍然是暗的,這固然不是咱們想要的,所以在PopupWindow消失的時候,須要改回原來的值。重寫onDismiss方法,添加以下代碼:

//若是設置了背景變暗,那麼在dissmiss的時候須要還原
        if(mWindow!=null){
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = 1.0f;
            mWindow.setAttributes(params);
        }複製代碼

如今,這個功能就完成了。看一下效果圖:

彈窗背景變暗.gif

3、CustomPopWindow 使用背景變暗配置

上面講了背景變暗的思路和方法,那麼咱們看一下CustomPopWindow 怎麼使用這個功能,很是簡單,添加了2個配置方法:

  • enableBackgroundDark(boolean isDark):是否開啓背景變暗,使用默認亮度值,也可使用setBgDarkAlpha改變亮度值。
  • setBgDarkAlpha(float darkValue):改變背景亮度值。

示例代碼以下:

//建立並顯示popWindow
 mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .enableBackgroundDark(true) //彈出popWindow時,背景是否變暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()
                .showAsDropDown(mButton5,0,20);複製代碼

4、添加PopupWindow顯示和消失動畫

有時候,咱們可能須要在PopupWindow 顯示和消失的時候添加上一些動畫,Popwindow是能夠本身添加顯示和消失的動畫的,經過setAnimationStyle方法設置,顯示動畫和消失動畫分別由android:windowEnterAnimationandroid:windowExitAnimation2個屬性控制。

以下步驟:
(1) 在res/anim 下添加兩個動畫文件
popwindow_anim_in:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <alpha android:fromAlpha="0"
          android:toAlpha="1.0"
          android:duration="100"
       />
    <scale android:fromXScale="0"
           android:toXScale="1.0"
           android:fromYScale="0"
           android:toYScale="1.0"
           android:duration="100"
        />
</set>複製代碼

popwindow_anim_out:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <alpha android:fromAlpha="1.0"
          android:toAlpha="0.0"
          android:duration="100"
       />
    <scale android:fromXScale="1.0"
           android:toXScale="0"
           android:fromYScale="1.0"
           android:toYScale="0"
           android:duration="100"
        />
</set>複製代碼

(2) 在styles.xml 中添加一個style

<style name="CustomPopWindowStyle">
        <item name="android:windowEnterAnimation">@anim/popwindow_anim_in</item>
        <item name="android:windowExitAnimation">@anim/popwindow_anim_out</item>
    </style>複製代碼

(3) 經過setAnimationStyle 應用動畫

CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(R.layout.pop_layout1)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .setAnimationStyle(R.style.CustomPopWindowStyle) // 添加自定義顯示和消失動畫
                .create()
                .showAsDropDown(mButton1,0,10);複製代碼

經過上面就能夠給PopupoWindow的顯示和消失添加動畫,除此以外,在API 21 以上,Google 給PopupWindow 添加了2個新方法用來添加顯示和消失時的過渡動畫,setEnterTransitionsetExitTransition,有興趣的能夠去研究一下。

5、總結

以上就是對於給PopupWindow的添加背景變暗和動畫的一些思路分析和實踐總結,若是你以爲每次添加一個PopupWindow很麻煩,那麼你不妨試一下這個簡單的庫,github.com/pinguo-zhou…。若是你還有什麼須要的功能,能夠留言,我會添加到這個庫上面。

相關文章
相關標籤/搜索