简体   繁体   中英

canvas bitmap animations without using a sprite sheet

   Bitmap[] planeFrames = new Bitmap[4];    
    protected void onDraw(Canvas canvas) {
    for(int i = 0 ; i < planeFrames.length;i++)
    canvas.drawBitmap(planeFrames[i], plane.getCenterX(), 0, null); // planeFrames is an array of Bitmaps
}

Im trying to animate a plane by just swapping images but its not working I don't know if my method is to simple to work with

You can not animate this way. onDraw is called per draw iteration meaning that whatever you draw on the Canvas at the return is what's going to be drawn on the screen. Basically what you're doing here is drawing all the Bitmaps on the Canvas at the same spot and they will all be plastered on top of each other.

What you need to do is call the invalidate() method of the View. To make things more efficient, you can use a Rect or define the areas around the Bitmaps.

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(planeFrames[i], plane.getCenterX(), 0, null);
    if(++i >= planeFrames.length) {
       i = 0;
    }
    invalidate(bmpRect);
}

This will consistently draw through the bitmaps while repeatedly drawing the new bitmaps. This will last forever as long as the View is on screen. bmpRect is a Rect that has the coordinates of the Bitmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM