嘗試這樣的事情
@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();
}
};
舉例摘自:http://stackoverflow.com/questions/18029097/android-cant-get-one-textview-to-animate-to-the-position- of-another-textview – kinezu
謝謝。現在我正試圖理解這段代碼。什麼變量是「toLoc」? – Sid
這將是你的文字視圖,你正在移動你的作品。 – kinezu