0

に設定されているTextViewがあります。背景として 9 パッチの画像を使用します。TextView のコンテンツは定期的に更新されます (しばらくスリープしてからハンドラーを開始するスレッドを使用します)。数回繰り返した後、いくつかのアーティファクトがあります。以前のメッセージのテキストと背景の一部を見ることができます (幅は現在表示されているメッセージの幅よりも大きくなっています)。何が問題なのですか?layout_widthwrap_content

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   ...
}

private class PeriodicHelp extends Thread{
    private static final int SLEEP_TIME = 4000;
    private static final int NUM_HELP_PHRASES = 5;

    private String getHelp(){
        int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
        return helpPhrases[pos];
    }

    public void run(){
        int i = 0;
        while(true){
            try {
                Thread.sleep(SLEEP_TIME);
                Log.d(TAG, "periodicHelp " + Integer.toString(i));
                i++; //used just for debugging
                mHandler.post(new Runnable(){
                    public void run() {
                        String help = getHelp();
                        helpField.setText(help);
                    }
                });
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

PeriodicHelp スレッドを開始しますonStart()

4

1 に答える 1

0

私はこの問題を解決することができました。ここにコードがあります

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   rl = (RelativeLayout) findViewById(R.id.mainView); //this RelativeLayout contains
                                                      //helpField  TextView     
   ...
}

private class PeriodicHelp extends Thread{
private static final int SLEEP_TIME = 4000;
private static final int NUM_HELP_PHRASES = 5;

private String getHelp(){
    int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
    return helpPhrases[pos];
}

public void run(){
    int i = 0;
    while(true){
        try {
            Thread.sleep(SLEEP_TIME);
            Log.d(TAG, "periodicHelp " + Integer.toString(i));
            i++; //used just for debugging

            rl.postInvalidate();//THIS LINE FIX THE PROBLEM

            mHandler.post(new Runnable(){
                public void run() {
                    String help = getHelp();
                    helpField.setText(help);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

}

于 2012-12-12T12:05:22.640 に答える