3

I am trying to tile a 20x20 background onto my Custom View but for some reason I am unable too.

 BitmapDrawable background; background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.back)); background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); background.draw(canvas); 

Does anyone have an idea why it isn't working?

3 Answers 3

6

Don't set the bounds to the size of the tile: set them to the total area to be tiled. In your case:

background.setBounds(0, 0, myView.getWidth(), myView.getHeight()); 
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to give your drawable bounds. You need to call drawable.setBounds() at least once before drawing it.

1 Comment

What bounds should I set if I want the image to be tiled all over the screen though? Setting to (0, 0, 20, 20) gives me the same problem, it doesn't appear.
1

I seem to have fixed this problem with the following code

//background Bitmap _back_bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.background); BitmapDrawable backTiled = new BitmapDrawable(_back_bmp); backTiled.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); backTiled.setBounds(0, 0, this.getWidth(), this.getHeight()); this.back_bmp = backTiled.getTileModeX(); this.setBackgroundDrawable(backTiled); 

But I have my own problem now. Nothing can be drawn to the canvas?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.