Android: BitmapDrawable.Draw (Canvas) Doesn't seem to work

I'm trying to focus a 20x20 background on my user view, but for some reason I can't either.

    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 is not working?

+3
source share
3 answers

Do not set tile size boundaries: set them in the common area to be split. In your case:

background.setBounds(0, 0, myView.getWidth(), myView.getHeight());
+6
source

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

+2
source

I seem to have fixed this issue 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 now I have my own problem. Can't be painted on canvas?

+1
source

Source: https://habr.com/ru/post/1794057/


All Articles