最近做的android項目,需要實現彈出層,並灰掉彈出層後面的部分。大致需要做的效果爲,點擊more時,彈出一層只遮罩中間部分(由圖一到圖二),效果圖如下(個人隨便寫的樣式,勿怪哈),廢話不說,直接上思路代碼。
圖一 圖二
點擊more,調用的代碼
/**
* 點擊more觸發的方法,彈出顯示所有年級學科
* @param view
*/
public void showAllSubject(View view) {
if (null == mPopupWindow) {
View layout = LayoutInflater.from(this).inflate(
R.layout.activity_allgrade, null);
int width = subjectListView.getWidth();
int height = subjectListView.getHeight();
//設置彈出部分和麪積大小
mPopupWindow = new PopupWindow(layout, width, height, true);
//設置動畫彈出效果
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
// 設置半透明灰色
ColorDrawable dw = new ColorDrawable(0x7DC0C0C0);
mPopupWindow.setBackgroundDrawable(dw);
mPopupWindow.setClippingEnabled(true);
}
int[] pos = new int[2];
subjectListView.getLocationOnScreen(pos);
mPopupWindow.showAtLocation(subjectListView, Gravity.RIGHT | Gravity.TOP,
pos[0], pos[1]);
}
彈出效果配置
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
popup_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fromXScale="1.0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
popup_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="50"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="0" />
</set>
至此,主要代碼完成。備註:popup_enter.xml和popup_exit.xml兩個文件放在res下面的anim文件夾中,彈出層的頁面沒有貼代碼,大家可以根據自己的需求來編寫彈出頁面的內容。