【问题标题】:Android Cant get One TextView to animate to the position of another TextViewAndroid 无法让一个 TextView 动画到另一个 TextView 的位置
【发布时间】:2015-03-31 15:19:35
【问题描述】:

好吧,从外观上看,这应该是动画最容易做到的事情。我必须执行以下操作: 我有 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 永远不会动画到顶部。我真的可以在这方面使用一些帮助。谢谢。

【问题讨论】:

    标签: android animation textview translation


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多