0

I am trying to draw non-rectangular shapes on canvas. I need to fill them up with textures like grass and tarmac(for roads). How can I do that? I have already tried to overlap bitmaps of different shapes to achieve this effect on one frame but the shapes keep changing as time progresses, so I need another solution. Thanks in advance Sameer Raina

1 Answers1

0

You can use BitmapDrawable to draw seamless pattern images like this

 BitmapDrawable tileImg = new BitmapDrawable(getResources(), R.drawable.image);
 tileImg.setBounds(bounds);
 tileImg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); 
 tileImg.draw(canvas); // this will fill the canvas. 

To create shapes with it you should clip the canvas to a path that corresponds to your shape, and then calling the tileImg.draw(canvas); method, to fill the clipped area only.

Be careful though, because there are some issues with using clipPath(), read more about that here and here

Community
  • 1
  • 1
Plato
  • 2,338
  • 18
  • 21