一 、前言
前面為了在項目中使用Popupwindow簡單方便,自己簡易封裝了一個PopupWindow,可以在項目很方便的使用。還沒有看過的可以去看一下文章介紹,通用PopupWindow,幾行代碼搞定PopupWindow彈窗,前段時間看到有留言說彈出PopupWindow同時使背景變暗這個功能沒有,能不能加上?想著這是個比較常見的需求,因此把它加到了這個庫中,本篇文章就簡單談?wù)勗趺磳崿F(xiàn)這個功能。
項目地址:https://github.com/pinguo-zhouwei/CustomPopwindow
最終效果圖:
二 、彈出PopupWindow 同時背景變暗
我們知道,一個Activity包含了一個Window對象,Activity是不直接顯示View的,View 被綁定到Window上來得以呈現(xiàn),也就是說View必須是依附Window而存在的,同樣PopupWindow也是如此。因此要控制整個屏幕的背景的變暗和變亮,我們只需要控制**當(dāng)前Activity的window的亮度,通過alpha屬性的值來控制就行了。 **
我們先來寫個例子測試一下:通過一個seekBar來控制alpha值來改變屏幕的亮度。
1, xml 文件如下:
<?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="改變當(dāng)前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) {
}
});
效果如下:
看到?jīng)]?隨著拖動seekBar,屏幕的亮度就跟著改變了,這不就是我們想要的效果嘛。
PopupWindow 彈出和消失時改變當(dāng)前window的alpha 來控制屏幕亮度。
有了上面的測試,我們就可以讓PopupWindow彈出時,同時屏幕變暗了。在build()
方法中添加如下代碼:
// 獲取當(dāng)前Activity的window
Activity activity = (Activity) mContentView.getContext();
if(activity!=null && mIsBackgroundDark){
//如果設(shè)置的值在0 - 1的范圍內(nèi),則用設(shè)置的值,否則用默認(rèn)值
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的時候,屏幕就會變暗了,但是你會發(fā)現(xiàn)一個問題,PopupWindow消失之后,屏幕仍然是暗的,這當(dāng)然不是我們想要的,因此在PopupWindow消失的時候,需要改回原來的值。重寫onDismiss
方法,添加如下代碼:
//如果設(shè)置了背景變暗,那么在dissmiss的時候需要還原
if(mWindow!=null){
WindowManager.LayoutParams params = mWindow.getAttributes();
params.alpha = 1.0f;
mWindow.setAttributes(params);
}
現(xiàn)在,這個功能就完成了。看一下效果圖:
三、CustomPopWindow 使用背景變暗配置
上面講了背景變暗的思路和方法,那么我們看一下CustomPopWindow 怎么使用這個功能,非常簡單,添加了2個配置方法:
- enableBackgroundDark(boolean isDark):是否開啟背景變暗,使用默認(rèn)亮度值,也可以使用
setBgDarkAlpha
改變亮度值。 - setBgDarkAlpha(float darkValue):改變背景亮度值。
示例代碼如下:
//創(chuàng)建并顯示popWindow
mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
.setView(contentView)
.enableBackgroundDark(true) //彈出popWindow時,背景是否變暗
.setBgDarkAlpha(0.7f) // 控制亮度
.create()
.showAsDropDown(mButton5,0,20);
四、添加PopupWindow顯示和消失動畫
有時候,我們可能需要在PopupWindow 顯示和消失的時候添加上一些動畫,Popwindow是可以自己添加顯示和消失的動畫的,通過setAnimationStyle
方法設(shè)置,顯示動畫和消失動畫分別由android:windowEnterAnimation
和android:windowExitAnimation
2個屬性控制。
如下步驟:
(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 應(yīng)用動畫
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個新方法用來添加顯示和消失時的過渡動畫,setEnterTransition
和setExitTransition
,有興趣的可以去研究一下。
五、總結(jié)
以上就是對于給PopupWindow的添加背景變暗和動畫的一些思路分析和實踐總結(jié),如果你覺得每次添加一個PopupWindow很麻煩,那么你不妨試一下這個簡單的庫,https://github.com/pinguo-zhouwei/CustomPopwindow。如果你還有什么需要的功能,可以留言,我會添加到這個庫上面。