2012-06-15 60 views
0

我目前遇到了一些非常密集繪製的動態壁紙的問題。然而,在測試我的代碼時,我發現壁紙是滯後的,並在屏幕旋轉時被破壞。動態壁紙性能問題

在我的畫法我有這樣的:

mRectArray = new Rect[] { 
       new Rect(0, 0, c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 2), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 2, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 3), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 3, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 4), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 4, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 5), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 5, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 6), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 6, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 7), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 7, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 8), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 8, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 9), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 9, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 10), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 10, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 11), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 11, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 12), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 12, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 13), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 13, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 14), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 14, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 15), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 15, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 16), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 16, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 17), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 17, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 18), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 18, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 19), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 19, 
         c.getWidth(), 
         (c.getHeight() + statusBarHeight)/20 * 20), 

       new Rect(0, 0, right1, 
         (c.getHeight() + statusBarHeight)/20), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20, right2, 
         (c.getHeight() + statusBarHeight)/20 * 2), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 2, 
         right3, (c.getHeight() + statusBarHeight)/20 * 3), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 3, 
         right4, (c.getHeight() + statusBarHeight)/20 * 4), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 4, 
         right5, (c.getHeight() + statusBarHeight)/20 * 5), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 5, 
         right6, (c.getHeight() + statusBarHeight)/20 * 6), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 6, 
         right7, (c.getHeight() + statusBarHeight)/20 * 7), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 7, 
         right8, (c.getHeight() + statusBarHeight)/20 * 8), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 8, 
         right9, (c.getHeight() + statusBarHeight)/20 * 9), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 9, 
         right10, 
         (c.getHeight() + statusBarHeight)/20 * 10), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 10, 
         right11, 
         (c.getHeight() + statusBarHeight)/20 * 11), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 11, 
         right12, 
         (c.getHeight() + statusBarHeight)/20 * 12), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 12, 
         right13, 
         (c.getHeight() + statusBarHeight)/20 * 13), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 13, 
         right14, 
         (c.getHeight() + statusBarHeight)/20 * 14), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 14, 
         right15, 
         (c.getHeight() + statusBarHeight)/20 * 15), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 15, 
         right16, 
         (c.getHeight() + statusBarHeight)/20 * 16), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 16, 
         right17, 
         (c.getHeight() + statusBarHeight)/20 * 17), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 17, 
         right18, 
         (c.getHeight() + statusBarHeight)/20 * 18), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 18, 
         right19, 
         (c.getHeight() + statusBarHeight)/20 * 19), 
       new Rect(0, (c.getHeight() + statusBarHeight)/20 * 19, 
         right20, 
         (c.getHeight() + statusBarHeight)/20 * 20), 

       new Rect(c.getWidth()/20, c.getHeight()/15, 
         c.getWidth()/20 * 8, c.getHeight()/15 * 4), 

       new Rect(c.getWidth()/20, c.getHeight()/15, 
         c.getWidth()/20 * 8, c.getHeight()/15 * 8), }; 

這是40長方形不斷被初始化的onDraw。 現在的問題是,這些矩形必須不斷初始化,因爲我的動畫矩形20的右側...

我還有一個40左右,如果進行動畫的矩形陳述..

我怎樣才能壓縮我的代碼,以便不犧牲性能?

+0

嘗試使用分析器查找瓶頸。另外,您不能一次創建Rect,然後更改其屬性?創建一個對象相對昂貴。也想想opengl,renderscript等。 – Ran

+0

最後20個矩形的右側有變量,而且我爲每個變量設置了動畫,但是當我嘗試在方法中初始化一個矩形後繪製每個矩形時,我會得到一個nullpointerexception和一個關閉力。 – Denizen

回答

0

使用onSurfaceChange()函數的高度和寬度初始化onSurfaceChange()中的數組。