caw
caw

Reputation: 31487

Speed up SurfaceView in Android that is only updated once every few seconds

When you use SurfaceViews in Android, especially when setting them as part of layouts (by adding the SurfaceView into the layout XML), performance is a crucial aspect.

In my case, the View is only updated once every few seconds (containing playing cards like in Poker games, with some effects and moving on touch events) and as it is part of a larger layout, I experience the problem that this SurfaceView slows down the rest of my Activity's UI, i.e. the rest of the Activity freezes for a short time and then it is updated so that a short period of time has not been shown.

public class MySurface extends SurfaceView implements SurfaceHolder.Callback {
    ...
}

In its onDraw() method, there are some Bitmaps drawn to a black background. Inside of that class, a thread is started that continuously calls the View's onDraw() method:

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class SurfaceThread extends Thread {

    private SurfaceHolder mSurfaceHolder;
    private MySurface mSurface;
    private boolean mRunning = false;

    public HandThread(SurfaceHolder surfaceHolder, MySurface surface) {
        mSurfaceHolder = surfaceHolder;
        mSurface = surface;
    }

    public SurfaceHolder getSurfaceHolder() {
        return mSurfaceHolder;
    }

    public void setRunning(boolean run) {
        mRunning = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (mRunning) {
            c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    if (c != null) {
                        mSurface.onDraw(c);
                    }
                }
            }
            finally { // when exception is thrown above we may not leave the surface in an inconsistent state
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

}

Unfortunately, this slows down my Activity from time to time, that means that there are freezes of ca. 0.5 seconds quite often.

Is there any possibility to speed things up? I've tried to abandon the Thread and call invalidate() in every call of onTouchEvent(), but this did not work, either.

I think there must be a way to improve performance because the SurfaceView is updated that infrequently. There are seconds where nothing happens but when the user touches the View, things are moving around until the finger lifts up.

Upvotes: 0

Views: 1203

Answers (1)

kabuko
kabuko

Reputation: 36302

You'll want to analyze what is really causing those pauses. Trace through a typical run and see what is actually using up the CPU time. Chances are, something is causing the UI to freeze up, you're doing a lot of work that should be moved to another thread.

You also want to avoid unnecessary work. Something you definitely want to look at is to make sure that in your background loop (including in the call to onDraw) you avoid creating new objects as much as possible. This will reduce garbage collection which can also cause hiccups. Also avoid or minimize loading bitmaps here if at all possible too.

Upvotes: 1

Related Questions