How to repeat a pattern in user view to borders?

I want to limit the pattern of repeating a set of random small patterns in a specific area. I create one object (CustomView) using canvas, I have knowledge on how to repeat a template on a layout using xml code.

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

this does not work for canvas.

I essentially want to use the bitmap as the background image for the user view and would like to repeat the bitmap in both the X and Y directions of view.

Look at this image

enter image description here

+5
source share
2 answers

Try using this code: -

paint = new Paint(Paint.FILTER_BITMAP_FLAG);
Shader mShader1 = new BitmapShader(bitmap, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
paint.setShader(mShader1);
+7
source

You can make your custom component with a repeating raster background:

BitmapDrawable bitmapBg = new BitmapDrawable(BitmapFactory.decodeResource(
            getResources(), R.drawable.repeatbg));
bitmapBg.setTileModeX(Shader.TileMode.REPEAT);
image.setBackgroundDrawable(bitmapBg);

, , bg.

public static Bitmap getMaskedContactImage(Context context,
        Bitmap contactImageBitmap, int maskToBeApplied) {
    Bitmap mask = BitmapFactory.decodeResource(context.getResources(),
            maskToBeApplied);
    Bitmap output = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(),
            Config.ARGB_8888);
    final Rect finalRect = new Rect(0, 0, contactImageBitmap.getWidth(),
            contactImageBitmap.getHeight());
    final Rect originRect = new Rect(0, 0, mask.getWidth(),
            mask.getHeight());
    Canvas canvas = new Canvas(output);

    Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    xferPaint.setColor(Color.BLACK);

    xferPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    canvas.drawBitmap(contactImageBitmap, finalRect, originRect, null);
    canvas.drawBitmap(mask, originRect, originRect, xferPaint);

    contactImageBitmap.recycle();
    mask.recycle();

    return output;
}

, .

, , .

,

+2

All Articles