Rob*_*ham 3 layout android gradient
我有以下代码和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);
}
}
Run Code Online (Sandbox Code Playgroud)
在布局中,这会生成以下内容,这几乎是正确的:

但是,当其他事情改变我的观点的Y位置时,它会出错:

LinearGradient使用相对于最顶层视图(即对话框)的绝对位置.我不能为我的生活弄明白 - 为什么?
谢谢!
抢
我有同样的问题.着色器中的位置不是形状而是画布相对.
因此,您需要在画布上始终围绕原点绘制形状,并将画布转换为所需位置.
例如:
不要:
canvas.drawCircle(posx, posy, radius);
Run Code Online (Sandbox Code Playgroud)
做:
canvas.save();
canvas.translate(posx, posy);
canvas.drawCircle(0,0,radius);
canvas.restore();
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助!;)
| 归档时间: |
|
| 查看次数: |
2008 次 |
| 最近记录: |