I have the following code with LinearGradient, which looks the same as all other examples.
public class CustomColourBar extends View
{
public CustomColourBar( Context context, AttributeSet attribs )
{
super( context, attribs );
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(170, 40);
}
@Override
protected synchronized void onDraw( Canvas canvas )
{
int height = this.getMeasuredHeight();
int width = this.getMeasuredWidth();
LinearGradient shader = new LinearGradient(
0, 0, 0, height,
Color.RED, Color.YELLOW,
Shader.TileMode.CLAMP );
Paint paint = new Paint();
paint.setShader(shader);
RectF fgRect = new RectF( 0, 0, width, height);
canvas.drawRoundRect(fgRect, 7f, 7f, paint);
}
}
In the layout, this leads to the following, which roughly corresponds to the rule:

However, when other things change the Y position of my view, this happens incorrectly:

LinearGradient uses the absolute position relative to the top view (i.e. the dialog box). I can’t understand for life why?
Thank!
Rob
source
share