2012-11-09 40 views
6

我有一個8x8圖像。 (位圖 - 可以更改)Android自定義畫筆模式/圖像

我想要做的是能夠繪製一個形狀,給定一個PathPaint對象到我的SurfaceView上。

目前我所能做的就是用純色填充形狀。我怎樣才能繪製一個模式。

Example

在圖像可以看到刷圖案(交叉)。它可以是從十字架到甜甜圈或精靈的任何東西。

我該如何去繪製這種圖案背景。

我也最終想要應用顏色。

到目前爲止,我的理論是創建一個形狀的剪輯區域,並平鋪位圖直到區域被覆蓋,但這在處理過程中極度過度。也不完美的理想。

就着色而言,我可以將畫筆編輯爲alpha,填充背景顏色,然後在上面繪製圖像。真正的問題是這種模式的平鋪。

我發現了幾個類似的問題,都沒有答案,和/或不適用於我的情況。 (在視圖中使用xmls等)

回答

18

您是否檢查了此blog。它使用BitmapShader

例子:

//Initialize the bitmap object by loading an image from the resources folder 
    fillBMP = BitmapFactory.decodeResource(m_context.getResources(), R.drawable.cross); 
    //Initialize the BitmapShader with the Bitmap object and set the texture tile mode 
    fillBMPshader = new BitmapShader(fillBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 

    fillPaint.setStyle(Paint.Style.FILL); 
    //Assign the 'fillBMPshader' to this paint 
    fillPaint.setShader(fillBMPshader); 

    //Draw the fill of any shape you want, using the paint object. 
    canvas.drawCircle(posX, posY, 100, fillPaint); 
+0

關於性能問題的任何建議?由於您正在畫布上快速繪製位圖,因此會導致沉重的用戶界面。導致非常緩慢的表現。 –