Вопрос

I'm creating my own view. Right now all I want to do is draw a rounded rect with a color gradient.

Here is how I create the gradient:

public void changePressedColor(int[] colors){
    if (colors != null){
        if (colors.length == 2){
            pressedcolors = colors;
            whenpressed = new LinearGradient(0,0,getWidth(),getHeight(),pressedcolors[0],pressedcolors[1], Shader.TileMode.MIRROR);
        }
    }
}

Wherre pressed colors are passed as a parameter from:

    int[] pc = new int[2];
    pc[0] = Color.rgb(0,0,50);
    pc[1] = Color.rgb(114,112,255);

Then when I draw I do this:

protected void onDraw(Canvas canvas){

    painter.setShader(whenpressed);
    painter.setStyle(Paint.Style.FILL);
    canvas.drawRoundRect(rect,rx,ry,painter);

    painter.setShader(null);
    painter.setStyle(Paint.Style.STROKE);

    painter.setColor(textColor);
    canvas.drawText(text,cx,cy+offsety,painter);
}

The problem is that no gradient appears. Instead there is a solid color equal to first color of the gradient. The height of the view, rect and therefore the gradient is the same.

Any idea of what I'm doing wrong.

Thanks for any help.

Это было полезно?

Решение

Be careful when you call getWidth() and getHeight(), they may not have been calculated yet.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top