2012-06-21 3 views
2

Я хочу анимировать ImageView, который начинается в центре экрана и перемещает его вверх на 10 пикселей ниже верхней части экрана. Как я могу достичь этого в коде?Android - TranslateAnimation from middle to top

То, что я делаю сейчас, заключается в том, что я получаю экранные координаты на другом изображении и немного помещаю этот анимационный образ поверх него, но на больших экранах с высокой плотностью он не сохраняет такое же расстояние, поэтому вместо этого я хочу переместить его на 10 пикселей сверху.

Как я могу это достичь?

Вот мой код:

//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);