给底部的对话框添加移动动画效果
可通过Window.setWindowAnimations(int resId)设置
SharingDialog.java
package com.bu_ish.sharing_dialog_example; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.Toast; public class SharingDialog extends Dialog { public SharingDialog(Context context) { super(context, R.style.SharingDialog); setCanceledOnTouchOutside(false); setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { Toast.makeText(getContext(), "已取消", Toast.LENGTH_SHORT).show(); } }); getWindow().setWindowAnimations(R.style.SharingDialogAnimation); } @Override public void show() { super.show(); WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.gravity = Gravity.BOTTOM; getWindow().setAttributes(layoutParams); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_sharing); findViewById(R.id.cancelTextView).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancel(); } }); } }
动画style
<style name="SharingDialogAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/sharing_dialog_show_animation</item> <item name="android:windowExitAnimation">@anim/sharing_dialog_dismiss_animation</item> </style>
对话框显示动画
sharing_dialog_show_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="100%p" android:toYDelta="0" /> </set>
对话框关闭动画
sharing_dialog_dismiss_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="0" android:toYDelta="100%p" /> </set>
P.S.
translate用于设置移动动画效果
完整Demo链接:https://pan.baidu.com/s/1hnFYo8i3SId8e3Ag0jKM3w,提取码:wgcr
SharingDialog