mrqs
Android Expert
Code:
public class V extends SurfaceView implements SurfaceHolder.Callback {
private CThread mThread; // see next code block for class
public int x;
public int y;
public V(Context context) {
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
mThread = new CThread(holder,this);
x = 0;
y = 0;
setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
}
@Override
public void onDraw(Canvas canvas)
{
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawLine(x, y, getWidth(), getHeight(), paint);
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
x = (int) event.getX();
y = (int) event.getY();
default: break;
}
return true;
}
public void surfaceCreated(SurfaceHolder holder) {
mThread.setRunning(true);
mThread.start();
}
/* sufraceDestroyed etc */
}
Code:
public class CThread extends Thread {
private SurfaceHolder mSurfaceHolder;
private V mView;
private boolean mRun = false;
public CThread(SurfaceHolder surfaceHolder, V view) {
mSurfaceHolder = surfaceHolder;
mView = view;
}
public void setRunning(boolean run) {
mRun = run;
}
@Override
public void run() {
Canvas c;
while (mRun) {
c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
mView.onDraw(c);
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
what's up with that?