1

I'm working on my first Android project - Live wallpaper. I need some images in my project to fly all the time on the screen. Images should fly from bottom to top, but they need to fly all time, so that background is never empty. This is part of my code but I know that I need more than that.

void draw(Canvas c) {
    c.save();
    c.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper1), 0, 0, null);
    double tmp = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart_s);
    c.drawBitmap(bm, 100 + (int) tmp, posY, paint);

    double tmp1 = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm1 = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart1);
    c.drawBitmap(bm1, 300 + (int) tmp1, posY, paint);

    double tmp2 = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm2 = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart3);
    c.drawBitmap(bm2, 200 + (int) tmp2, posY, paint)

    posY = posY -direction;
    fiX = fiX + 10;

    if(posY < 0) {
        //posY = getResources().getDisplayMetrics().heightPixels;
        direction = 5;                  
    }

    if(posY > getResources().getDisplayMetrics().heightPixels) {
        direction = 5;                                      
    }
    if(fiX > 180) {
            fiX = 0;
    }
    c.restore();
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I need that my images fly all the time on the screen, not only from bottom to top. –  Oct 22 '12 at 08:44
  • You should explain a little better your idea of animation. _"Images should fly from bottom to top, but they need to fly all time"_ So, what is actually supposed to happen when the image reaches the top of the screen? – Less Oct 22 '12 at 08:49

1 Answers1

2

At the end of your draw method call to invalidate view. This will start a loop over your draw.

Add animation logic at the beginning of your draw, create some drawModel and use this logic to update it. Then change the positions of the images according to your model.

void draw(Canvas canvas) {
    handleUpdateModelEvent();
    onDraw();
    invalidate();
}

private void handleUpdateModelEvent(){

}

private void onDraw(){
   // in MVC this method should be called from model change event
}
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216