2014-04-27 101 views
0

我一直在試圖畫一個位圖,然後用矩形突出顯示一個區域。最初,我在繪畫中使用alpha黑色繪製位圖,使圖像變暗,然後在頂部繪製原始位圖,創建高光效果區域。我發現最大的放緩是因爲Paint中的alpha。所以,我已經修改了代碼,並結束了與我的拉線如下:加速位圖在android上的繪製

private synchronized void drawSquare(int xStart, int yStart, int xEnd, int yEnd) { 
    Canvas c = holder.lockCanvas(); 

    if(c != null) { 

     // Draw the background picture on top with some changed alpha channel to blend 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 


     if(bg != null && cWidth > 0 && cHeight > 0) { 
      c.clipRect(xStart, yStart, xEnd, yEnd, Region.Op.DIFFERENCE); 
      c.drawBitmap(bg, gTransform, blackSqr); // Draw derker background 
      c.clipRect(xStart, yStart, xEnd, yEnd, Region.Op.REPLACE); 
      c.drawBitmap(bg, gTransform, paint); ///draw original in selection 
      c.clipRect(0, 0, cWidth, cHeight,Region.Op.REPLACE); 
     } 

     Matrix RTcorner = new Matrix(); 
     RTcorner.setRotate(90); 
     RTcorner.postTranslate(xEnd + 13, yStart - 13); 

     Matrix RBcorner = new Matrix(); 
     RBcorner.setRotate(180); 
     RBcorner.postTranslate(xEnd + 13, yEnd + 13); 

     Matrix LBcorner = new Matrix(); 
     LBcorner.setRotate(270); 
     LBcorner.postTranslate(xStart - 13, yEnd + 13); 

     // Draw the fancy bounding box 
     c.drawRect(xStart, yStart, xEnd, yEnd, linePaintB); 

     // Draw corners for the fancy box 
     c.drawBitmap(corner, xStart - 13, yStart - 13, new Paint());  
     c.drawBitmap(corner, RBcorner, new Paint());   
     c.drawBitmap(corner, LBcorner, new Paint());  
     c.drawBitmap(corner, RTcorner, new Paint()); 


    } 

    holder.unlockCanvasAndPost(c); 
} 

所以這個剪輯了我的選擇區域,我有這個代碼,使其更暗油漆畫。

blackSqr.setColorFilter(new LightingColorFilter(Color.rgb(100,100,100),0)); 

而在剪輯區域內,我繪製了我的原始位圖。有用。但我對響應時間不滿意。分析位圖後是最長的。我已經將位圖縮放到屏幕的大小,所以它繪製了300x800的圖像。最大的資源豬似乎是燈光效果。因爲當我關閉它時,我會得到相當不錯的響應時間。

所以我想知道如果我錯過了任何提高位圖繪製速度的方法,可能是緩存?或者我只是堅持這一點,因爲我想要更深的圖像,實際上應該重新考慮「突出顯示/選擇」?爲什麼在2D圖像中使用alpha顏色繪製位圖非常昂貴?

回答

0

對於任何有興趣的人,可能面臨類似的問題。該解決方案適用於我的具體情況,但我必須使用手動設置變暗像素單獨的背景位圖:

for(int i = 0; i < cWidth; i++){ 
     for(int j = 0; j < cHeight; j++){ 
      int c = bg2.getPixel(i, j); 

      float mult = 0.15f; 

      int r = (int) (Color.red(c) * mult); 
      int g = (int) (Color.green(c) * mult); 
      int b = (int) (Color.blue(c) * mult); 

      bg2.setPixel(i, j, Color.rgb(r, g, b)); 
     } 
    } 

然後使用bg2繪製主要部分和原始(未變暗)爲的剪裁矩形選擇。創建和維護第二個位圖時會有一些開銷,但與使用alpha的位圖相比,繪製速度和響應時間快速且平滑。

0

如果我明白你想要什麼,你想要一個矩形(圓角)來突出顯示另一個圖像的一部分。

如果它是,則我會使用的圖像與方形機智draw9patch並且使用它作爲在圖像視圖中的浮動視圖

RelativeLaoyut(圖像容器) + - ImageView的(實際圖像) + - 查看(它有方形作爲背景,只需將其移至要突出顯示的區域)

對不起,我沒有很好的解釋我自己。

+0

謝謝,我會盡快嘗試9patch。 – Alex

+0

我無法獲得9patch,因爲我認爲您不能動態創建一個。 – Alex

+0

你動態地引用了什麼?你想成爲什麼樣的動態人物? – xgc1986