Android Tile Bitmap

Android Tile Bitmap

You would do this in the xml instead of the java code. I haven't attempted this myself but I did find this example.

<xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/backrepeat"
>

then in an xml called backrepeat.xml

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/back"
android:tileMode="repeat" />

reference

Android: Drawing tiled bitmaps with bottom or some other alignments similar to css background-position

Another way would be to extend BitmapDrawable and override the paint() method:

In this method we avoid creating a new bitmap having the size of the view.

class MyBitmapDrawable extends BitmapDrawable {
private Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
private boolean mRebuildShader = true;
private Matrix mMatrix = new Matrix();

@Override
public void draw(Canvas canvas) {
Bitmap bitmap = getBitmap();
if (bitmap == null) {
return;
}

if (mRebuildShader) {
mPaint.setShader(new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT));
mRebuildShader = false;
}

// Translate down by the remainder
mMatrix.setTranslate(0, getBounds().bottom % getIntrinsicHeight());
canvas.save();
canvas.setMatrix(mMatrix);
canvas.drawRect(getBounds(), mPaint);
canvas.restore();
}
}

It can be set to the view like this:

view.setBackgroundDrawable(new MyBitmapDrawable(getResources().getDrawable(R.drawable.smiley).getBitmap()));

How do you scale and tile a bitmap drawable being used as a background?

In my experience it is better to create different sized bitmaps for each density you are targeting. That way you don't have to worry about these scale attributes and get crisp results on any device.

These scale attributes are not defined for BitmapDrawables. They exist for ScaleDrawables which are meant to scale another drawable. Have a look at:

http://developer.android.com/guide/topics/resources/drawable-resource.html#Scale

Android Compose - How to tile/repeat a bitmap/vector?

If you want to use native canvas you can do something like this in jetpack compose.


Canvas(
modifier = Modifier
.fillMaxSize()
) {

val paint = Paint().asFrameworkPaint().apply {
isAntiAlias = true
shader = ImageShader(pattern, TileMode.Repeated, TileMode.Repeated)
}

drawIntoCanvas {
it.nativeCanvas.drawPaint(paint)
}
paint.reset()
}

And If you want to limit your repetition to a certain height and width you can use the clip modifier in canvas like below otherwise it will fill the entire screen.


Canvas(
modifier = Modifier
.width(300.dp)
.height(200.dp)
.clip(RectangleShape)
) {
----
}

Tiling a Bitmap on a Canvas

You have two easy solutions:

  • Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
  • Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).

Tile a Bitmap on SurfaceView Canvas

It isn't easy to understand what you are trying to do...

How do you encode coordinates into that grassCoords array? As its current form it has 5x5 elements.

int[][] grassCoords = new int[][] { { 0, 16, 32, 48, 64 },
{ 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 },
{ 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 } };

Since it has grass in its name I assume you want to draw only grass, then you could define it like this

int[][] grassCoords = new int[][] { {0, 0}, {16, 16}, {32, 32} };

Above each element like {0, 0} would be a single coordinate for a grass tile.

Second issue is with your loop, you don't read any data from grassCoords except arrays length and when you increment the index you increment it with grass.getWidth() which doesn't really make sense.

    int x = 0;
int y = 0;

for (x = 0; x < grassCoords.length; x += grass.getWidth()) {

for (y = 0; y < grassCoords.length; y += grass.getHeight()) {

c.drawBitmap(grass, x, y, null);
}

}

You should iterate array correctly and fetch the data from it.

    int x = 0;
for (x = 0; x < grassCoords.length; x++) {
c.drawBitmap(grass, grassCoords[x][0], grassCoords[x][1], null);
}

If I were you I would study related parts of Java tutorial at least once.



Related Topics



Leave a reply



Submit