简体   繁体   中英

Animate view in DialogFragment

Well I'm trying to animate a view in DialogFragment when is created and when is dismissed. 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. I tried : onActivityCreate , setStyle(myCustom), oncreate etc.

I have a custom DialogFragment view. 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.

This might be a solution for you:

1) Create animation you like, ie.:

/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

<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

/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:

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

4) Done. 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);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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