使用
構(gòu)建popupwindow
int width = LayoutParams.WRAP_CONTENT;
int height = LayoutParams.WRAP_CONTENT;
//獲取要popupwindow對(duì)象—并設(shè)置參數(shù)
// 參1 要彈出的view 參2 寬 參3高
PopupWindow popupWindow = new PopupWindow(convertview, width, height);
// 點(diǎn)擊popupwindow外的地方不消失,點(diǎn)擊返回鍵應(yīng)用退出,說明焦點(diǎn)在activity上.
// 設(shè)置背景--點(diǎn)擊外面會(huì)消失,點(diǎn)擊返回鍵也會(huì)消失(如果不設(shè)置,不消失)
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
// 點(diǎn)擊popupwindow外的地方不消失,點(diǎn)擊返回鍵應(yīng)用退出,說明焦點(diǎn)在activity上.
讓其按返回鍵不退出也不消失
//讓其有焦點(diǎn)--按返回鍵不退出了,但也不消失
// 是因?yàn)槟銢]有設(shè)置popupwindow的背景
popupWindow.setFocusable(true);
三種show的方式
//第一種 參1 錨點(diǎn) :view 即:顯示到這個(gè)view的下方
popupWindow.showAsDropDown(tv);
//第二種 參1view 參2x軸的偏移量 參3Y軸的偏移量
// 默認(rèn)顯示在參一的下方
popupWindow.showAsDropDown(convertview, 100, 100);
//第三種 參1輸入當(dāng)前activity里的任意一個(gè)view--為了找到當(dāng)前activity ,window的一個(gè)標(biāo)識(shí)
// 參2 x軸的偏移量
// 參3 Y軸的偏移量
// 參4 重心位置顯示的大概位置,可以加或|來再更加確定其位置
// 默認(rèn)是上下居中的,且是全屏的
popupWindow.showAsDropDown(tv, 0, 0, Gravity.RIGHT | Gravity.TOP);
設(shè)置動(dòng)畫樣式
popupWindow.setAnimationStyle(R.style.PopAnimation);
案例
protected void ShowPop(View view) {
//----------------構(gòu)造pop--------------
//將布局轉(zhuǎn)成view
View contentView = View.inflate(getApplicationContext(), R.layout.pop_view, null);
//寬高是WRAP_CONTENT
int width = LayoutParams.WRAP_CONTENT;
int height = LayoutParams.WRAP_CONTENT;
//獲取popupwindow對(duì)象
// 參1 要現(xiàn)實(shí)的彈出框的view 參2 寬 參3高
PopupWindow popupWindow = new PopupWindow(contentView, width, height);
//----------------一般性設(shè)置--------------
//按返回鍵不退出了,但也不消失
popupWindow.setFocusable(true);// 讓有焦點(diǎn)
//設(shè)置背景 如果不設(shè)置 點(diǎn)擊其它區(qū)域不能消失 如果不需要 就設(shè)置為透明
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//----------------設(shè)置動(dòng)畫樣式--------------
popupWindow.setAnimationStyle(R.style.PopAnimation);
//----------------顯示--------------
// 錨點(diǎn) 顯示在描點(diǎn)的下面
// popupWindow.showAsDropDown(view);
// 參2 x軸偏移量 參3 y軸偏移量
//要向右和向上挪動(dòng)
popupWindow.showAsDropDown(view, 60, -60);
// 參1 輸入當(dāng)前activity里的任意一個(gè)view 參2重心位置 參3x軸偏移量參4y軸偏移量
// popupWindow.showAtLocation(tv, Gravity.RIGHT | Gravity.TOP, 0, 100);
}
進(jìn)入和退出動(dòng)畫
enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="1000"
android:fromXDelta="100%"
android:interpolator="@anim/overshoot"
android:toXDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.5"
android:interpolator="@anim/decelerate_cubic"
android:toAlpha="1.0" />
</set>
exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="1000"
android:fromXDelta="0"
android:interpolator="@anim/anticipate"
android:toXDelta="100%" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@anim/accelerate_cubic"
android:toAlpha="0.0" />
</set>
插補(bǔ)器
到sdk下面拷貝
使用Androidstudio--將其放在res/anim目錄下
eclipse放在res/interpolator目錄下
android-sdk-windows\platforms\android-20\data\res\interpolator
Anticipate.xml和Overshoot.xml
anticipate.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2009, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<anticipateInterpolator />
accelerate_cubic.xml
<?xml version="1.0" encoding="utf-8"?><!--
/*
** Copyright 2010, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<accelerateInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="1.5" />
添加動(dòng)畫樣式--然后在代碼中添加
<style name="PopAnimation">
<item name="android:windowEnterAnimation">@anim/pop_enter</item>
<item name="android:windowExitAnimation">@anim/pop_exit</item>
</style>
修改彈出pop的背景
//彈出pop前后修改背景色
private void setBackgroundAlpha(float from, float to, int duration) {
final WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
ValueAnimator animator = ValueAnimator.ofFloat(from, to);
animator.setDuration(duration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
lp.alpha = (float) animation.getAnimatedValue();
mActivity.getWindow().setAttributes(lp);
}
});
animator.start();
}
//或者
public void backgroundAlpaha(Activity context, float bgAlpha) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.alpha = bgAlpha;
context.getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
context.getWindow().setAttributes(lp);
}
消失后將背景透明度還原
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackgroundAlpha(alpha, 1f, 300);
}
});
修改pop的寬高
和View(錨點(diǎn))一樣寬
int width = view.getWidth();
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
mPopupWindow = new PopupWindow(mTopPop, width, height);
全屏寬
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
mPopupWindow = new PopupWindow(mTopPop, width, height);