user820913 user820913 - 3 months ago 8
Android Question

Using Property animation in android

I am trying to learn Property animation in Android, but I canĀ“t get my code to work. The idea is to make two animations happen at the same time. I want an image of a football to rotate and translate at the same time. Unfortunately the app crashes in my emulator. Android studio complains that it cannot execute the "onclick- command" in the xml file. What is wrong here?

First the Xml-code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.runeasheim.codeplayingpropertyanimationjava.MainActivity"
android:background="#CCCCFF">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/footballround"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animation"
android:id="@+id/btn1"
android:onClick="setAnimation"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />




and here the Java code:

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
private ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.imageView);

}

public void setAnimation(View view){
AnimatorSet rootSetAnim = new AnimatorSet();

ObjectAnimator rotateBall = ObjectAnimator.ofFloat(image, "rotationY", 0.0f, 360.0f);
rotateBall.setDuration(2000);

AnimatorSet childSetAnim = new AnimatorSet();

ObjectAnimator translateBall = ObjectAnimator.ofFloat(image, "translationX, 0f, 300f");
translateBall.setDuration(2000);

rootSetAnim.play(rotateBall).before(childSetAnim);
childSetAnim.play(translateBall);
rootSetAnim.start();

}


}

Answer

Change from

ObjectAnimator translateBall = ObjectAnimator.ofFloat(image, "translationX, 0f, 300f");

to

ObjectAnimator translateBall = ObjectAnimator.ofFloat(image, "translationX", 0f, 300f);