我想知道是否有办法使用ObjectAnimator跟踪动画。基本上当你创建活动时,我移动一个按钮,如果我在动画中点击按钮,我想以较低的速度到达同一点,而不再从0开始动画。这是代码:
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();
}});
}
}
答案 0 :(得分:0)
然后您可以尝试使用xml中的动画更改第二个动画,并在onClick方法中使用ObjectAnimator。例如:
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);
}});
}
}
在res-> anim-> trans:
中创建xml代码<?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>