PopupWindow设置动画效果
创建popupwindow的方法
Button menu;
private void showPopupWindow() {
//设置contentView
float density = DensityUtil.Obtain(activity).density;
View contentView = LayoutInflater.from(ActivityHomeImpl.this).inflate(R.layout.activity_home_menu, null);
contentView.setBackgroundColor(0xff003333);
mPopWindow = new PopupWindow(contentView, -2, DensityUtil.round(50* density), true);//DensityUtil.round(50* density)
mPopWindow.setContentView(contentView);
//设置各个控件的点击响应
((Button)contentView.findViewById(R.id.btn1)).setOnClickListener(this);
((Button)contentView.findViewById(R.id.btn2)).setOnClickListener(this);
((Button)contentView.findViewById(R.id.btn3)).setOnClickListener(this);
((Button)contentView.findViewById(R.id.btn4)).setOnClickListener(this);
((Button)contentView.findViewById(R.id.btn5)).setOnClickListener(this);
((Button)contentView.findViewById(R.id.btn6)).setOnClickListener(this);
mPopWindow.setTouchable(true);
mPopWindow.setOutsideTouchable(true);
// 实例化一个ColorDrawable颜色为半透明
//ColorDrawable dw = new ColorDrawable(0xd0000000);
//mPopWindow.setBackgroundDrawable(dw);
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
mPopWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
mPopWindow.dismiss();
}
});
// 设置popWindow的显示和消失动画
mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
mPopWindow.showAsDropDown(menu);
}
popupwindow 动画
activity的Theme设置
context_menu_enter.xml
<?xml version="1.0" encoding="utf-8"?>
context_menu_exit.xml
<?xml version="1.0" encoding="utf-8"?>
布局文件activity_home_menu.xml
<?xml version="1.0" encoding="utf-8"?>