0

画像を描画するカスタム View クラスがあります。問題は、 Thread.sleep(200); を使用するときです。onDraw メソッドでは、同じアクティビティにある XML 要素にも影響します。そのため、何かが起こるまで 200 ミリ秒待たなければなりません。

MainPage extends Activity implements OnClickListener{
    onCreate(...){

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.main_rl);
        rl.addView(new CustomView(this));
    }

    onClick(View v){
        switch(v.getId){
            ...
        };
    }
}


CustomView extends View{

    onDraw(){
        try{
            Thread.sleep(200);
            ....
        }
    }
}

どんな助けでも大歓迎です。

4

1 に答える 1

1

Never run long operations in any UI listener callback (or anywhere else that runs on the UI thread) in Android. Instead, create a new thread to perform the sleep and then do whatever needs to be done. Just remember that if you want to interact with the UI again, you need to do that within the UI thread, which you can schedule by calling runOnUiThread().

CustomView extends View{

    onDraw(){
        (new Thread(myRunnable)).start();
    }
}

Runnable myRunnable = new Runnable() {
    void run() {
        try { Thread.sleep(...); } catch...;
        do_non_ui_things_if_needed();
        runOnUiThread(some_runnable_for_this_if_needed);
    }
}
于 2013-02-06T12:14:39.340 に答える