自定义Dialog 并且设置Dialog的进入和退出的动画效果

时间:2023-02-09 09:06:59
public class BottomDialog extends Dialog {

/**
*<li> 构造函数 </li>
* @param context
* @param view 供显示的View
* @param cancel 点击外部区域是否可取消
*/
@SuppressWarnings("deprecation")
public BottomDialog(Context context, View view, boolean cancel) {
super(context, R.style.dialog);
setContentView(view);

setCanceledOnTouchOutside(cancel);
Window window = getWindow();
window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
window.setWindowAnimations(R.style.DialogBottom); // 添加动画

LayoutParams params = view.getLayoutParams();
if (params == null) {
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
params.width = window.getWindowManager().getDefaultDisplay().getWidth();
view.setLayoutParams(params);
}


}


    <style name="DialogBottom">
<item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="@android:windowExitAnimation">@anim/dialog_exit</item>
</style>

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="50%p"
android:duration="200"
/>
</set>


<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="50%p"
android:duration="200"
/>
</set>