¿Cómo repetir un patrón en la vista personalizada hasta los límites?

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.

  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.

mira esta imagen

enter image description here

preguntado el 04 de julio de 12 a las 07:07

2 Respuestas

Intente con este código: -

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

Respondido 10 Jul 12, 11:07

Me puede ayudar con esto stackoverflow.com/questions/19264300/… - Goofy

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

BitmapDrawable bitmapBg = new BitmapDrawable(BitmapFactory.decodeResource(
            getResources(), R.drawable.repeatbg));

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(),
    Bitmap output = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(),
    final Rect finalRect = new Rect(0, 0, contactImageBitmap.getWidth(),
    final Rect originRect = new Rect(0, 0, mask.getWidth(),
    Canvas canvas = new Canvas(output);

    Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

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

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


    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.

Saludos, Alex

Respondido 06 Jul 12, 09:07

why im unable to import Shader, is it requires any jar files. - RajaReddy PolamReddy

No any special need, developer.android.com/reference/android/graphics/Shader.html since API 1. Its a bitmapDrawable method for repeat mode desarrollador.android.com/reference/android/graphics/drawable/…, android.graphics.Shader.TileMode) - Goofyahead

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

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

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.