Buenas tengo el siguiente código y quería cambiar la imagen dependiendo de si es 1 o 2, si es 1 pasa a ser 2 y si es 2 pasa a ser 1.
PD: segun entiendo, con llamar a TileView y ponerle la posicion de la imagen que quieres modificar (el valor de index por ejemplo) deberia funcionar, pero realmente no me funciona cuando le doy click, espero que podais ayduarme con esa duda. TileView tv = new TileView(this, x, y, elements,index,pictures[index]);
int topTileX = Integer.parseInt(topTileXstring); int topTileY = Integer.parseInt(topTileYstring); topElements = Integer.parseInt(topElementsString); pictures = null; tilePictureToShow = 0;
Vibrator vibratorService = (Vibrator)(getSystemService(Service.VIBRATOR_SERVICE)); MediaPlayer mp = MediaPlayer.create(this, R.raw.touch); TextView tvNumberOfClicks = (TextView) findViewById(R.id.clicksTxt); //obtención de parámetros de configuración
if ("Colores".equals(opcion1)){ pictures = colors; } else{ pictures = numbers; } /*hasSound= extras.getBoolean("hasSound"); hasVibration= extras.getBoolean("hasVibration");*/ //limpiar el tablero LinearLayout ll = (LinearLayout) findViewById(R.id.fieldLandscape); ll.removeAllViews(); //obtención de tamaño de pantalla DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels / topTileX; int height = (dm.heightPixels - 180) / topTileY; //inicialización de arrays int[][] ids = new int[topTileX][topTileY]; int[][] values = new int [topTileX][topTileY]; //inicialización de números aleatorio Random r = new Random(System.currentTimeMillis()); tilePictureToShow = r.nextInt(topElements); // crear celdas int ident = 0; for (int i = 0; i < topTileY; i++) { LinearLayout l2 = new LinearLayout(this); l2.setOrientation(LinearLayout.HORIZONTAL); for (int j = 0; j < topTileX; j++) { tilePictureToShow = r.nextInt(topElements); // guardamos la trama a mostrar values[j][i] = tilePictureToShow; TileView tv = new TileView(this, j, i, topElements, tilePictureToShow, pictures[tilePictureToShow]); ident++; // se asigna un identificador al objeto creado tv.setId(ident); // se guarda el identificador en una matriz ids[j][i] = ident; tv.setHeight(height); tv.setWidth(width); tv.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { hasClick(((TileView) view).x, ((TileView) view).y,((TileView) view).topElements,((TileView) view).index); } }); l2.addView(tv); } ll.addView(l2); } // cronómetro Chronometer t = (Chronometer)findViewById(R.id.Chronometer); t.start(); } protected void hasClick(int x, int y, int elements, int index) { elements = elements -1; if(index == elements) { index = 0; } else { index ++; } elements = elements + 1; TileView tv = new TileView(this, x, y, elements, index, pictures[index]); }
private static final int[] colors = new int[]{ R.drawable.ic_1c, R.drawable.ic_2c, R.drawable.ic_3c, R.drawable.ic_4c, R.drawable.ic_5c, R.drawable.ic_6c }; //numeros private static final int[] numbers = new int[]{ R.drawable.ic_1n, R.drawable.ic_2n, R.drawable.ic_3n, R.drawable.ic_4n, R.drawable.ic_5n, R.drawable.ic_6n };
TileView.java
public class TileView extends android.support.v7.widget.AppCompatButton { // coordenadas public int x = 0; public int y = 0; // trama a mostrar public int index = 0; //max tramas public int topElements = 0; public TileView(Context context, int x, int y, int topElements, int index, int background) { super(context); this.x = x; //coordenada X this.y = y; //coordenada Y this.topElements = topElements; //max tramas this.index = index; //índice de trama this.setBackgroundResource(background); } public int getNewIndex(){ index ++; //controlar si necesitamos volver a comenzar el ciclo de tramas if (index == topElements)index = 0; return index; } }
Buenas tengo el siguiente código y quería cambiar la imagen dependiendo de si es 1 o 2, si es 1 pasa a ser 2 y si es 2 pasa a ser 1.
PD: segun entiendo, con llamar a TileView y ponerle la posicion de la imagen que quieres modificar (el valor de index por ejemplo) deberia funcionar, pero realmente no me funciona cuando le doy click, espero que podais ayduarme con esa duda. TileView tv = new TileView(this, x, y, elements,index,pictures[index]);
content_activity_juego.xml `
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|fill_horizontal">
<Chronometer android:id="@+id/Chronometer"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right|fill_horizontal"></Chronometer>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:id="@+id/clicksTxt" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fieldLandscape"
>
</LinearLayout>
</LinearLayout>
ActivityJuego.java
public class ActivityJuego extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
MediaPlayer mediaPlayer;
static int topElements;
static int tilePictureToShow;
static int[] pictures;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_juego);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//---------------------------------------------------------------------------------------
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String topTileXstring = sharedPreferences.getString("ejeX","3");
String topTileYstring = sharedPreferences.getString("ejeY","3");
String topElementsString = sharedPreferences.getString("tramas","2");
String opcion1 = sharedPreferences.getString("opcion1","Numeros");
boolean hasSonido = sharedPreferences.getBoolean("sonido",true);
boolean hasVibracion = sharedPreferences.getBoolean("vibracion",true);
int topTileX = Integer.parseInt(topTileXstring);
int topTileY = Integer.parseInt(topTileYstring);
topElements = Integer.parseInt(topElementsString);
pictures = null;
tilePictureToShow = 0;
Vibrator vibratorService = (Vibrator)(getSystemService(Service.VIBRATOR_SERVICE));
MediaPlayer mp = MediaPlayer.create(this, R.raw.touch);
TextView tvNumberOfClicks = (TextView) findViewById(R.id.clicksTxt);
//obtención de parámetros de configuración
if ("Colores".equals(opcion1)){
pictures = colors;
}
else{
pictures = numbers;
}
/*hasSound= extras.getBoolean("hasSound");
hasVibration= extras.getBoolean("hasVibration");*/
//limpiar el tablero
LinearLayout ll = (LinearLayout) findViewById(R.id.fieldLandscape);
ll.removeAllViews();
//obtención de tamaño de pantalla
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels / topTileX;
int height = (dm.heightPixels - 180) / topTileY;
//inicialización de arrays
int[][] ids = new int[topTileX][topTileY];
int[][] values = new int [topTileX][topTileY];
//inicialización de números aleatorio
Random r = new Random(System.currentTimeMillis());
tilePictureToShow = r.nextInt(topElements);
// crear celdas
int ident = 0;
for (int i = 0; i < topTileY; i++) {
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < topTileX; j++) {
tilePictureToShow = r.nextInt(topElements);
// guardamos la trama a mostrar
values[j][i] = tilePictureToShow;
TileView tv = new TileView(this, j, i, topElements,
tilePictureToShow, pictures[tilePictureToShow]);
ident++;
// se asigna un identificador al objeto creado
tv.setId(ident);
// se guarda el identificador en una matriz
ids[j][i] = ident;
tv.setHeight(height);
tv.setWidth(width);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
hasClick(((TileView) view).x, ((TileView) view).y,((TileView) view).topElements,((TileView) view).index);
}
});
l2.addView(tv);
}
ll.addView(l2);
}
// cronómetro
Chronometer t = (Chronometer)findViewById(R.id.Chronometer);
t.start();
}
protected void hasClick(int x, int y, int elements, int index)
{
elements = elements -1;
if(index == elements)
{
index = 0;
}
else
{
index ++;
}
elements = elements + 1;
TileView tv = new TileView(this, x, y, elements,
index, pictures[index]);
}
private static final int[] colors = new int[]{
R.drawable.ic_1c,
R.drawable.ic_2c,
R.drawable.ic_3c,
R.drawable.ic_4c,
R.drawable.ic_5c,
R.drawable.ic_6c
};
//numeros
private static final int[] numbers = new int[]{
R.drawable.ic_1n,
R.drawable.ic_2n,
R.drawable.ic_3n,
R.drawable.ic_4n,
R.drawable.ic_5n,
R.drawable.ic_6n
};
TileView.java
public class TileView extends android.support.v7.widget.AppCompatButton {
// coordenadas
public int x = 0;
public int y = 0;
// trama a mostrar
public int index = 0;
//max tramas
public int topElements = 0;
public TileView(Context context, int x, int y, int topElements, int index, int
background) {
super(context);
this.x = x; //coordenada X
this.y = y; //coordenada Y
this.topElements = topElements; //max tramas
this.index = index; //índice de trama
this.setBackgroundResource(background);
}
public int getNewIndex(){
index ++;
//controlar si necesitamos volver a comenzar el ciclo de tramas
if (index == topElements)index = 0;
return index;
}
}