Trying to set a simple animation of the change of fragments. When placing the xml - animation files in the / anim / folder / throws an exception:
Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator If you place the animation files in the / animator / folder, the setCustomAnimation () method scolds and says that the animation resource from the / anim / folder is needed. Tell me how you still need to make it work.
File R.anim.slide_in_left.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="1500" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="y" android:valueFrom="-1280" android:valueTo="0" android:valueType="floatType"/> </set> File R.anim.slide_in_right.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="300" android:interpolator="@android:anim/accelerate_interpolator" android:propertyName="alpha" android:valueTo="0" android:valueType="floatType"/> <objectAnimator android:duration="1500" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="x" android:valueFrom="0" android:valueTo="1280" android:valueType="floatType"/> </set> The mainActivity.java file:
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Fragment1 f1; Fragment2 f2; FragmentTransaction fm; @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); f1 = new Fragment1(); f2 = new Fragment2(); fm = getSupportFragmentManager().beginTransaction(); fm.setCustomAnimations( R.anim.slide_in_left, R.anim.slide_in_right ); fm.replace( R.id.fragCont, f1 ); fm.addToBackStack( null ); fm.commit(); Button btn = ( Button ) findViewById( R.id.btn ); btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick( View v ) { fm = getSupportFragmentManager().beginTransaction(); fm.setCustomAnimations( R.anim.slide_in_left, R.anim.slide_in_right ); if ( f1.isVisible() ) { fm.replace( R.id.fragCont, f2 ); } else { fm.replace( R.id.fragCont, f1 ); } fm.commit(); } } ); } }