简体   繁体   中英

canvas.drawBitmap() takes more time to draw bitmap first time while tiling the bitmap

I am using canvas.drawBitmap(Bitmap,src,dst,null) for animation. I am using 1 tiled images for animation, each tiled image containing more then 100 image for making animation. I have put onCLickListner to start animation. how can I have an instant responce for animations on onClick?

I have used logic for animation as per following link. http://warriormill.com/2009/10/adroid-game-development-part-1-gameloop-sprites/

@Override
    public void draw(Canvas canvas) {
        try
        {
            FrameInfo frameinfo= animations.get(currentAnimation).sequence.get(currentFrame);
            Rect rclip = frameinfo.rect;
            Rect dest = new Rect(this.getXpos(), getYpos(), getXpos() + (rclip.right - rclip.left),
                getYpos() + (rclip.bottom - rclip.top));
            if(cf!=null)
            {
                //color filter code here

            }
            canvas.drawBitmap(tileSheet, rclip, dest, null);
            update(); //after drawing update the frame counter
        }
        catch (Exception e) 
        {
            Log.e("ERROR", "ERROR IN SPRITE TILE  CODE:"+e.toString()+e.getStackTrace().toString());
        }

    }

Resource loading takes significant time (they are compressed inside your APK ), so much that you must pre-load these, and not load them just-in-time. The draw() method is the last place to be doing any just-in-time resource handling. You should also be doing that in the background, to avoid ANR issues. An AsyncTask implementation works just great! You will also need to be very careful about matching DPI, or drawBitmap() will scale your bitmaps, which will degrade your frame rate noticeably. If you have the skill, use OpenGL with 2D texture mapping.

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