简体   繁体   English

在Android中的SurfaceView中暂停和恢复线程

[英]Pausing and resuming thread in SurfaceView in Android

I have a SurfaceView on wich am drawing a small circle periodically with a special thread but i want to when i press the surfaceView the thread stops drawing the circle and when i repress it the thread resume drawing the circle again. 我有一个SurfaceView,它会定期用特殊的螺纹绘制一个小圆圈,但是我想当我按surfaceView时,该线程停止绘制该圆圈,而当我按下它时,该线程又重新开始绘制该圆圈。

I tried with thread methods and i can stop temporarily it with its sleep() method but i did not understand how to use wait and notify and i even found some exemples but did not get help from them 我尝试使用线程方法,但可以使用其sleep()方法暂时停止它,但是我不了解如何使用wait and notify,甚至发现了一些示例,但没有得到他们的帮助

My code is : 我的代码是:

    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) {

            }
    }
   }

    }

Can't you just use something like this: 您不能只使用以下内容:

    Timer drawTimer = new Timer("draw");
    updateTimer.schedule(new TimerTask() {
        public void run() {
            draw();
        }
    }, 0, 1000);
    private void draw() {
    runOnUiThread(new Runnable() {
        public void run() { ...}}}

I don't see why you need to subclass Thread. 我不明白为什么您需要继承Thread。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM