0

我想问,在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。有没有办法加快速度?

谢谢!

4

1 回答 1

1

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

于 2012-09-15T15:43:06.443 回答