I am trying to create rectangle with rounded corners and background as repeated bitmaps. I am writing like this, but getting bitmaps in the corners.
Could anyone help out?
background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<corners android:radius="50dp" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/carbon_4"
android:tileMode="repeat" />
</item>
</layer-list>
I found this post by Romain Guy that's a much easier way to round corners on a tiled bitmap. Here's the short answer:
class CurvedAndTiled extends Drawable {
private final float mCornerRadius;
private final RectF mRect = new RectF();
private final BitmapShader mBitmapShader;
private final Paint mTilePaint;
CurvedAndTiled(
Bitmap bitmap,
float cornerRadius) {
mCornerRadius = cornerRadius;
mBitmapShader = new BitmapShader(bitmap,
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mTilePaint = new Paint();
mTilePaint.setAntiAlias(true);
mTilePaint.setShader(mBitmapShader);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRect.set(0, 0, bounds.width(), bounds.height());
}
@Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mTilePaint);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void setAlpha(int alpha) {
mTilePaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
mTilePaint.setColorFilter(cf);
}
}
You just set your view's background drawable to be one of these guys and you're good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With