【发布时间】:2011-02-26 00:58:48
【问题描述】:
我有以下带有 LinearGradient 的代码,它看起来与那里的所有其他示例大致相同。
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);
}
}
在布局中,这会产生以下内容,这几乎是正确的:
但是,当其他事情改变我视图的Y位置时,它就出错了:
LinearGradient 使用相对于最顶层视图(即对话框)的绝对位置。我一辈子都想不通 - 为什么?
谢谢!
罗伯
【问题讨论】:
-
我已经放弃了,现在使用 Ninepatch 图形(有渐变)。我的图形是白色的,所以我还借用了一些代码来做一个“着色”过滤器,它可以让我控制颜色。我发现所有渐变(径向渐变和我忘记的渐变)都以相同的方式表现,并且我看不到任何活动/布局不可知的方式来使它们起作用。即使检索和使用屏幕上的位置也无济于事 - 在一种配置上运行良好,但在其他配置上运行良好。