我是编程android的新手,我正在开发一个BattleShip游戏。我创建了一个10x10板,现在我试图在板上添加随机船。我通过getPositionOfTag找到了位置,并通过它完成了另一个垂直方向的textview。我想知道我如何把更多的textviews放在船上,以便用它的真实尺寸(5个方格)来补充它。并且还想知道如何横向调整ramdom。
Java代码:
for (int i = 0; i < tamanho_tabuleiro; i++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(rowParams);
for (int j = 0; j < tamanho_tabuleiro; j++) {
textView = new TextView(this);
textView.setBackgroundDrawable(gd);
textView.setLayoutParams(rowParams);
//textView.setText(" ");
textView.getLayoutParams().height = lado_quadrado;
textView.getLayoutParams().width = lado_quadrado;
textView.setTag(String.valueOf(i + "." + j));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundDrawable(gradientDrawable);
}
});
tableRow.addView(textView);
}
tableLayout.addView(tableRow, tableParams);
}
for (int k = 0; k < qtd_portaAvioes; k++) {
Random random = new Random();
int indexI = random.nextInt(tamanho_tabuleiro);
int indexJ = random.nextInt(tamanho_tabuleiro);
TableRow tableRow = (TableRow) tableLayout.getChildAt(indexI);
View view = tableRow.findViewWithTag(indexI + "." + indexJ );
if (view != null && !verificaPopulado(view)) {
int positionComeco = getPositionOfTag((String) view.getTag());
addNavio(tableRow, new PortaAvioes(this), positionComeco);
int orientation = random.nextInt(2);
for(int x = 0; x < tam_portaAvioes; x++) {
TableRow tableRow1 = (TableRow) tableLayout.getChildAt(indexI + 1);
if (orientation == 0) {
//verificar sea a tablerow existe, se nao esta acima do limite
if (tableRow1 != null) {
View view2 = tableRow1.findViewWithTag(indexI + 1 + "." + positionComeco);
if (view2 != null && !verificaPopulado(view2)) {
addNavio(tableRow1, new PortaAvioes(this), positionComeco);
}
} else {
View view2 = tableRow1.findViewWithTag(indexI + 1 + "." + positionComeco);
}
}else if (orientation == 1) {
//add ship horizontal
}
}
}
}
public void addNavio(TableRow tableRow, TextView navio, int position) {
tableRow.removeViewAt(position);
tableRow.addView(navio, position);
}
public boolean verificaPopulado(View view) {
if (view instanceof Encouracado || view instanceof Destroyer || view instanceof PortaAvioes || view instanceof Cruiser || view instanceof Submarino)
return true;
return false;
}
public int getPositionOfTag(String tag) {
return Integer.parseInt(tag.substring(tag.indexOf(".") + 1));
}
&#13;
PortaAvioes类:
public class PortaAvioes extends TextView {
public PortaAvioes(Context context) {
super(context);
GradientDrawable gd = new GradientDrawable();
gd.setStroke(1, 0xFF000000);
gd.setColor(Color.YELLOW);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setText("PA");
setBackground(gd);
} else
setBackgroundDrawable(gd);
&#13;
答案 0 :(得分:1)
RotateAnimation rotateAnmi = (RotateAnimation) AnimationUtils.loadAnimation(this,R.drawable.rorate); rotateTv.setAnimation(rotateAnmi); rotateTv.setGravity(Gravity.AXIS_X_SHIFT); rotateTv.setText();