我正在为游戏创建一个简单的菜单。当玩家点击一个选项时,选项列表移动到一侧,为子菜单腾出空间淡入。
gameModeChoose
包含在选择选项后移动到一侧的选项,subMenu
是淡入的子菜单(以编程方式创建)。要将子菜单右侧的选项列表对齐,我正在使用subMenuParams.addRule(RelativeLayout.RIGHT_OF,R.id.game_mode_choose)
和mainFrame.addView(subMenu,subMenuParams)
,但是这会将子菜单与之前的选项列表对齐/ strong>它移动为子菜单腾出空间。我怀疑问题可能正在发生,因为我正在使用gameModeChoose.animate().translationXBy(...)
移动选项列表,但我不知道如何调试它。我在选项列表移动后尝试使用addRule(...)
,并尝试使用subMenu.setLayoutParams(...)
尝试更新视图,但这些都不起作用。
我描述的问题发生在我发表评论的onAnimationEnd(Animator animation)
方法中。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Button btn1plyr;
Button btn2plyr;
RelativeLayout gameModeChoose;
RelativeLayout subMenu;
RelativeLayout.LayoutParams subMenuParams;
int menuState;
ImageButton circle;
ImageButton cross;
TextView hint;
RelativeLayout mainFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuState = 0;
btn1plyr = (Button) findViewById(R.id.btn1Plyr);
btn2plyr = (Button) findViewById(R.id.btn2Plyr);
gameModeChoose = (RelativeLayout) findViewById(R.id.game_mode_choose);
hint = (TextView) findViewById(R.id.menu_hint);
subMenu = new RelativeLayout (this);
subMenuParams = new RelativeLayout.LayoutParams(0,0);
circle = new ImageButton(this);
cross = new ImageButton(this);
mainFrame = (RelativeLayout) findViewById(R.id.activity_main);
subMenu.setAlpha(0);
subMenu.setBackground(getResources().getDrawable(R.drawable.title_border));
subMenuParams.leftMargin = 10;
subMenuParams.addRule(RelativeLayout.RIGHT_OF, R.id.game_mode_choose);
subMenuParams.addRule(RelativeLayout.ALIGN_TOP,R.id.game_mode_choose);
subMenuParams.height = 400;
subMenuParams.width = 150;
btn1plyr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(menuState == 0) {
gameModeChoose.animate().translationXBy(-150f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//here
Log.d("coords", "x:" + String.valueOf(gameModeChoose.getX()) + " y:" + String.valueOf(gameModeChoose.getY()));
mainFrame.addView(subMenu,subMenuParams);
subMenu.animate().alpha(1f);
hint.setText("Choose : O's go first.");
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
menuState = 1;
} else if(menuState == 1 || menuState == 2){
subMenu.animate().alpha(0);
MainActivity.this.gameModeChoose.animate().translationXBy(150f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
hint.setText("Choose a game mode:");
Log.d("coords", "x:" + String.valueOf(gameModeChoose.getX()) + " y:" + String.valueOf(gameModeChoose.getY()));
mainFrame.removeView(subMenu);
}
@Override
public void onAnimationCancel(Animator animation) {
hint.setText("Choose a game mode:");
mainFrame.removeView(subMenu);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
menuState = 0;
}
}
});
btn2plyr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(menuState == 0) {
gameModeChoose.animate().translationXBy(-150f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//here
Log.d("coords", "x:" + String.valueOf(gameModeChoose.getX()) + " y:" + String.valueOf(gameModeChoose.getY()));
subMenu.setLayoutParams(subMenuParams);
mainFrame.addView(subMenu);
subMenu.animate().alpha(1f);
hint.setText("Choose : O's go first.");
menuState = 2;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
} else if(menuState == 2 || menuState == 1){
subMenu.animate().alpha(0);
MainActivity.this.gameModeChoose.animate().translationXBy(150f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
hint.setText("Choose a game mode:");
Log.d("coords", "x:" + String.valueOf(gameModeChoose.getX()) + " y:" + String.valueOf(gameModeChoose.getY()));
mainFrame.removeView(subMenu);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
menuState = 0; }
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.darkjava.tictactoe.MainActivity">
<RelativeLayout
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/game_mode_choose"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="0dp">
<Button
android:id="@+id/btn1Plyr"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="1 PLAYER"
android:background="@drawable/title_border"
android:textColor="@color/title">
</Button>
<Button
android:id="@+id/btn2Plyr"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="2 PLAYER"
android:background="@drawable/title_border"
android:layout_marginTop="10dp"
android:textColor="@color/title"
android:layout_below="@+id/btn1Plyr">
</Button>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/menu_hint"
android:text="Choose a game mode:"
android:layout_above="@+id/game_mode_choose"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:textSize="20dp"
android:textColor="#464646"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TIC TAC TOE"
android:textSize="55dp"
android:textColor="@color/title"
android:layout_marginTop="20dp"
android:id="@+id/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>