Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)

时间:2023-02-09 08:48:47

话不多说,直接上代码:

AlertDialog.Builder builder = new Builder(HomeActivity.this);
View view = View.inflate(HomeActivity.this, R.layout.home_add_dialog, null);
btn_add_cancel = (Button) view.findViewById(R.id.btn_add_cancel);
builder.setView(view);
dialog = builder.create();
//消除dialog黑色边框
dialog.setView(view,0,0,0,0);
dialog.show();
//overridePendingTransition(R.anim.hor_tran_in, R.anim.hor_tran_out);
//设置大小
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.width = LayoutParams.MATCH_PARENT;
layoutParams.height = LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(layoutParams);
//设置dialog进入的动画效果
<span style="color:#33ff33;"> Window window = dialog.getWindow();
window.setWindowAnimations(R.style.mystyle);</span>

Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)

先在图片对应values/styles.xml中新建个style,名称为mystyle:

<style name="mystyle" parent="android:Animation">
<item name="@android:windowEnterAnimation">@anim/ver_tran_in</item>
<item name="@android:windowExitAnimation">@anim/ver_tran_out</item>
</style>

而其中的anim/ver_tran_in内容如下:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="80%p"
android:toYDelta="0" >

</translate>

anim/ver_tran_out内容:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="-100%p" >

</translate>