Android - Duda para cambiar la imagen Android Studio

 
Vista:

Duda para cambiar la imagen Android Studio

Publicado por Alexis (1 intervención) el 16/11/2017 23:44:22
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder