1.建立anim动画文件
(1)建立popshow_anim.xml文件(popwindow显示动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
(2)建立pophidden_anim.xml文件(popwindow退出动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
2.布局文件pop.xml(此处写弹出的内容)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
3.Activity文件
public class Activity extends AppCompatActivity {
private View contentView;
private PopupWindow popupWindow;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
showPopwindow();
//将背景透明化
WindowManager.LayoutParams params=GoodsDetail.this.getWindow().getAttributes();
params.alpha=0.5f;
GoodsDetail.this.getWindow().setAttributes(params);
//弹出
popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
//退出
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
WindowManager.LayoutParams params=getActivity().getWindow().getAttributes();
params.alpha=1f;
getActivity().getWindow().setAttributes(params);
}
}
private void showPopwindow() {
contentView = LayoutInflater.from(GoodsDetail.this).inflate(
R.layout.pop, null);
//此处使用contentView.findViewById找pop.xml中控件
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);// 取得焦点
//注意 要是点击外部空白处弹框消息 那么必须给弹框设置一个背景色 不然是不起作用的
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//点击外部消失
popupWindow.setOutsideTouchable(true);
//设置可以点击
popupWindow.setTouchable(true);
//进入退出的动画
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
//popupWindow隐藏时恢复屏幕正常透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params=GoodsDetail.this.getWindow().getAttributes();
params.alpha=1f;
GoodsDetail.this.getWindow().setAttributes(params);
}
});
}
}
4.在style中
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style>