2012-05-24 38 views
0

設置我的背景圖像起來surfaceCreated():SurfaceView背景減慢使用此代碼的onDraw 20-30毫秒

Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.untitled); 
    float heightScale = ((float)background.getHeight())/(float)this.getHeight(); 
    float widthScale = (float)background.getWidth()/(float)this.getWidth(); 
    float scale = heightScale > widthScale ? widthScale : heightScale; 
    int newWidth = Math.round((float)background.getWidth()/scale); 
    int newHeight = Math.round((float)background.getHeight()/scale); 
    scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true); 

縮放是受保護的位圖。在的onDraw()函數,我有這樣的:

canvas.drawBitmap(scaled, 0, 0, null); // draw the background 

減慢20-30毫秒我的畫相比,使用這樣的:

canvas.drawColor(Color.Black); 

有沒有解決這個辦法嗎?加快背景onDraw函數?我注意到,如果我不設置背景,應用程序不會清除我繪製的精靈。

回答

0

嗯......這裏是你如何做到這一點:

您創建一個佈局,並設置佈局的內容視圖。然後將surfaceview添加到佈局,則表面視圖像素格式設置爲透明像這樣:

panel = new Panel(this, getNewHandler()); 
    ((LinearLayout)findViewById(R.id.panellinear)).addView(panel); 
    SurfaceHolder p; 
    panel.setZOrderOnTop(true); // necessary 
    p = panel.getHolder(); 
    p.setFormat(PixelFormat.TRANSPARENT) 

(面板對我來說是一件延伸SurfaceView)