Random position ImageViews... more time out of screen

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 10

Sep 26, 2018, 8:06:22 PM via Website

Good morning,
new question that have been struck by a couple of days,
always for the splash screen talk with mini game to entertain the user.
Since positioning in a static way for an undefined number of combinations would be a long work and I think also useless since I hope that the splash screen is loaded for the shortest possible time, I opted for a pseudo-random positioning of the imageView but often they are positioned outside of the screen but unfortunately I can not identify the best code to avoid it, does anyone have a conscience? :'(

This is the code used by me that creates the problem.

DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay()
            .getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    /*Random Number01*/
    Random rand01 = new Random();
    int numb01 = rand01.nextInt(10)+1; // Gives n such that 0 <= n < 10
    Random randomX01 = new Random();
    int X01 = randomX01.nextInt(width)+1;
    Random randomY01 = new Random();
    int Y01 = randomY01.nextInt(height)+1;
    if (X01 > metrics.widthPixels /2 ) {
        X01 = X01 - ((ImageView)findViewById(R.id.coin01)).getMeasuredWidth ();
    }
    if (Y01 > metrics.heightPixels /2 ) {
        Y01 = Y01 - ((ImageView)findViewById(R.id.coin01)).getMeasuredHeight ();
    }
    ((ImageView)findViewById(R.id.coin01)).setX ( X01 );
    ((ImageView)findViewById(R.id.coin01)).setY ( Y01 );
    if (numb01 == 1) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon001souls);
        accumulator01 =  1;
    } else if (numb01 == 2) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon002souls);
        accumulator01 =  2;
    } else if (numb01 == 3) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon005souls);
        accumulator01 =  5;
    } else if (numb01 == 4) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon010souls);
        accumulator01 =  10;
    } else if (numb01 == 5) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon020souls);
        accumulator01 =  20;
    } else if (numb01 == 6) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon050souls);
        accumulator01 =  50;
    } else if (numb01 == 7) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon100souls);
        accumulator01 =  100;
    } else if (numb01 == 8) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon200souls);
        accumulator01 =  200;
    } else if (numb01 == 9) {
        ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon500souls);
        accumulator01 =  500;
    }

Waiting for your kind help, (thinking)

Thank you :)

Reply
  • Forum posts: 10

Sep 26, 2018, 11:56:15 PM via Website

Resolved,
all the problem arose from the selection of the constrain of the image view to the layout.
Consequently, the point 0,0 of the image corresponded to the position fixed in the layout design page (I mistakenly thought that the zero corresponded regardless of the top left margin) and the image moved outside the screen.

See you next time (cool)

Risolto,
tutto il problema nasceva dalla selezione della constrain dell'imageview al layout.
Conseguentemente il punto 0,0 dell'immagine corrispondeva alla posizione fissata nella pagina di design del layout (erroneamente pensavo che lo zero corrispondesse a prescindere al margine alto sinistro) e l'immagine si spostava al di fuori dello schermo.

Alla prossima (cool)

Helpful?
Reply

Recommended articles