Question

I would like to know if there was a way to keep track of an animation using ObjectAnimator. Basically when you create the Activity I move a button and if I click on the button while it is in animation I want to arrive at the same point with a lower speed without the animation start again from 0. This is the code :

public class MainActivity extends Activity {



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

final Button prova1 = (Button)findViewById(R.id.prova); 

ObjectAnimator mover = ObjectAnimator.ofFloat(prova1, "translationY", 0, 1000);
mover.setDuration(2000);
mover.start();




prova1.setOnClickListener(new Button.OnClickListener(){
 @Override
public void onClick(View v) {
// TODO Auto-generated method stub
ObjectAnimator mover = ObjectAnimator.ofFloat(prova1, "translationY", "??", 1000);
mover.setDuration(4000);
mover.start();

}});


        }
    }
Was it helpful?

Solution

then you can try changing the second animation with the animation in xml and gates ObjectAnimator in the onClick method. example:

public class MainActivity extends Activity {



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

final Button prova1 = (Button)findViewById(R.id.prova); 
final Animation trans = AnimationUtils.loadAnimation(this, R.anim.trans);
ObjectAnimator mover = ObjectAnimator.ofFloat(prova1, "translationY", 0, 1000);
mover.setDuration(2000);
mover.start();




prova1.setOnClickListener(new Button.OnClickListener(){
 @Override
public void onClick(View v) {
// TODO Auto-generated method stub
 mover.cancel();
 prova1.startAnimation(trans);

}});


        }
    }

create xml code in your res->anim->trans :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <translate
       android:fromYDelta="0"
       android:toYDelta="2000"
       android:duration="20000"
       />
</set>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top