Lucas Romier
Lucas Romier

Reputation: 1377

Line that's supposed to turn around point not doing what it is supposed to do

I am updating a function in a thread, to make a line follow a circular path in android:

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    renderEnvironmentalVariables(canvas);
}

private void renderEnvironmentalVariables(Canvas canvas){
    while(angle > 360){
        angle -= 360;
    }
    canvas.drawColor(0xFFe6e6e6);
    //renderBaseRadar(canvas);
    float origin = width / 2;
    float x = origin + (float) Math.cos(angle) * (origin);
    float y = origin + (float) Math.sin(angle) * (origin);

    Log.i("Radio frequency tracker", y + "_" + x);

    Paint lineColor = new Paint();
    lineColor.setColor(0xFFFFFFFF);
    lineColor.setStyle(Paint.Style.STROKE);
    lineColor.setStrokeWidth(5);

    canvas.drawLine(origin, origin, x, y, lineColor);
    angle += 20;
}

onDraw(canvas);
//Log.i("Radio frequency tracker", "Updating canvas thread");
try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Instead if turning rather smoothly around the origin point, the line stays in one place and doesn't move, even though the Log function logs new coordinates...

What am I doing wrong and how can I fix it?

Upvotes: 0

Views: 15

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93728

You shouldn't call onDraw directly like that. Call postInvalidate instead.

Upvotes: 1

Related Questions