2012-06-21 29 views
2

我想爲在屏幕中心開始的ImageView創建動畫,並將其向上移動10px以下到屏幕頂部。我如何在代碼中實現這一點?Android - 從中​​間到頂部的翻譯動畫

我現在正在做的是,我將屏幕座標放在另一張圖像上,並將此動畫圖像置於其上,但在高密度大屏幕中,它不保留相同的距離,所以我想把它從頂部移動10px。

我該如何做到這一點?

這裏是我的代碼:

//calculate where to put the logo in y-axis depending on screen size 
      int coords[] = {0,0}; 
      logoImageFixed.getLocationOnScreen(coords); 
      int y = coords[1]; 
      int imgFixedHeight = logoImageFixed.getHeight(); 
      Log.d("daim","height: "+imgFixedHeight); 
      float pointY = -(y + 3*imgFixedHeight); 

      Animation a = new LogoAnimation(0.0f, 0.0f, 0.0f, pointY); 
      a.setFillAfter(true); 
      a.setDuration(600); 
      a.setAnimationListener(new AnimationListener() { 
       @Override 
       public void onAnimationStart(Animation animation) {} 
       @Override 
       public void onAnimationRepeat(Animation animation) {} 
       @Override 
       public void onAnimationEnd(Animation animation) { 
        logoAnimated = true; 
       } 
      }); 
      logoImage.startAnimation(a); 

回答

4

嗯,我想你在正確的道路是

int[] coords = {0,0}; 
logoImage.getLocationOnScreen(coords); 
int y = coords[1]; 

//to top of screen 
TranslateAnimation toTop = new TranslateAnimation(0, 0, 0, -y); 
toTop.setDuration(700); 
toTop.setFillAfter(true); 
logoImage.startAnimation(toTop); 

但如果你想添加填充10個像素,你需要做到這一點

//padding 10 pixels from top  
TranslateAnimation toTop = new TranslateAnimation(0, 0, 0, ((-y) - 10)); 
toTop.setDuration(700); 
toTop.setFillAfter(true); 
logoImage.startAnimation(toTop);