If you think the Android project slider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package de.devisnik.android.sliding;
/*www.java2s.com*/import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.SurfaceHolder;
publicclass RenderHandler extends Handler {
privatestaticfinalint MAX_FPS = 60;
privatestaticfinalint TARGET_DURATION = 1000 / MAX_FPS;
privatestaticfinalint ACTION_REDRAW = 1;
privatestaticfinal String TAG = "RenderHandler";
long lastDraw = 0;
privatefinal SurfaceHolder mHolder;
privatefinal FrameDrawer mFrameDrawer;
public RenderHandler(final Looper looper, final FrameDrawer frameDrawer, final SurfaceHolder holder) {
super(looper);
mFrameDrawer = frameDrawer;
mHolder = holder;
}
@Override
publicvoid handleMessage(final Message msg) {
if (msg.what == ACTION_REDRAW)
redraw();
else
Logger.e(TAG, "unable to handle message: " + msg);
}
privatevoid redraw() {
long startTime = SystemClock.uptimeMillis();
Canvas c = null;
try {
lastDraw = startTime;
Rect dirtyRegion = mFrameDrawer.getDirtyRegion();
c = mHolder.lockCanvas(dirtyRegion);
if (c != null)
mFrameDrawer.draw(c, dirtyRegion);
// if (Logger.isDebugEnabled())
// mFrameDrawer.drawFpsInfo(c, dirtyRegion.centerX(), dirtyRegion.centerY());
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null)
mHolder.unlockCanvasAndPost(c);
long drawDuration = SystemClock.uptimeMillis() - startTime;
long delay = Math.max(1, TARGET_DURATION - drawDuration);
sendMessageDelayed(obtainMessage(ACTION_REDRAW), delay);
}
}
publicvoid postDraw() {
sendMessage(obtainMessage(ACTION_REDRAW));
}
}