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
Asked
Active
Viewed 1,070 times
1 Answers
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
-
2I actually achieved the effect by using paint.setShader(BitmapShader). – user1990654 Jun 25 '13 at 12:01