¿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.

<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.

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);
paint.setShader(mShader1);

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));
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.

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.