1

好的,从外观上看,这应该是动画中最容易做的事情。我必须执行以下操作:我放置了 2 个 TextViews 和屏幕的随机位置。(例如,第一个 TextView 在左上角 (0,0)),另一个 TextView 在右下角(width_of_screen,height_of_screen)。

现在,当我单击底部 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 永远不会动画到顶部。我真的可以在这方面使用一些帮助。谢谢。

4

1 回答 1

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);
于 2013-08-03T04:10:17.813 回答