最近在体验path和知趣天气的时候,对话框都是采用向下掉落的方法弹出,让人觉得非常喜欢,新颖,于是乎也自己做了一个这样的效果,废话少说,直接上图:
为什么在编辑状态下看不到图片的gif效果嘞!!!
靠,csnd的服务器果然抽风了,我在G+上也上传了一张,传送门
好了,现在开始进入正题,如何实现的,实现方法倒是挺简单的
第一步:在xml中定义动画效果,一共两个,进入的动画跟出去的动画:
dialog_anim_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/accelerate_interpolator" android:fromYDelta="-50%p" android:toYDelta="0%" android:duration="300" /> <rotate android:fromDegrees="0" android:toDegrees="2" android:startOffset="290" android:duration="20" android:pivotX="50%" android:pivotY="50%" /> <rotate android:fromDegrees="2" android:toDegrees="0" android:startOffset="310" android:duration="20" android:pivotX="50%" android:pivotY="50%" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/decelerate_interpolator" android:fromYDelta="0%" android:toYDelta="50%p" android:duration="200" android:startOffset="200" /> <rotate android:fromDegrees="0" android:toDegrees="5" android:duration="200" android:pivotX="50%" android:pivotY="50%" /> </set>
SlidingInAndOutDialogFragment
public class SlidingInAndOutDialogFragment extends DialogFragment{ private Button button; @Override public void onStart() { super.onStart(); // safety check if (getDialog() == null) { return; } // set the animations to use on showing and hiding the dialog getDialog().getWindow().setWindowAnimations( R.style.DialogAnimation); // alternative way of doing it //getDialog().getWindow().getAttributes(). // windowAnimations = R.style.dialog_animation_fade; // ... other stuff you want to do in your onStart() method // setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); View v = inflater.inflate(R.layout.custom_dialog, null); button = (Button) v.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i("debug", "click"); } }); return v; } }
<style name="DialogAnimation"> <item name="android:windowEnterAnimation">@anim/dialog_anim_in</item> <item name="android:windowExitAnimation">@anim/dialog_anim_out</item> </style>
void showDialog() { // DialogFragment.show() will take care of adding the fragment // in a transaction. We also want to remove any currently showing // dialog, so make our own transaction and take care of that here. FragmentTransaction ft = getFragmentManager().beginTransaction(); Fragment prev = getFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); // ft.setCustomAnimations(R.anim.fragment_slide_left_enter, 0); // Create and show the dialog. newFragment = new SlidingInAndOutDialogFragment(); newFragment.show(ft, "dialog"); }