简体   繁体   中英

Translate(Slide in) animation not working android

In my app what i want is that on button click i wanna slide in a fragment and on again on click slide out the fragment.My code for sliding the fragment down is working but the sliding in transition is not working..

Slide Up

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <objectAnimator
        android:interpolator="@android:anim/linear_interpolator"
        android:propertyName="getXFraction"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="1000"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <objectAnimator
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="alpha"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="1.0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

Slide down

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="translationY"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="1280"
        android:duration="1000"/>

Code

private void toggleList() {
        Fragment f = getFragmentManager()
                .findFragmentByTag(LIST_FRAGMENT_TAG);
        if (f != null) {
            getFragmentManager().popBackStack();
        } else {
            getFragmentManager().beginTransaction()
                    .setCustomAnimations(R.animator.slide_up,
                            R.animator.slide_down,
                            R.animator.slide_up,
                            R.animator.slide_down)
                    .add(R.id.frag_container, new frag(),
                            LIST_FRAGMENT_TAG
                    ).addToBackStack(null).commit();
        }
    }

As you're using xFraction (in my code, it's just xFraction not getXFraction ), I believe you only need to animate between 0 and 1 (the unit is screen width). Setting the latter to 100 results in moving object 100x the screen width. Slide down is working (but only for a specific screen size) because in that case you are using yTranslate , for which the units are pixels. In that case, you'd be better to use yFraction set from 0 to 1. As others have commented, if all else fails, test slide in as a solo animation before adding it to a set.

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