5

i want to restrict the repeat pattern of a set of random small pattern's to a particular region. I am creating one object ( CustomView ) by using canvas, I have knowledge how to repeat a pattern on the layout using xml code.

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

this is not working for canvas.

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

look at this image

enter image description here

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

2 Answers2

7

Please try with this code:-

paint = new Paint(Paint.FILTER_BITMAP_FLAG);
Shader mShader1 = new BitmapShader(bitmap, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
paint.setShader(mShader1);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
Kumar Shorav
  • 531
  • 4
  • 16
  • can you help me with this http://stackoverflow.com/questions/19264300/bitmap-with-tile-mode-repeat-and-round-corners – Goofy Oct 09 '13 at 07:10
2

You can make your custom component with a background bitmap repeated:

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

And you can make a mask with the image that you want like that triangle and your 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;
}

That will use a mask where the black color is the final image.

If i didn't misunderstood, that can achieve what you are showing in the capture.

Regards, Alex

Goofyahead
  • 5,874
  • 6
  • 29
  • 33
  • why im unable to import Shader, is it requires any jar files. – RajaReddy PolamReddy Jul 06 '12 at 08:57
  • No any special need, http://developer.android.com/reference/android/graphics/Shader.html since API 1. Its a bitmapDrawable method for repeat mode http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html#setTileModeXY(android.graphics.Shader.TileMode, android.graphics.Shader.TileMode) – Goofyahead Jul 06 '12 at 09:46
  • it's taking full view ( that repeat pattern was applying for total view other than my custom view i want to repeat it upto customview boundaries remaining view should be bg color). – RajaReddy PolamReddy Jul 06 '12 at 09:52
  • The custom view has that shape because is generated or its drawable based, i mean if its a shape that its always the same you can use the mask code for getting only the background on that shape and the rest on transparent, if its not the case, please explain further :) – Goofyahead Jul 06 '12 at 10:15
  • Actually i have frame layout on that i am adding customview by button click, i have to add more customview on the framelayout. when ever i create a custom with this drawable bg, it takes full screen when i create same custom view previous one goes to hide. i don't want like that i want repeat that pattern only inside custom view(Object). – RajaReddy PolamReddy Jul 06 '12 at 10:34
  • let me check it this afternoon :) – Goofyahead Jul 10 '12 at 09:08