Use AnimationDrawable in a SurfaceView

I tried for a long time. Only questions about it could be found on the Internet, but no complete answer. I have to admit, this is the most painful part while developing an android application.

 

While, anyhow, here are the steps you need to follow if you want to draw an animation drawable in a SurfaceView.

 

1.       Override the verifyDrawable method of SurfaceView.

The most painful part is this verifyDrawable method is mentioned neither on the android Reference document nor on any book about android development in details.

 

    public boolean verifyDrawable(Drawable who){

       return who != null;

}

 

2.       Call setCallback of the animation drawable object you need to draw, and send the surfaceview as its parameter.

3.       Call start method of the animation drawable object outside the onCreated method of the view or activity.

 

In general, you need to draw an animation drawable with both the following and above sample code.

 

           animDrawable.setBounds(new Rect());

           animDrawable.setCallback(surfaceView);

           animDrawable.draw(c);

           animDrawable.start();

 

Without any step above, the animation drawable will not work correctly.

posted on 2010-09-12 21:56  南柯之石  阅读(1154)  评论(0编辑  收藏  举报

导航