如何在libgdxjava中使用渐变色绘制曲线

如何在libgdxjava中使用渐变色绘制曲线,java,android,libgdx,Java,Android,Libgdx,我想用渐变色画一条曲线,就像这幅画一样 这是我的曲线绘制代码,但此代码仅适用于一种颜色 public class Test extends ApplicationAdapter{ //create paths private Bezier<Vector2> path1; private ShapeRenderer sr; @Override public void create () { // set up random control points int

我想用渐变色画一条曲线,就像这幅画一样

这是我的曲线绘制代码,但此代码仅适用于一种颜色

public class Test extends ApplicationAdapter{

//create paths
private Bezier<Vector2> path1; 
private ShapeRenderer sr;

@Override
public void create () {

    // set up random control points
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();
    int points = 4;
    Vector2[] controlPoints = new Vector2[points];
    for (int i = 0; i < points; i++) {
       int x = (int) (Math.random() * width) ;
       int y = (int) (Math.random() * height);
       Vector2 point = new Vector2(x, y);
       controlPoints[i] = point;
    }


    path1 = new Bezier<Vector2>(controlPoints);


    sr = new ShapeRenderer();
    sr.setAutoShapeType(true);
}




@Override
public void render () {
    Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    sr.begin();
    sr.setColor(Color.WHITE);

    //draw path1
    for(int i = 0; i < 100; ++i){
        float t = i /100f;
        // create vectors to store start and end points of this section of the curve
        Vector2 st = new Vector2();
        Vector2 end = new Vector2();
        // get the start point of this curve section
        path1.valueAt(st,t);
        // get the next start point(this point's end)
        path1.valueAt(end, t-0.01f);
        // draw the curve
        sr.line(st.x, st.y, end.x, end.y);

    }



    sr.end();
}

@Override
public void dispose () {
}

但我需要用渐变色绘制一条曲线,我已经搜索了很多,还没有找到任何解决方案


所以请帮助我。

您可以使用开始颜色和结束颜色之间的线性插值来查找每个部分的颜色。像这样:

Color color = new Color();

@Override
public void render() {
    Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    sr.begin();

    Color startColor = Color.YELLOW;
    Color endColor = Color.RED;

    //draw path1
    for (int i = 0; i < 100; ++i) {
        float t = i / 100f;
        //interpolate linearly between start and end colors
        sr.setColor(color
                .set(startColor)
                .lerp(endColor, t)
        );
        // create vectors to store start and end points of this section of the curve
        Vector2 st = new Vector2();
        Vector2 end = new Vector2();
        // get the start point of this curve section
        path1.valueAt(st, t);
        // get the next start point(this point's end)
        path1.valueAt(end, t - 0.01f);
        // draw the curve
        sr.line(st.x, st.y, end.x, end.y);
    }

    sr.end();
}
Color Color=新颜色();
@凌驾
公共无效呈现(){
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
高级贝京();
颜色开始颜色=Color.YELLOW;
Color endColor=Color.RED;
//绘制路径1
对于(int i=0;i<100;++i){
浮子t=i/100f;
//在开始颜色和结束颜色之间进行线性插值
高级设置颜色(颜色)
.set(起始颜色)
.lerp(endColor,t)
);
//创建向量以存储曲线此部分的起点和终点
Vector2 st=新Vector2();
Vector2 end=新Vector2();
//获取此曲线截面的起点
路径1.价值(st,t);
//获取下一个起点(此点的终点)
路径1.valueAt(结束,t-0.01f);
//画曲线
sr.line(st.x,st.y,end.x,end.y);
}
sr.end();
}
shapeRenderer.line(x, y, x2, y2, Color.RED, Color.GREEN);
Color color = new Color();

@Override
public void render() {
    Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    sr.begin();

    Color startColor = Color.YELLOW;
    Color endColor = Color.RED;

    //draw path1
    for (int i = 0; i < 100; ++i) {
        float t = i / 100f;
        //interpolate linearly between start and end colors
        sr.setColor(color
                .set(startColor)
                .lerp(endColor, t)
        );
        // create vectors to store start and end points of this section of the curve
        Vector2 st = new Vector2();
        Vector2 end = new Vector2();
        // get the start point of this curve section
        path1.valueAt(st, t);
        // get the next start point(this point's end)
        path1.valueAt(end, t - 0.01f);
        // draw the curve
        sr.line(st.x, st.y, end.x, end.y);
    }

    sr.end();
}