-2

actualアプリケーションの負荷率が高いプログレスバーを介して、開始時にアプリケーションのスプラッシュ画面を表示したいと思います。

次の要件/クエリがあります-

  1. プログレスバーを表示するために使用するコンポーネント
  2. スプラッシュ画面自体がアプリケーションの一部である場合の負荷率の計算方法
  3. スプラッシュ画面に触れると、プログレスバーを強調表示したい
4

2 に答える 2

1

スレッドを作成するか、非同期タスクを使用してカスタム進行状況バーを作成できます

asynctask の例 (疑似コードのみ)

private class SplashLoading extends AsyncTask<Variable, Variable, Variable> {

     @Override
     protected void onPreExecute(Variable) {
         Show the progress UI in here
     }
     @Override
     protected Long doInBackground(Variable) {
        do the heavy task here and don't forget to publish the progress
     }

     @Override
     protected void onProgressUpdate(Variable) {
         set the progress here
     }

     @Override
     protected void onPostExecute(Variable) {
         what will you do after it complete?
     }
 }

私の asynctask 疑似コードは 4 つの関数で構成されています

  1. onPreExecute は、タスクが実行された直後に UI スレッドで呼び出されます。このステップは、通常、タスクをセットアップするために使用されます。たとえば、ユーザー インターフェイスに進行状況バーを表示します。

  2. doInBackground は、onPreExecute() の実行が終了した直後にバックグラウンド スレッドで呼び出されます。このステップは、時間がかかる可能性のあるバックグラウンド計算を実行するために使用されます。

  3. onProgressUpdate は、publishProgress(Progress...) の呼び出し後に UI スレッドで呼び出されます。実行のタイミングは未定義です。このメソッドは、バックグラウンド計算がまだ実行されている間に、ユーザー インターフェイスに任意の形式の進行状況を表示するために使用されます。たとえば、進行状況バーをアニメーション化したり、テキスト フィールドにログを表示したりするために使用できます。

  4. onPostExecute(Result) は、バックグラウンド計算が終了した後に UI スレッドで呼び出されます。バックグラウンド計算の結果は、パラメーターとしてこのステップに渡されます。

タスクがキャンセルされたときに処理したい場合は、 onCancelled() を追加することもできます

于 2012-10-23T04:33:27.860 に答える
0

これを試してみてください:私はこれを試しましたが、これはエラーなしで動作します

/**
 * Async class to get News
 * 
 */
protected class AsynchTask extends AsyncTask<Void, Integer, Integer> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Integer doInBackground(Void... args) {

        download();

        return 1;
    }

    private void download() {
        // We are just imitating some process thats takes a bit of time
        // (loading of resources / downloading)
        int count = 10;
        for (int i = 0; i < count; i++) {

            // Update the progress bar after every step
            int progress = (int) ((i / (float) count) * 100);
            publishProgress(progress);

            // Do some long loading things
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ignore) {
            }
        }

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar1.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Integer a) {
        progressBar1.setVisibility(View.GONE);
    }
}
于 2012-10-23T04:46:26.433 に答える