我一直在試圖畫一個位圖,然後用矩形突出顯示一個區域。最初,我在繪畫中使用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顏色繪製位圖非常昂貴?
謝謝,我會盡快嘗試9patch。 – Alex
我無法獲得9patch,因爲我認爲您不能動態創建一個。 – Alex
你動態地引用了什麼?你想成爲什麼樣的動態人物? – xgc1986