Since this question is of some interest, I will answer.
The best way to do this is to create a separate canvas stream. A “stand-alone” canvas can only be achieved with SurfaceView . LunarLanding is a great example of this use. Each frame is calculated separately than the main view, dividing only the processor time, and not the drawing time. Therefore, faster, even with a combination, for example, from the usual view at the top and animating view below.
But you must set the interval if you are in this shared environment. This interval is used for the FPS cover. If you do not install the FPS cap, then the CPU will operate flawlessly to get good animation for the SurfaceView if it were one. Capping it at a speed of 60 frames per second or less will do the trick to efficiently use all the views without overloading the CPU.
So, look at the Moon Landing drawing drawing from the API demos and set the FPS cap.
private long timeNow; private long timeDelta; private long timePrevFrame; private void capFps(int fps) { timeNow = System.currentTimeMillis(); timeDelta = timeNow - timePrevFrame; try {
// ps you can always set 16 instead of 1000 / fps for 60FPS to avoid calculating every time
Thread.sleep((1000 / fps) - timeDelta); } catch (InterruptedException e) { } timePrevFrame = System.currentTimeMillis(); }
and then the drawing thread will look something like this:
@Override public void run() { Canvas c; while (run) { c = null; sleepFps(60, false); try { synchronized (surfaceHolder) { c = surfaceHolder.lockCanvas(null); widgetView.doDraw(c); } } finally { if (c != null) { surfaceHolder.unlockCanvasAndPost(c); } } } }
weakwire
source share