本文由PurpleSword(jzj1993)原创,转载请注明
原文网址 http://blog.csdn.net/jzj1993
定义弹窗动画
res/anim/scale_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="300"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
res/anim/scale_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.0"
android:toYScale="0.0" />
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</
set
>
res/values/styles.xml
<style name="pop_scale">
<item name="android:windowEnterAnimation">@anim/scale_in</item>
<item name="android:windowExitAnimation">@anim/scale_out</item>
</style>
程序实现
private PopupWindow pop;
void showPop() {
if (pop != null && pop.isShowing()) {
return;
}
LayoutInflater i = LayoutInflater.from(this);
View v = i.inflate(R.layout.main_menu_popupwnd, null);
// 指定宽度和高度 固定值
pop = new PopupWindow(v, 100, 100);
// 指定宽度和高度 LayoutParams参数
pop = new PopupWindow(v, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// 设置弹窗之外是否可点击(点击后弹窗消失)
pop.setOutsideTouchable(true);
// 要实现点击弹窗外部使得弹窗消失,需要设置背景
pop
.setBackgroundDrawable(
new
ColorDrawable(0));
// 设置动画
pop.setAnimationStyle(R.style.pop_scale);
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
// 刷新设置
pop.update();
// 显示 (位置和偏移:靠下方显示,向上偏移30像素)
pop.showAtLocation(findViewById(R.id.layout_main_scroll_view),
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 30);
boolean dismissPop() {
if (pop != null && pop.isShowing()) {
pop.dismiss();
return true;
}
return false;
}
按返回键、Activity暂停、退出时,确保关闭PopupWindow,否则会报错
@Override
public void onPause() {
dismissMenu();
super
.onPause();
}