[英]How to animate a dialog to slide from the bottom of a screen to the middle without bounce?
我想膨胀一个视图,它将成为我的“无网络对话框”。 我创建了一个自定义布局,现在我想对其进行膨胀和显示。 我遇到的冲突是动画不起作用。 我希望对话框从屏幕底部滑入中间,然后在最后反弹。 这是我正在使用的。
样式文件
<!-- animation for network dialog -->
<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">@anim/slide_up_bounce</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
这是我的 slide_up_bounce.xml
<scale
android:duration="600"
android:fromXScale="1"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="600"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<translate
android:duration="600"
android:fromYDelta="50%"
android:toYDelta="0%" />
在实用程序类中,我有一个方法,看起来像
public static void networkDialog(Context context) {
final Dialog dialog = new Dialog(context, R.style.DialogAnimation);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_network, null);
dialog.setContentView(view);
ImageButton ibRefresh = (ImageButton) view.findViewById(R.id.ib_refresh);
}
目前,对话框立即显示,没有滑动或弹跳。 我该如何解决这个问题,或者我应该尝试采取不同的方法来实现这一目标? 提前致谢!
我想出了如何做到这一点。 与其使用 Dialog,不如使用 DialogFragment。 无论出于何种原因,我的 Dialog 实现都是正确的,它只是不适用于动画。 这是我的实现:
public class NetworkDialog extends DialogFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.dialog_network, container, false);
v.setAlpha(0.5f);
final ImageButton ibRefresh = (ImageButton) v.findViewById(R.id.ib_refresh);
ibRefresh.setClickable(true);
ibRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapActivity.rotateImageView(ibRefresh);
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
}
然后我调用对话框
NetworkDialog dialogFragment = new NetworkDialog();
dialogFragment.show(getFragmentManager(), "ProgressDialog");
一种解决方案是将样式作为Dialog
参数传递,如下所示:
Dialog confirm = new Dialog(context, R.style.BottomSheetDialog);
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.