0

I'm doing a Live Wallpaper. I have a Runnable (which is updated every 5 seconds) where I call a draw() method which draws something on a Canvas. It calls a method in another class which is supposed to draw a series of bitmaps (with a delay, so it's animated). How would I change the code below, so that the next bitmap would be drawn with a delay?

int imageToDraw = 10;
    while(imageToDraw>=0)
    {
        Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier("image_"+imageToDraw, "raw", mContext.getPackageName()));

        float centerX = (width - b.getWidth())/2;
        float centerY = (height - b.getHeight())/2;

        Paint p = new Paint();
        p.setColor(Color.BLACK);
        mCanvas.drawRect(0f, 0f, (float)width, (float)height, p); //draws a rectangle to clear the screen
        mCanvas.drawBitmap(b, centerX,  centerY, new Paint());

        --imageToDraw;
        b.recycle();
    }
domen
  • 1,239
  • 3
  • 15
  • 26

1 Answers1

1

From Android's API Guide on Canvas and Drawables:

Draw your graphics directly to a Canvas. This way, you personally call the appropriate class's onDraw() method (passing it your Canvas), or one of the Canvas draw...() methods (like drawPicture()). In doing so, you are also in control of any animation.

This means you have to perform the animation yourself, frame by frame. If you don't actually need to draw complex graphics, consider switching back to standard views so you can use help class like AnimationDrawable. Check here for an example of how to do your own animation in Canvas.

Community
  • 1
  • 1
Kai
  • 15,284
  • 6
  • 51
  • 82