I want to set a View background that is a regular grid of vertical stripes. The stripes alternate between two colors. (For example, across a single row, there might be 6 pixels of light gray followed by 2 pixels of dark gray, repeated to fill the width.)
It's easy enough to do this using a Bitmap (either as a resource or generated in code). For instance:
ShapeDrawable bg = new ShapeDrawable(new RectShape());
int[] pixels = new int[] { 0xFFCCCCCC, 0xFFCCCCCC, 0xFFCCCCCC,
0xFFCCCCCC, 0xFFCCCCCC, 0xFFCCCCCC, 0xFF999999, 0xFF999999};
Bitmap bm = Bitmap.createBitmap(pixels, 8, 1, Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(bm,
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
bg.getPaint().setShader(shader);
view.setBackgroundDrawable(bg);
Is there a way to do this strictly as XML drawables, without using bitmap resources?
BitmapShader
snippet. I was struggling to do this in XML and wasn't aware of the shader approach. – Bullfrog