SurfaceView上的逐帧动画

时间:2012-09-15 15:24:59

标签: android

我想问一下,在surfaceview上进行逐帧动画的正确方法是什么?

这是我到目前为止所做的......

class MySurfaceView extends SurfaceView implements Runnable{


Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
Bitmap bitMap;

int i = 1;

public MySurfaceView(Context context)
{
super(context);
surfaceHolder = getHolder();

bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y1);

final CountDownTimer t = new CountDownTimer(75000,50)
{

    @Override
    public void onFinish() {    
    }

    @Override
    public void onTick(long millisUntilFinished) {
        i++;
        if ( i >= 26 ) { i = 1; }

        if ( i == 1 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y1); }
        else if ( i == 2 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y2); }
        else if ( i == 3 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y3); }
        else if ( i == 4 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y4); }
        else if ( i == 5 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y5); }
        else if ( i == 6 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y6); }
        else if ( i == 7 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y7); }
        else if ( i == 8 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y8); }
        else if ( i == 9 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y9); }
        else if ( i == 10 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y10); }

        else if ( i == 11 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y11); }
        else if ( i == 12 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y12); }
        else if ( i == 13 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y13); }
        else if ( i == 14 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y14); }
        else if ( i == 15 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y15); }
        else if ( i == 16 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y16); }
        else if ( i == 17 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y17); }
        else if ( i == 18 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y18); }
        else if ( i == 19 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y19); }
        else if ( i == 20 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y20); }

        else if ( i == 21 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y21); }
        else if ( i == 22 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y22); }
        else if ( i == 23 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y23); }
        else if ( i == 24 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y24); }
        else if ( i == 25 ) { bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.y25); }

    }

}.start();



running = true;
thread = new Thread(this);
thread.start();
}


//////////////////////////////////////////////////////////

public void run() {

while(running){

if( surfaceHolder.getSurface().isValid() ){

Canvas canvas = surfaceHolder.lockCanvas();

if ( canvas == null ) { Log.e("null","n"); } else
    {

    /////

    canvas.drawBitmap(bitMap, 0, 0, null);

    /////
    surfaceHolder.unlockCanvasAndPost(canvas);

    }

}
}
}

//////////////////////////////////////////////////////////
...

至少它在没有崩溃或超过vm缓冲区的情况下播放,但帧率非常低,可能大约为5-10 fps。有没有办法加快速度?

谢谢!

1 个答案:

答案 0 :(得分:1)

每帧加载图像非常耗时。一个更好的方法来做你想要做的是将所有图像并排放在一个图像中(称为图像Atlas或Texture Atlas更准确)并在启动时仅将其加载到内存中一次。然后使用Canvas. drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)绘制每个帧作为Atlas的一部分。这将显着提高帧速率。