I am trying to make an animation for an image, but the problem is that the animation is not running, it does not show any error, I put my image that I want to animate in a class that extends from 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 is 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 is displayed, but the animation does nothing, the emulator where I run the project is an emulator with API 25
From what I see in your code, it is not finding the reference of
ImageView
it is only inflating theLayout
, for this you must declare a variableImageView
as you did withFloatingActionButton
and then reference it inside theonCreateView
.Code: