Compare images

0

I'm trying to make an application that takes a random image of an array and displays it in a view image. What I want to do now, and I can not, is that depending on the image that appears, I want different things to happen, try a switch but it does not work, what am I doing wrong?

My code:

public class MainActivity extends Activity {

Button b1, b2,b3,b4,b5;
ImageView iv1,iv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv1=(ImageView)findViewById(R.id.iv1);
    iv2=(ImageView)findViewById(R.id.iv2);
    b1=(Button)findViewById(R.id.b1);
    b2=(Button)findViewById(R.id.b2);
    b3=(Button)findViewById(R.id.b3);
    b4=(Button)findViewById(R.id.b4);
    b5=(Button)findViewById(R.id.b5);
}

@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);
}

public void Accion1 (View view){

    final TypedArray imgs = getResources().obtainTypedArray(R.array.RandomImages);
    final Random rand = new Random();
    final int rndInt = rand.nextInt(imgs.length());
    final int rID = imgs.getResourceId(rndInt, 0);
    iv2.setImageResource(rID);

    b1.setVisibility(View.INVISIBLE);
    b2.setVisibility(View.VISIBLE);
}

public void Accion2(View view){
    b2.setVisibility(View.INVISIBLE);
    switch (iv2.getId()){
        case 1:
            iv2.setImageResource(R.drawable.imagen1);
            b3.setVisibility(View.VISIBLE);
            break;
        case 2:
            iv2.setImageResource(R.drawable.imagen2);
            b4.setVisibility(View.VISIBLE);
            break;
        case 3:
            iv2.setImageResource(R.drawable.imagen3);
            b5.setVisibility(View.VISIBLE);
            break;
    }
}

}

    
asked by Agustin Val 30.06.2016 в 20:45
source

2 answers

2

I can think of the following:

public void Accion1 (View view){

    final TypedArray imgs = getResources().obtainTypedArray(R.array.RandomImages);
    final Random rand = new Random();
    final int rndInt = rand.nextInt(imgs.length());
    final int rID = imgs.getResourceId(rndInt, 0);
    iv2.setImageResource(rID);
    iv2.setTag(rID);

    b1.setVisibility(View.INVISIBLE);
    b2.setVisibility(View.VISIBLE);
}

public void Accion2(View view){
    b2.setVisibility(View.INVISIBLE);
    int tagImageView = (int) iv2.getTag();
    switch (tagImageView){
        case R.drawable.imagenX:
            iv2.setImageResource(R.drawable.imagen1);
            b3.setVisibility(View.VISIBLE);
            break;
        case R.drawable.imagenY:
            iv2.setImageResource(R.drawable.imagen2);
            b4.setVisibility(View.VISIBLE);
            break;
        case R.drawable.imagenZ:
            iv2.setImageResource(R.drawable.imagen3);
            b5.setVisibility(View.VISIBLE);
            break;
    }
}
    
answered by 01.07.2016 / 00:36
source
2

You can create a TypedArray containing the possible ids

link

For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="mis_imagenes">
        <item>@drawable/imagen1</item>
        <item>@drawable/imagen2</item>
        <item>@drawable/imagen3</item>
    </array>
</resources>

You get the values in this way:

Resources res = getResources();
TypedArray imagenes = res.obtainTypedArray(R.array.mis_imagenes);

The drawable you get it this way:

Drawable drawable = imagenes.getDrawable("valor random 0 a 3 que es la cantidad de elementos definidos");
    
answered by 01.07.2016 в 01:48