Friday, 27 September 2013

Animating child back to original position

Animating child back to original position

I am using the animate() method to send my TextView to the
bottom-most-right of the layout using the following code:
move.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int xValue = container.getWidth() - myView.getWidth();
int yValue = container.getHeight() - myView.getHeight();
myView.animate().x(xValue).y(yValue);
}
});
This does the trick but I am sort of lost on restoring the element back to
its original position. I did try the following, but it didn't work:
public class MainActivity extends Activity {
Button fadeIn, fadeOut, move, moveBack;
TextView myView;
LinearLayout container;
float originalX , originalY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fadeIn = (Button)findViewById(R.id.fadeIn);
fadeOut = (Button)findViewById(R.id.fadeOut);
move = (Button)findViewById(R.id.move);
moveBack = (Button)findViewById(R.id.moveBack);
myView = (TextView)findViewById(R.id.myView);
originalX = myView.getX();
originalY = myView.getY();
Log.i("X and Y", ""+originalX+" "+originalY);
container = (LinearLayout) findViewById(R.id.container);
fadeIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myView.animate().alpha(1);
}
});
fadeOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myView.animate().alpha(0);
}
});
move.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int xValue = container.getWidth() - myView.getWidth();
int yValue = container.getHeight() - myView.getHeight();
myView.animate().x(xValue).y(yValue);
}
});
moveBack.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myView.animate().x(originalX).y(originalY);;
}
});
}
}
The following gives me with the 0,0 position on X and Y. Help please.

No comments:

Post a Comment