我正在按照在線教程進行按鈕更改位置(從左上角到右下角),同時增加尺寸。我正在使用transitionmanager進行此操作。Android按鈕不動?
TransitionManager.beginDelayedTransition(mylayout);
然後我想,如果我能做到從頂端左側還右下角的動畫(這不包括在本教程中,我只嘗試了這一點純粹的好奇心)。我創建稱爲
moveButtonBack();
X變量得到由每一個或moveButton moveButtonBack()被調用時遞增的另一種方法,和moveButton被調用,如果x是如果x爲奇數偶數而moveButtonBack被調用。順便說一下,我在代碼中添加了很多註釋以跟蹤事情。要將按鈕大小更改回moveButtonBack,我使用變量H和W來顯示按鈕的第一個高度和寬度。
問題
爲了我的代碼似乎罰款,我不能找出問題所在。當我在手機上運行該按鈕時,該按鈕會變得瘋狂。文本在按鈕上快速變化,它以無組織的方式移動,或者當我觸摸時移動到中心,當我放開時移動到中心,或者它停留在中心並且看起來好像振動快速上下移動)。它具有恆定的高度300和寬度450,而不是縮小和放大。我不知道問題是什麼,如果我得到了幫助,我會很感激。我花了一個多小時在代碼中插入註釋,並試圖通過改變之類的東西聽衆的高度,以解決這一問題,等
package com.example.my.transitions;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.*;
import android.transition.*;
import android.support.v7.app.*;
public class MainActivity extends AppCompatActivity {
ViewGroup myslayout;
View mysButton; //Used in move and moveback
int x = 0;
double h;
double w;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myslayout = (ViewGroup) findViewById(R.id.myslayoutid);
myslayout.setOnTouchListener(
new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (x%2 == 0) {
moveButton();
return true;
}
else{
moveButtonBack();
return true;
}
return true; //touch listener as been handled
}
}
);
}
public void moveButton() {
Button mysButton = (Button) findViewById(R.id.mysbuttonid);
h = mysButton.getHeight();
w = mysButton.getWidth();
TransitionManager.beginDelayedTransition(myslayout);
//////////////////////Change position of button////////////////////////////
RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_BOTTOM);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.ALIGN_PARENT_RIGHT);
mysButton.setLayoutParams(positionRules);
/////////////////////////Change size of button////////////////////////////
ViewGroup.LayoutParams sizerules = mysButton.getLayoutParams();
sizerules.width = 450;
sizerules.height = 300;
mysButton.setLayoutParams(sizerules);
mysButton.setText("Height: " + sizerules.height + "Width: " + sizerules.width);
x++; //Make x odd, to be called as moveButtonBack next time
}
public void moveButtonBack(){
Button mysButton =(Button) findViewById(R.id.mysbuttonid);
TransitionManager.beginDelayedTransition(myslayout);
RelativeLayout.LayoutParams positionBackRules = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_TOP);
positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.ALIGN_PARENT_LEFT); //Move back to top left
mysButton.setLayoutParams(positionBackRules);
ViewGroup.LayoutParams sizebackrules = mysButton.getLayoutParams();
sizebackrules.height = (int) h;
sizebackrules.width = (int) w;
//I am typcasting h and w into ints in case it is a double, since .width and .height only take ints.
mysButton.setLayoutParams(sizebackrules);
mysButton.setText("Height: " + sizebackrules.height + "Width: " + sizebackrules.width + "h " + h + "w" + w);
x++; //Make x even again
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
同樣,我會很感激的答案,因爲我一直在這個問題上現在幾個小時!
您是否使用'x'來維持當前位置?另外onTouchListener被多次調用 - 觸摸並修改。因此'x'在奇數和偶數之間交替。 – ntsh
@ntsh是的,我使用x來維持當前位置。基本上,每當按鈕向下移動並放大時,x變爲奇數,並且每次它向上移動並收縮時,x變爲偶數。另外,我應該做些什麼來使onTouchListener只被調用一次,因爲我懷疑這可能是問題所在。我很抱歉遲到的迴應,我真的很感激你的時間。 –