我正在使用animate()
以下代码将我的 TextView 发送到布局的最右下角的方法:
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);
}
});
这可以解决问题,但是我在将元素恢复到原始位置时有点迷失了。我确实尝试了以下方法,但没有奏效:
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);;
}
});
}
}
以下为我提供0,0
了X and Y
. 请帮忙。