2011-11-19 70 views
2

我嘗試在xml文件中定義動畫,但它沒有提供足夠的靈活性。關於如何讓它在代碼中工作的任何想法。TranslateAnimation - 開始動畫時圖像消失

我的圖像消失,然後在動畫開始時重新出現。實質上,當我擡起手指時,我需要圖像作爲慢速滑動回到其原始位置。這不是由於某種原因發生的。

Java代碼:

chargeButton.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     int x_cord = (int) event.getRawX(); 
     int y_cord = (int) event.getRawY(); 

     switch (event.getActionMasked()){ 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       chargeButton.setLayoutParams(setPosition(x_cord, false)); 
       break; 
      case MotionEvent.ACTION_UP: 
       int x_start = x_cord; 
       x_cord = 0; 
       slowMove(x_start, x_cord, false); 

       break; 
      default: 
       break; 
     } 
     return true; 
    } 
}); 


} 

public void slowMove(int x_start, int x_final, boolean pay) 
{ 
Animation transAnimation = new TranslateAnimation(x_start, x_final, 0, 0); 
transAnimation.setFillAfter(true); 
transAnimation.setDuration(1000); 
if (pay) 
    payButton.startAnimation(transAnimation); 
else 
{ 
    chargeButton.clearAnimation(); 
    chargeButton.startAnimation(transAnimation); 
} 
} 

我真的很感激所有幫助我能。

回答

1

我希望能夠在我的代碼本身中更改fromXdelta。

爲什麼不使用constructor本身?

public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 

fromXDelta Change in X coordinate to apply at the start of the animation 
toXDelta Change in X coordinate to apply at the end of the animation 
fromYDelta Change in Y coordinate to apply at the start of the animation 
toYDelta Change in Y coordinate to apply at the end of the animation 
+0

使用構造將是完美的。但是,當我嘗試使用構造函數時,我的imageView完全消失。我不知道爲什麼會發生這種情況。 我輸入x_start,x_final,y_start和y_final作爲整數。這很重要嗎?你知道爲什麼會發生這種情況嗎?你有沒有通過使用視圖的絕對定位的構造函數來處理translateImage的代碼? – roro

+0

int應該很重要。我無法說出爲什麼你的圖像消失,直到我看到你的代碼。這裏是一個[工作示例](http://iserveandroid.blogspot.com/2010/12/slide-up-down-translate.html) – Reno

+0

嘿裏諾..我已經更新了我的問題,包括我的代碼。任何想法爲什麼圖像消失? – roro