Suppose D is an imageview and its move from 0 to 200 in x direction.
See below code.
public class DActivity extends Activity {
ImageView D;
int x=0,y=0;
int a=0;
int newx=0;
TranslateAnimation TA;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
D=(ImageView)findViewById(R.id.d);
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
D.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x=v.getLeft();
y=v.getTop();
Toast.makeText(getApplicationContext(), "X="+x+"y="+y, Toast.LENGTH_LONG).show();
if(x==0){
D.setEnabled(false);
a=200;
newx=200;
Anim();
}
if(x==200){
D.setEnabled(false);
a=-200;
newx=0;
Anim();
}
return true;
}
});
}
public void Anim(){
TranslateAnimation TAnimation=new TranslateAnimation(0, a, 0,0);
TAnimation.setInterpolator(new LinearInterpolator());
TAnimation.setDuration(5000);
TAnimation.setFillAfter(false);
TAnimation.setFillEnabled(true);
TAnimation.setFillBefore(true);
D.startAnimation(TAnimation);
TAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) { }
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
LayoutParams param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(newx, y, 0, 0);
D.setLayoutParams(param);
D.setEnabled(true);
}
});
}
}