2012-09-15 82 views
0

我想問一下,在surfaceview上進行逐幀動畫的正確方法是什麼?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幀。有沒有辦法加快速度?

謝謝!

回答

1

每幀加載圖像非常耗時。一個更好的方法來做你想做的事情就是把所有的圖像並排放在一個圖像中(稱爲圖像地圖集或紋理圖集更準確),並在啓動時將其加載到內存中一次。然後你使用Canvas. drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)來繪製每個幀作爲你的Atlas的一部分。這將顯着增加幀速率。

+0

不,糟糕的想法:) java.lang.OutOfMemoryError:位圖大小超過虛擬機預算。 –

+0

你需要犧牲一些分辨率,或者你可以做我提到的批次(例如有4個地圖集),這會比你有更多的幀速率。此外,請注意,不要在模擬器上測量幀頻,因爲它非常慢 –

+0

每個幀都是全屏,480x800,因此必須按照您的建議考慮批量。我在手機上測試它,仍然很慢... –