You want to translate imageTwo
to imageThree
, am I right?
If so, the parameters you pass to TranslateAnimation's
constructor are wrong.
Here are the four parameters:
float fromXDelta ==> starting xValue RELATIVE to the view
-> ZERO in your case // imageTwo starts from where it is
float toXDelta ==> final xValue RELATIVE to the view
-> (destinationX - sourceX) in your case // imageTwo travels this much in X
float fromYDelta ==> starting yValue RELATIVE to the view
-> ZERO in your case // imageTwo starts from where it is
float toYDelta ==> final yValue RELATIVE to the view
-> (destinationY - sourceY) in your case // imageTwo travels this much in Y
Modified case for R.id.imageView2
:
case R.id.imageView2 :
transAnimation= new TranslateAnimation(0f,
(destinationX - sourceX),
0f,
(destinationY - sourceY));
transAnimation.setDuration(3000);
v.startAnimation(transAnimation);
break;
If you want imageTwo
to 'stay' after the animation, attach an AnimationListener
to transAnimation
and change the LayoutParams
for imageTwo
accordingly in onAnimationEnd
.
One more thing: Look at the comments to this(Link) answer. Are you sure you're getting valid values for positions? A safe thing to do would be to call readLocation(String str)
from onClick(View)
:
@Override
public void onClick(View v) {
readLocation("onClick");
switch(v.getId()) {
....
....
}
}
And, locationOnScreen(int[])
is enough for your purpose. locationInWindow(int[])
is not required.