瓷砖模式重复和圆角的位图
作者:互联网
我有一个带有圆角方法的位图:
码:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
BitmapDrawable TileMe = new BitmapDrawable(output);
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
Canvas canvas = new Canvas(TileMe);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
这仅使图像具有圆角,并且角也不平滑,现在我如何使其与重复模式以及圆角一起平铺?
解决方法:
终于我解决了!
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);
}
}
也将其应用于您的图像视图.
backgroundImage.setBackgroundDrawable(new CurvedAndTiled(((BitmapDrawable) drawable).getBitmap(), 45));
希望以后能对某人有所帮助.
快乐编码:)
标签:android,tile,bitmap,rounded-corners 来源: https://codeday.me/bug/20191013/1904606.html