Rain effect in Android Studio

1

I'm trying to create the rain effect. I already created the sprite, what I do not know is how to make X number of drops per second or life time appear. Can someone help me?

Thank you.

Sprite:

public class Sprite_Lluvia {

private Bitmap gota_de_lluvia;
private Juego juego;
private int velocidad = 50;
int x, y = -100;
boolean posicion = true;
Paint paint;

public Sprite_Lluvia(Juego juego, Bitmap gota_de_lluvia){

    this.juego = juego;
    this.gota_de_lluvia = gota_de_lluvia;

    paint = new Paint();
}

public void onDraw(Canvas canvas) {

    if (posicion == true) {
        Random random = new Random();
        x = random.nextInt(canvas.getWidth());
        posicion = false;
    }

    canvas.drawBitmap(gota_de_lluvia, x, y, null);
}
}

Game:

public class Juego extends SurfaceView {

private Bitmap gota_de_lluvia;
private Sprite_Lluvia sprite_lluvia;
Paint paint;

private List<Sprite_Lluvia> sprites = new ArrayList<Sprite_Lluvia>();

public Juego(Context context, AttributeSet attrs) {
    super(context, attrs);

    gota_de_lluvia = BitmapFactory.decodeResource(getResources(), R.drawable.gota_de_lluvia);
    sprite_lluvia = new Sprite_Lluvia(this, gota_de_lluvia);

    paint = new Paint();
}

private Sprite_Lluvia createSprite(int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
    return new Sprite_Lluvia(this, bmp);
}

@Override
protected void onDraw(Canvas canvas) {

    paint.setColor(Color.BLACK);

    int i = 0;
    for (Sprite_Lluvia sprite : sprites) {
        sprite.onDraw(canvas);
        i++;
    }
    invalidate();
}
}
    
asked by Zekirak 20.12.2017 в 15:18
source

1 answer

0

To show X number of drops per second you could use a Timer , or a < a href="https://developer.android.com/reference/android/os/Handler.html"> Handler that is more efficient in Android. I recommend CountDownTimer that although less efficient than the Handler, it is more easy to implement. Example modifying your class Game in the onDraw () method:

Game Class

private Handler mHandler = new Handler();
...

@Override
protected void onDraw(Canvas canvas) {

    paint.setColor(Color.BLACK);

    ....

    //contador de 30 segundos y que se ejecuta cada segundo
    new CountDownTimer(30000, 1000) {

       public void onTick(long millisUntilFinished) {
           int segundos = millisUntilFinished/1000;

           if(segundos > 20){
           //Creas una gota y la dibujas
               sprite_lluvia = new Sprite_Lluvia(this, gota_de_lluvia);
               sprite_lluvia.onDraw(canvas);
           }
           else if(segundos > 10){
           //Creas 2*gotas y las dibujas
               sprite_lluvia2 = new Sprite_Lluvia(this, gota_de_lluvia);
               sprite_lluvia2.onDraw(canvas);
               sprite_lluvia3 = new Sprite_Lluvia(this, gota_de_lluvia);
               sprite_lluvia3.onDraw(canvas);
           }
           else{
           //Creas 3*gotas y las dibujas
           }
       }

       public void onFinish() {
          //si quieres hacer algo al acabar el tiempo de juego
       }
    }.start();

    ....
};

For the remaining life time, more of the same. Use another counter for each drop that when you reach 0 make it disappear.

    
answered by 20.12.2017 в 18:48