2012-02-09 102 views
2

我試圖做一個動畫,點擊右鍵從幻燈片的右側向右滑動圖片,然後點擊左鍵從幻燈片左側的位置滑動圖片。動畫形象,並在最後停止?

我到目前爲止已經做到了這一點:

public class DenemeActivity extends Activity implements View.OnClickListener { 

Button LeftSlide, RightSlide; 
View image; 
int width; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LeftSlide = (Button) findViewById(R.id.slideLeft); 
    RightSlide = (Button) findViewById(R.id.slideRight); 
    image = (View) findViewById(R.id.imageV); 
    width = getWindowManager().getDefaultDisplay().getWidth(); ; 
    LeftSlide.setOnClickListener(this); 
    RightSlide.setOnClickListener(this);  

} 

public void onClick(View v) { 
    runOnAnimation(v.getId(), image); 
} 

void runOnAnimation(final int id, final View view) { 

    AnimationSet animation = new AnimationSet(true); 
    TranslateAnimation translateAnimation = null; 

    int fromXDelta = view.getLeft(); 

    if(id == LeftSlide.getId()) 
     translateAnimation = new TranslateAnimation(fromXDelta, 0, 0, 0); 
    else if(id == RightSlide.getId()) 
     translateAnimation = new TranslateAnimation(fromXDelta, width - view.getWidth() - fromXDelta , 0, 0); 

    translateAnimation.setDuration(1000); 
    animation.addAnimation(translateAnimation); 
    animation.setAnimationListener(new AnimationListener() { 

     public void onAnimationEnd(Animation v) { 
      v.reset(); 
      view.clearAnimation(); 

      // Change view to state B by modifying its layout params and scroll 
      if(id == LeftSlide.getId()) 
       view.layout(0, 0, view.getWidth(), view.getHeight()); 
      else if(id == RightSlide.getId()) 
       view.layout(width - view.getWidth(), 0, width, view.getHeight());    
     } 

     public void onAnimationRepeat(Animation v) {} 
     public void onAnimationStart(Animation v) {} 
    }); 
    view.startAnimation(animation); 
}  
} 

我檢查點:

寬度= 480

fromXDelta = 408(當點擊左)

所以它確實沒有超出邊界,但是當我點擊左鍵時,圖像來自邊界以外?這會對此產生什麼影響? 在此先感謝...

回答

1

我想你得到了錯誤的位置類型爲您想要移動的視圖。 嘗試讓你查看的位置,實施翻譯如下:

int fromPos = view.getLeft(); 
int toPos = 0; 
if(id == LeftSlide.getId()) { 
    translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0); 
} else { 
    toPos = getWidth()-view.getWidth(); 
    translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0); 
} 

這應該做的伎倆。

+0

做過同樣的事情...來自外部:/ – yahya 2012-02-09 12:32:54

+0

試圖評論onAnimationEnd()函數嗎?註釋代碼時會發生什麼情況,因此沒有設置佈局參數? – Andreas 2012-02-09 12:38:09

+0

這是爲了讓圖像出現,因爲在view.clearAnimation之後圖像消失了。所以如果沒有設置佈局參數,它根本就不工作。 – yahya 2012-02-09 12:41:19