Android custom brush pattern/image

2019-03-20 03:07发布

问题:

I have an 8x8 Image. (bitmap - can be changed)

What I want to do is be able to draw a shape, given a Path and Paint object onto my SurfaceView.

At the moment all I can do is fill the shape with solid colour. How can I draw it with a pattern.

In the image you can see the brush pattern (The cross). It can be anything from a cross to a donut or an elf.

How would I go about drawing this pattern background.

I also eventually want to apply colours to it.

So far, my theory is to create a clip area of the shape, and tile the bitmaps until area is covered, but this is extreme overkill in processing. Nor sound ideal.

In terms of colouring, I can edit the brushes to be alpha, fill the same with background colour, then draw the images on top. The real issue it the tiling of such patterns.

Ive found a few questions of a similar nature, all unanswered, and/or not applicable to my situation. (use of xmls on views etc)

回答1:

Did you checked this blog. Its using BitmapShader

Example:

    //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);