2014-02-24 54 views
1

我想知道是否有方法使用ObjectAnimator跟蹤動畫。基本上,當你創建活動時,我移動一個按鈕,如果我點擊按鈕時,它是在動畫中,我想以較低的速度到達相同的點,而動畫不從0重新開始。這是代碼:ObjectAnimator getY()動畫進行中的座標android

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

然後,您可以嘗試使用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); 

}}); 


     } 
    } 

中創建XML代碼的水庫 - > anim->反式:

<?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> 
+0

yeahh工作謝謝,如果是有用 – ANDROID01

+0

接受我的答案 –

+0

yeahhhhh broooo你 – ANDROID01

相關問題