如何在surfaceView中创建Rain效果?

时间:2017-12-22 21:54:28

标签: java android effect

我正在尝试在Android中创建一个降雨效果。我已经创建了精灵类,我不知道该怎么做才出现每秒X滴数或生命时间。有人能帮助我吗?

感谢。

这是我的代码

Sprite类:

public class Sprite_Rain {

private Bitmap rain_drop;
private Game game;
private int speed = 50;
int x, y = -100;
boolean position = true;
Paint paint;

public Sprite_Rain(Game game, Bitmap rain_drop){

    this.game = game;
    this.rain_drop= rain_drop;

    paint = new Paint();
}

public void onDraw(Canvas canvas) {

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

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

游戏课程:

public class Game extends SurfaceView {

private Bitmap rain_drop;
private Sprite_Rain sprite_rain;
Paint paint;

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

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

    rain_drop = BitmapFactory.decodeResource(getResources(), R.drawable.rain_drop);
    sprite_rain = new Sprite_Rain(this, rain_drop);

    paint = new Paint();
}

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

@Override
protected void onDraw(Canvas canvas) {

    paint.setColor(Color.BLACK);

    int i = 0;
    for (Sprite_Rain sprite : sprites) {
        sprite.onDraw(canvas);
        i++;
    }
    invalidate();
}
}

0 个答案:

没有答案