Does not play an animation on Android

2

I'm trying to make an animation for an image, but the problem is that the animation is not running, does not show any errors, put my image I want to animate in a class that extends Fragment .

This is my class that extends from Fragment:

public class animacionesFrgmento extends Fragment {

    public FloatingActionButton reproductor;

    public  animacionesFrgmento(){

    }

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

        View view = inflater.inflate(R.layout.activity_animaciones, container, false);
        reproductor = (FloatingActionButton) view.findViewById(R.id.floatingActionButton);
        reproductor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlayAnimation(view);
            }
        });
        return view;
    }

    public void PlayAnimation(View view){

        Toast.makeText(getActivity(), "Reproduciendo animacion", Toast.LENGTH_LONG).show();
        ImageView esfera = (ImageView)view.findViewById(R.id.esfera);
        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.animation_esfera);
        animatorSet.setTarget(esfera);
        animatorSet.start();

    }
}

This is my XML animation file, it's in a folder called "animator"

<?xml version="1.0" encoding="UTF-8"?>
<set android:ordering="together" xmlns:android="http://schemas.android.com/apk/res/android">

    <objectAnimator
        android:propertyName="x"
        android:duration="400"
        android:valueTo="500"/>

    <objectAnimator
        android:propertyName="y"
        android:duration="400"
        android:valueTo="500"/>

</set>

This is my file where I have my floating button and my ImageView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView
        android:id="@+id/esfera"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="12dp"
        android:src="@drawable/esfera" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:clickable="true"
        app:srcCompat="@android:drawable/ic_media_play" />

</RelativeLayout>
  

I am printing a Toast in my method where I try to execute the   animation and the message if it is shown, but the animation does nothing,   the emulator where I run the project is an emulator with API 25

    
asked by Enrique Espinosa 01.06.2018 в 22:28
source

1 answer

2

From what I see in your code you are not finding the reference of ImageView you are only inflating the Layout , for this you must declare a variable ImageView as you did with FloatingActionButton and then reference it within onCreateView .

Code:

public class animacionesFrgmento extends Fragment {

    public FloatingActionButton reproductor;
    public ImageView esfera;
    public  animacionesFrgmento(){

    }

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

        View view = inflater.inflate(R.layout.activity_animaciones, container, false);
esfera = (ImageView)view.findViewById(R.id.esfera);
        reproductor = (FloatingActionButton) view.findViewById(R.id.floatingActionButton);
        reproductor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlayAnimation(view);
            }
        });
        return view;
    }

    public void PlayAnimation(View view){

        Toast.makeText(getActivity(), "Reproduciendo animacion", Toast.LENGTH_LONG).show();

        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.animation_esfera);
        animatorSet.setTarget(esfera);
        animatorSet.start();

    }
}
    
answered by 02.06.2018 / 00:31
source