2012-11-09 52 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

关于性能问题的任何建议?由于您正在画布上快速绘制位图,因此会导致沉重的用户界面。导致非常缓慢的表现。 –