lundi 13 juin 2016

Random image, array and probability android studio

I have several questions with random topic . First, I 'm trying to make an app that take a random picture of an array and show it on a image view . What I want to do next, and i cant, is that set visible a button according to the displayed image appears. How can i do?

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

}

My other two questions are, it is possible to have 3 array , each with pictures, and do a random? first make a random array with those 3 and then another to take a picture of that array. The other question is, it is possible to make an image has more or less probabilities to appear in a random?

Aucun commentaire:

Enregistrer un commentaire