在Android中暂停和恢复SurfaceView中的线程
我有一个表面视图,我用一根特殊的线周期性地画一个小圆圈 但我想当我按下surfaceView时,线程停止绘制圆,当我按下它时,线程再次恢复绘制圆 我尝试了线程方法,我可以用它的sleep方法暂时停止它,但我不知道如何使用wait和notify,我甚至找到了一些示例,但没有得到它们的帮助 我的代码是:在Android中暂停和恢复SurfaceView中的线程,android,Android,我有一个表面视图,我用一根特殊的线周期性地画一个小圆圈 但我想当我按下surfaceView时,线程停止绘制圆,当我按下它时,线程再次恢复绘制圆 我尝试了线程方法,我可以用它的sleep方法暂时停止它,但我不知道如何使用wait和notify,我甚至找到了一些示例,但没有得到它们的帮助 我的代码是: public class GameView extends SurfaceView implements SurfaceHolder.Callback { private float
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private float x = 100;
private float y = 100;
private int radius = 20;
private Paint paint;
private SurfaceHolder mSurfaceHolder;
private DrawingThread mTh ead;
private Context myContext;
public GameView(Context context) {
super(context);
this.myContext = context;
setWillNotDraw(false);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Paint.Align.LEFT);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
}
public void onDraw(Canvas canvas){
canvas.drawCircle(x, y, radius, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
// I want to do my job here
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mThread = new DrawingThread(mSurfaceHolder, myContext);
mThread.mRun = true;
mThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
public final class DrawingThread extends Thread {
public boolean att = true;
public long delaiAttente = 1000;
boolean mRun;
Canvas mcanvas;
SurfaceHolder surfaceHolder;
Context context;
public DrawingThread(SurfaceHolder sholder, Context ctx)
{
surfaceHolder = sholder;
context = ctx;
mRun = false;
}
void setRunning(boolean bRun)
{
mRun = bRun;
}
boolean keepDrawing = true;
@Override
public void run() {
while (keepDrawing) {
Canvas canvas = null;
try {
canvas = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder) {
draw(canvas);
}
}
catch(Exception e){
}
finally {
if (canvas != null)
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
waitThreaed();
}
}
public void waitThreaed() {
try {
x = (float) (getWidth()*Math.random());
y = (float) (getHeight()*Math.random());
this.sleep(1000);
postInvalidate();
} catch (InterruptedException e) {
}
}
}
}
你就不能用这样的东西吗:
Timer drawTimer = new Timer("draw");
updateTimer.schedule(new TimerTask() {
public void run() {
draw();
}
}, 0, 1000);
private void draw() {
runOnUiThread(new Runnable() {
public void run() { ...}}}
我不明白你为什么需要对线程进行子类化。是的,我理解,但我如何能够在用户触摸时暂停并恢复你要绘制的绘图,并在释放时暂停?将其更改为计时器。开始和停止按键按否,我想听听这样的触摸事件:当用户触摸时,如果我正在画画,我将停止画画;如果我正在画画,我将重新画画对不起,我刚刚看到了您的代码,但我从未使用过计时器,我想知道如何使用thnxx