在某些时候更改Android库中的图像...帮助

时间:2011-02-01 14:27:30

标签: java android handler image-gallery

我的代码在这里工作正常。它会在某些时候更改我的库中的图像,但我不认为这是最好的方法,尤其是使用thread.sleep()。我怎样才能为此编写更好的代码?我错过了什么? 感谢..

@覆盖     protected void onResume(){         paraThread = true;         workThread = new LooperFoto(“MyWorkThread”);         workThread.setPriority(从Thread.MIN_PRIORITY);         workThread.start();         super.onResume();     }

@Override
protected void onStop() {
    Log.i(TAG, "thread state: " + workThread.getState());
    paraThread = false;
    workThread = null;
    super.onStop();
}


class LooperFoto extends HandlerThread {

    public LooperFoto(String name) {
        super(name);

    }

    @Override
    public void run() {
        while (paraThread) {
            try {
                this.sleep(2000);
                mudaFoto.sendMessage(new Message());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}

Handler mudaFoto = new Handler() {
    public void handleMessage(Message msg) {
        Random generator = new Random();
        int randomIndex = generator.nextInt(thumbImgGallery.getAdapter()
                .getCount());
        thumbImgGallery.setSelection(randomIndex);
    };
};

1 个答案:

答案 0 :(得分:0)

只要.sleeping()的线程不是你的主(UI)线程,你应该没问题。你在担心什么?