Skip to content

Instantly share code, notes, and snippets.

@arch-jslin
Created February 23, 2011 17:45
Show Gist options
  • Save arch-jslin/840797 to your computer and use it in GitHub Desktop.
Save arch-jslin/840797 to your computer and use it in GitHub Desktop.
surfaceview.java
//igdshare 110220: code provided by hsufong
class MainPlan extends SurfaceView implements SurfaceHolder.Callback {
private DrawPlanThread drawplanThread;
public MainPlan(Context context) {
super(context);
getHolder().addCallback(this);
drawplanThread = new DrawPlanThread(getHolder(), this);
}
private boolean XYInRect(int fX,int fY,Rect fRect){
boolean tInside = false;
if(fY > fRect.top && fY <= fRect.bottom ){
if(fX >= fRect.left && fX < fRect.right){
tInside = true;
}
}
return tInside;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(stopInput == true){
return true;
}
int touchCity = -1;
int absX = 0;
int absY = 0;
mx = (int) event.getX();
my = (int) event.getY();
absX = mapStartX + mx;
absY = mapStartY + my;
if(event.getAction() == event.ACTION_DOWN){
}
if(event.getAction() == event.ACTION_UP){
}
if(event.getAction() == event.ACTION_MOVE){
}
return true;
}
class DrawPlanThread extends Thread {
private SurfaceHolder _surfaceHolder;
private MainPlan mainMap;
private boolean _run = false;
private boolean _draw = false;
public DrawPlanThread(SurfaceHolder surfaceHolder, MainPlan map) {
_surfaceHolder = surfaceHolder;
mainMap = map;
}
public void setRunning(boolean run) {
_run = run;
}
public void setDrawing(boolean fDraw) {
_draw = fDraw;
}
public boolean isDrawing() {
return _draw;
}
@Override
public void run() {
Canvas c;
while (_run) {
if(_draw){
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
mainMap.onDraw(c);
}
} 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) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}else{
try {
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment