2

I'm trying to draw some animations in a view by invalidating it, thus changing the positions in the onDraw method.

Looks something like this:

Controller:

public void setStateProgress(int percent) {
    stateProgress = percent;
    view.invalidate();
}

Thread:

public void run() {
    long startTime = System.currentTimeMillis();

    while (activity.STATE != MainActivity.State.ANIMATION_FINISHED) {
        long timeDiff = System.currentTimeMillis() - startTime;

        int percent = Math.round(((float) MainActivity.TIME_PER_STATE / 10000f)
                * timeDiff);

        // if this state's time is over, move on to the next.
        if (timeDiff >= MainActivity.TIME_PER_STATE
                && activity.STATE != MainActivity.State.ANIMATION_FINISHED) {
            activity.setState(activity.STATE + 1);
            startTime = System.currentTimeMillis();
            percent = 0;
        }
        final int finalPercent = percent;

        activity.runOnUiThread(new Runnable() {
            public void run() {
                Log.d(TAG, "Updating progress to "+finalPercent+"%, invalidating view...");
                activity.getAnalyzeController().setStateProgress(
                        finalPercent);
            }
        });
        try {
            Thread.sleep(50);
        } catch (Exception e) {

        }
    }
}

I've played around with the sleep time, but even with 50ms sleep time it does not seem to re-draw the view.

In the onDraw method of the view is not too much. Just some lines and rectangles for testing purpose. Anyone got an idea how to speed things up? Should I handle the thread in a different way? I'm open for suggestions, timing in threads is not my speciality. :)

Thanks for your help.

EDIT:

My guess: By runOnUiThread I invoke another thread passed to some handler (?) and this takes a while.. but how should I do it otherwise?

4

0 回答 0