I am having trouble using a Drawable with ShaderFactory in a LayerDrawable. It is showing all black instead of to the two layers (none of which should be all black). It worked when I used a static Shader using drawable.getPaint().setShader(), but not now with ShaderFactory.
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
Log.d("shader", width+"x"+height);
LinearGradient gradient = new LinearGradient(0, 0, width, height,
new int[] { 0x33FF0000, 0x00000000, 0x00000000, 0x11000000 },
new float[] { 0.0f, 0.04f, 0.97f, 1.0f }, Shader.TileMode.CLAMP);
return gradient;
}
};
ShapeDrawable grad = new ShapeDrawable();
grad.setShaderFactory(sf);
Drawable[] layers = new Drawable[] { anotherDrawable, grad };
Drawable d = new LayerDrawable(layers);
someImageView.setImageDrawable(d);
It turns out the problem was that I did not specify the shape of the ShapeDrawable.
Changed to this and it is now good. Another thing was that I actually wanted to specify the linear gradient from top to bottom so I don’t need the width, and the effect makes me didn’t notice it. So in fact the drawing it with PaintDrawable also works.