步骤:
- 在res下新建anim文件夹
- 在文件夹里新建pop_in.xml和pop_out.xml文件
- 在style文件中配置动画
- 在代码中引用
代码:
pop_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration = "1000"
android:fromYDelta="100%p"
/>
</set>
pop_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration = "1000"
android:toYDelta="50%p"
/>
</set>
style:
<style name="pop_anim">
<!--进入动画-->
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<!--退出动画-->
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
activity中调用:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopWindow();
}
});
}
private void showPopWindow() {
View inflate = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
popupWindow.setFocusable(true);
popupWindow.setAnimationStyle(R.style.pop_anim);
popupWindow.showAtLocation(findViewById(R.id.start), Gravity.BOTTOM,0,0);
}
}