Tuesday, July 7, 2015

How to set start delay of an ObjectAnimator animation in Android

MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.animation.ObjectAnimator;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);
        final TextView tv = (TextView) findViewById(R.id.tv);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Move the object at the X position 300
                ObjectAnimator anim = ObjectAnimator.ofFloat(tv,"x",300);
                anim.setDuration(3000);
                anim.start();

                // Move the object at the Y position 400
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(tv,"y",400);
                anim2.setDuration(3000);
                anim2.setStartDelay(3000);
                anim2.start();
            }
        });
   }
}
More android examples