【问题标题】:SurfaceView implements Runnable - Thread does not startSurfaceView 实现 Runnable - 线程不启动
【发布时间】:2012-03-23 08:07:53
【问题描述】:

一直在寻找一些使用 SurfaceView 绘制画布的教程,但唯一出现的是黑色背景。

public class FighterActivity extends Activity implements OnTouchListener {
    /** Called when the activity is first created. */
    SurfaceController surface;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surface = new SurfaceController(this);
        surface.setOnTouchListener(this);
        setContentView(surface);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        surface.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        surface.resume();
    }

    public class SurfaceController extends SurfaceView implements Runnable{

        Thread thread = null;
        SurfaceHolder holder;

        public SurfaceController(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            holder = getHolder();
            System.out.println("HERE");
        }

        public void run() {
            // TODO Auto-generated method stub
            System.out.println("Hello World2");
            while(true){
                if(!holder.getSurface().isValid()){
                    System.out.println("NOT VALID");
                    continue;
                }
                System.out.println("VALID!");
                Canvas can = holder.lockCanvas();
                can.drawARGB(255, 150, 150, 0);
                holder.unlockCanvasAndPost(can);
            }
        }

        public void pause(){

        }

        public void resume(){

        }

    }

    public boolean onTouch(View view, MotionEvent me) {
        // TODO Auto-generated method stub
        return false;
    }
}

它到达 System.out.println("HERE");并在此处打印出来,但没有更多的事情发生,换句话说,由于未打印“Hello World2”,因此线程没有启动,这是什么问题?

感谢您的帮助

【问题讨论】:

  • 你根本没有展示你是如何使用它的。听上去你是在调用构造函数,然后呢?

标签: android multithreading surfaceview android-canvas


【解决方案1】:

我假设您正在构建以下内容:http://android-coding.blogspot.ca/2011/05/drawing-on-surfaceview.html

您会注意到onResumeMySurfaceViewonPauseMySurfaceView(分别在SurfaceController 中的resumepause)启动了实际线程。您也需要在代码中执行此操作,例如在SurfaceController:

protected boolean running = false;

public void resume() {
    running = true;
    thread = new Thread(this);
    thread.start();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多