Android PopUpWindow的底部弹出和消失

来源:互联网 发布:中国综合国力知乎 编辑:程序博客网 时间:2024/04/26 05:00
实现的方法是:
private void show(){
View popLayout = LayoutInflater.from(this).inflate(R.layout.layout_pop_up_window,null);
ImageView ivPop = popLayout.findViewById(R.id.iv_pop);
ivPop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "123456789", Toast.LENGTH_SHORT).show();
}
});
PopupWindow popupWindow = new PopupWindow(popLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(0));
popupWindow.setAnimationStyle(R.style.pop_anim);
popupWindow.showAtLocation(btnShow, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}


在values中的style文件中添加样式:
<style name="pop_anim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
<item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
</style>

在res文件夹下,新建anim文件夹,新建两个文件:
pop_enter_anim:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
pop_exit_anim:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>