Android的不能让一个TextView的动画到其他的TextView的位置 [英] Android Cant get One TextView to animate to the position of another TextView
问题描述
这是它的外观好了,这应该是当它涉及到动画最容易做的事情。我必须做到以下几点:
我有地方2 TextViews和屏幕的任意位置。 (有关例如第一TextView的是在左上角(0,0))和其他的TextView是右下(width_of_screen,height_of_screen)
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).
现在,当我点击底部的TextView我希望它向动画左上角的TextView的位置。我的code是以下内容:
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();
}
};
该动画是怪异。底部的TextView从未动画顶端之一。我真的可以使用一些帮助在这。谢谢。
The animations are weird. The bottom textView never animates to the top one. I really could use some help in this. Thanks.
推荐答案
好吧,我发现在我的code中的错误。如果有人有问题,那么使用这个:
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);
而不是
Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000);
这篇关于Android的不能让一个TextView的动画到其他的TextView的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!