步驟:
- 在res下新建anim文件夾
- 在文件夾里新建pop_in.xml和pop_out.xml文件
- 在style文件中配置動(dòng)畫(huà)
- 在代碼中引用
代碼:
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">
<!--進(jìn)入動(dòng)畫(huà)-->
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<!--退出動(dòng)畫(huà)-->
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
activity中調(diào)用:
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);
// 設(shè)置popWindow彈出窗體可點(diǎn)擊,這句話必須添加,并且是true
popupWindow.setFocusable(true);
popupWindow.setAnimationStyle(R.style.pop_anim);
popupWindow.showAtLocation(findViewById(R.id.start), Gravity.BOTTOM,0,0);
}
}