2
下面的代碼應該是:Android動畫和轉換
當我觸摸屏幕時,按鈕從左上角移動到右下角。如果我再次觸摸屏幕,則按鈕將從右下移到左上。
但是,當我觸摸屏幕時,按鈕開始從TL移動到BR,但隨後它跳回到TL角落!
我長時間按着屏幕的時間越長,按鈕就會向BL角落「移動」。 這是爲什麼?我第一次觸摸屏幕時,爲了讓按鈕在一個方向上移動,我必須改變什麼,而在第二次觸摸屏幕時,我必須改變其他方向?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
state = false;
myLayout = (ViewGroup)findViewById(R.id.main_layout);
myLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
moveButton();
return true;
}
});
}
public void moveButton() {
View theButton = findViewById(R.id.button1);
TransitionManager.beginDelayedTransition(myLayout); //for smooth transitioning
//Change position
RelativeLayout.LayoutParams posRules = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
if (state) {
posRules.addRule(RelativeLayout.ALIGN_PARENT_TOP);
posRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
else {
posRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
posRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
theButton.setLayoutParams(posRules);
//Change size
ViewGroup.LayoutParams sizeRules = theButton.getLayoutParams();
if (state) {
sizeRules.width = 250;
sizeRules.height = 100;
}
else {
sizeRules.width = 450;
sizeRules.height = 300;
}
theButton.setLayoutParams(sizeRules);
state = !state;
}
謝謝你,解決了它。 – yaronkl