Android Cant get One TextView to animate to the position of another TextView

时间秒杀一切 提交于 2019-12-08 07:19:06

问题


Ok by the looks of it, this should be the easiest thing to do when it comes to animations. I have to do the following: I have place 2 TextViews and Random position of the screen. (For eg the first TextView is at top-left (0,0)) and the other TextView is at bottom right(width_of_screen, height_of_screen).

Now when I click on the bottom textView I want it to animate to the position of the top left textview. My Code is the following:

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

The animations are weird. The bottom textView never animates to the top one. I really could use some help in this. Thanks.


回答1:


Ok I found the error in my code. If anyone is having problems then use this :

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

instead of

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


来源:https://stackoverflow.com/questions/18029097/android-cant-get-one-textview-to-animate-to-the-position-of-another-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!