android surfaceview runnable正确的延迟方式?

时间:2013-11-21 02:12:29

标签: java android multithreading handler runnable

好的,所以我似乎无法让这个工作,我正在尝试找到一个向surfaceview runnable添加延迟的正确方法。我最初只是使用Thread.sleep开始,但谷歌上有很多条目说使用Thread.sleep进行运行是不好的,我一直试图“正确”使用处理程序来实现基本相同的目标。到目前为止,这是我的代码,我已经测试过,请注意处理程序后延迟的注释和位置

public class demosf extends Activity {

    OurView v;
    int Measuredwidth;
    int Measuredheight;
    WindowManager w;
    Bitmap whatever;
    LinearLayout llMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Measuredwidth = 0;
        Measuredheight = 0;
        whatever = BitmapFactory
                .decodeResource(getResources(), R.raw.dragarrow);

        llMain = new LinearLayout(this);

        setContentView(llMain);
        v = new OurView(demosf.this);

        llMain.post(new Runnable() {

            @Override
            public void run() {

                Measuredwidth = llMain.getWidth();
                Measuredheight = llMain.getHeight();

                llMain.addView(v);
            }
        });
    }
//the Runnable:
    public class OurView extends SurfaceView implements Runnable {

        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK;
        Handler handler = new Handler();

        public OurView(Context context) {
            super(context);
            holder = getHolder();
        }

        @Override
        public void run() {
            //handler.postDelayed placed here freezes app
            while (isItOK) {
                if (!holder.getSurface().isValid()) {
                    continue;
                }
                Canvas c = holder.lockCanvas();
                c.drawARGB(255, 0, 0, 0);
                c.drawBitmap(
                        whatever,
                        ((float) this.getWidth() - (float) whatever.getWidth()),
                        ((float) this.getHeight() - (float) whatever
                                .getHeight()), null);
                holder.unlockCanvasAndPost(c);

                //handler.postDelayed placed here also freezes app
            }
            //this one isn't reached
            handler.postDelayed(this, 100);
        }

        public void pause() {
            isItOK = false;
            while (true) {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }

        public void resume() {
            isItOK = true;
            t = new Thread(this);
            t.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        v.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        v.resume();
    }

}

所以我只是坚持使用Thread.sleep?或者我如何在这里使用Handler?我是个新手,所以任何帮助都会非常感激=)

1 个答案:

答案 0 :(得分:0)

在实例化Handler时,您正在创建OurView,这意味着处理程序将使用当前线程,在这种情况下是UI线程 - 这不是您想要的。

请参阅Looper了解如何在后台Looper上正确创建Thread,然后可以使用该Handler创建背景{{1}}。

框架中有一个名为HandlerThread的辅助类,可以为你做这个样板。