简体   繁体   English

DialogFragment中的动画视图

[英]Animate view in DialogFragment

Well I'm trying to animate a view in DialogFragment when is created and when is dismissed. 好吧,我正在尝试在DialogFragment中创建一个视图,并在何时被解除。 But I can figure it out, it's not working for me. 但我可以弄明白,它不适合我。 I've tried also all the answers pepole have gave about this here. 我也尝试过pepole在这里给出的所有答案。 I tried : onActivityCreate , setStyle(myCustom), oncreate etc. 我试过:onActivityCreate,setStyle(myCustom),oncreate等。

I have a custom DialogFragment view. 我有一个自定义DialogFragment视图。 and i want to animate it when it open and when it closed. 我想在它打开和关闭时为它制作动画。

Really need help here. 真的需要帮助。 Thanks ahead. 谢谢你。

here is my code : 这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     inflater = context.getLayoutInflater();
      view = inflater.inflate(R.layout.details_page, container, false);
     //Setting up the image by id
     ImageView img = (ImageView)view.findViewById(R.id.details_img);
     img.setImageResource(this.imageId);
     //Setting up the title
     TextView tittle = (TextView)view.findViewById(R.id.title);
     tittle.setText(this.title);
     tittle.setGravity(Gravity.RIGHT);
     //Setting up all the details about the item
     TextView details = (TextView)view.findViewById(R.id.details);
     details.setText(this.details);
     details.setGravity(Gravity.RIGHT);

     return view;


}

Refering to your comment: 参考你的评论:

I want to open the dialogfragment with animation like slide up and when the user go back also animate with slide down. 我想打开带有动画的dialogfragment,比如向上滑动,当用户返回时也可以向下滑动动画。

This might be a solution for you: 这可能是您的解决方案:

1) Create animation you like, ie.: 1)创建你喜欢的动画,即:

/res/anim/slide_up.xml /res/anim/slide_up.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromYDelta="100%"
        android:toYDelta="0%"
        android:interpolator="@android:anim/decelerate_interpolator" />

</set>

/res/anim/slide_down.xml /res/anim/slide_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:interpolator="@android:anim/decelerate_interpolator" />

</set>

2) Create dialog animation style in 2)创建对话框动画样式

/res/values/styles.xml /res/values/styles.xml

    <!-- [...] -->

    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
    </style>

</resources>

3) Attach style to your dialog: 3)将样式附加到对话框:

@Override
public void onActivityCreated(Bundle arg0) {
    super.onActivityCreated(arg0);
    getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}

4) Done. 4)完成。 Should work. 应该管用。

try this . and let me know the updates ...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View layout = inflater.inflate(R.layout.my_layout, null);

    layMain = (LinearLayout) layout.findViewById(R.id.layMain);

    TextView btnCancel = (TextView) layout.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                final Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.translate_to_bottom);
                anim.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        dismiss();
                    }
                });
                layMain.startAnimation(anim);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM