2013-08-03 41 views
1

好吧,它看起来应该是最简单的动画。我必须做到以下几点: 我有2个TextViews和随机位置的屏幕。 (例如,第一个TextView位于左上角(0,0)),另一个TextView位于右下角(width_of_screen,height_of_screen)。安卓不能让一个TextView动画到另一个TextView的位置

现在当我点击底部textView时,我想让它动画到左上方textview的位置。我的代码如下:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtOne = (TextView)findViewById(R.id.textone); 
    txtTwo = (TextView)findViewById(R.id.text_two); 
    relative = (RelativeLayout)findViewById(R.id.relative); 


    txtTwo.setOnClickListener(new View.OnClickListener() { 

     @SuppressLint("NewApi") 
     @Override 
     public void onClick(View v) { 



      float to_x = txtOne.getX(); 
      float to_y = txtOne.getY(); 


      float x_from = txtTwo.getX(); 
      float y_from = txtTwo.getY(); 
      //txtTwo.getLocationInWindow(fromLoc); 
      txtOne.getLocationOnScreen(toLoc); 
      Animations anim = new Animations(); 

      Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000); 
      txtTwo.startAnimation(a); 

     } 
    }); 
} 



public class Animations { 
    public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){ 


     Animation fromAtoB = new TranslateAnimation(

       fromX, 

       toX, 

       fromY, 

       toY 
       ); 

     fromAtoB.setDuration(speed); 
     //fromAtoB.setInterpolator(new); 


     if(l != null) 
      fromAtoB.setAnimationListener(l);    
     return fromAtoB; 
    } 
} 



AnimationListener animL = new AnimationListener() { 

    @Override 
    public void onAnimationStart(Animation animation) {  
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) {   
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     //this is just a method to delete the ImageView and hide the animation Layout until we need it again. 
     //clearAnimation();  
    } 
}; 

动画很奇怪。底部的textView永远不会动画到顶部。我真的可以在这方面使用一些帮助。谢谢。

回答

1

好的我在代码中发现错误。如果有人有问题,然后用这个:

Animation a = anim.fromAtoB(0, 0, to_x-x_from, to_y - y_from, animL,1000); 

,而不是

Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000); 
相关问题