Tiled Drawable Sometimes Stretches

Make bitmap drawable tile in x but stretch in y

This method invokes creation of new bitmap but it looks like it's acrhiving your goal

        View view = findViewById(R.id.layout);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.tile);
int width = view.getWidth();
int intrinsicHeight = bd.getIntrinsicHeight();
Rect bounds = new Rect(0,0,width,intrinsicHeight);
bd.setTileModeX(TileMode.REPEAT);
bd.setBounds(bounds);
Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), bd.getBitmap().getConfig());
Canvas canvas = new Canvas(bitmap);
bd.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
view.setBackground(bitmapDrawable);

Please note that it only works if view was already layouted, so a method lile onWindowFocusChanged is a good place for this code.

Repeated background image is stretched

I'm not sure what Raghav's referring to with the bug, exactly, but if they've changed something in ICS+ then there was certainly something wrong. That said, I'm fairly certain that bitmap images in drawable are assumed to be mdpi, and scaled from there (maybe they changed that behavior?), which makes sense given that your 60px image became 90px (60 * 1.5 scaling factor).

You can avoid scaling altogether, keeping in mind that the physical size will differ on different densities (although for a tiled background it's probably not important), by placing your bitmaps into a drawable-nodpi folder only. Android will use those drawables without scaling, regardless of density.

tileMode repeat in drawable won't work

Unfortunately yes - it's Android bug. Check this answer

Tiled drawable sometimes stretches

In short: you should set repeating in Java code instead of XML.

XML drawable Bitmap tileMode bug?

This is a known bug, partially fixed in Android 3.0 and completely fixed in ICS.

Android - bitmap tileMode=repeat not work correct

I copy my bitmap xml for each view and problem solve Greatly

StateListDrawable and tiled bitmap

This is the bug, it was fixed in ICS, see this answer: https://stackoverflow.com/a/7615120/1037294

Here is a workaround: https://stackoverflow.com/a/9500334/1037294

Note, that the workaround is only applicable for BitmapDrawable, for other types of drawables like StateListDrawable you'll need to do extra work. Here is what I use:

public static void fixBackgrndTileMode(View view, TileMode tileModeX, TileMode tileModeY) {
if (view != null) {
Drawable bg = view.getBackground();

if (bg instanceof BitmapDrawable) {
BitmapDrawable bmp = (BitmapDrawable) bg;
bmp.mutate(); // make sure that we aren't sharing state anymore
bmp.setTileModeXY(tileModeX, tileModeY);
}
else if (bg instanceof StateListDrawable) {
StateListDrawable stateDrwbl = (StateListDrawable) bg;
stateDrwbl.mutate(); // make sure that we aren't sharing state anymore

ConstantState constantState = stateDrwbl.getConstantState();
if (constantState instanceof DrawableContainerState) {
DrawableContainerState drwblContainerState = (DrawableContainerState)constantState;
final Drawable[] drawables = drwblContainerState.getChildren();
for (Drawable drwbl : drawables) {
if (drwbl instanceof BitmapDrawable)
((BitmapDrawable)drwbl).setTileModeXY(tileModeX, tileModeY);
}
}
}
}
}


Related Topics



Leave a reply



Submit