Android无法将一个TextView设置为另一个TextView的动画位置

Android无法将一个TextView设置为另一个TextView的动画位置,android,animation,textview,translation,Android,Animation,Textview,Translation,从外观上看,这应该是动画中最容易做到的事情。我必须做到以下几点: 我有两个文本视图和屏幕的随机位置。(例如,第一个文本视图位于左上角(0,0)),另一个文本视图位于右下角(屏幕的宽度、高度) 现在,当我点击底部文本视图时,我想让它动画到左上文本视图的位置。我的代码如下: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCont

从外观上看,这应该是动画中最容易做到的事情。我必须做到以下几点: 我有两个文本视图和屏幕的随机位置。(例如,第一个文本视图位于左上角(0,0)),另一个文本视图位于右下角(屏幕的宽度、高度)

现在,当我点击底部文本视图时,我想让它动画到左上文本视图的位置。我的代码如下:

@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();       
    }
};

动画很奇怪。底部文本视图从不设置为顶部文本视图的动画。我真的需要一些帮助。谢谢

好的,我在代码中找到了错误。如果任何人有问题,请使用以下方法:

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

好的,我在代码中找到了错误。如果任何人有问题,请使用以下方法:

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