RatingBar と ProgressDialog

RatingBar ProgressDialog で進捗に合わせる場合のポイント、

(1)Context から、LayoutInflater で、レイアウトのView を生成し、
WindowManager.LayoutParams を用意して、ProgressDialog onCreate をオーバライドして setContentView を実行すること。

(2)RatingBar は、(1)で生成するView から、findViewById で取得しておき、
進捗に合わせて、setRating を実行する。

AsyncTask として書くサンプルは、以下のとおり。。。

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RatingBar;
import android.widget.TextView;
/**
 * Customise3ProgressTask
 */

public abstract class Customise3ProgressTask extends AsyncTask<String,Integer,Long>{
   private Context context;
   private boolean cancelFlag;
   private ProgressDialog dialog;
   RatingBar ratingbar;

   protected abstract void onResulted(Long result);

   public Customise3ProgressTask(Context context){
      cancelFlag = false;
      this.context = context;
   }
   @Override
   protected void onPreExecute(){
      showDialog();
   }
   @Override
   protected Long doInBackground(String...params){
      int count=1;
      try{
      for(;count < 10;count++){
         if (isCancelled()){
            Logger.d("Cancelled!");
            break;
         }
         Thread.sleep(600);
         if (cancelFlag) break;
         // 進捗 Update
         publishProgress(count * 10);
      }
      }catch(InterruptedException e){
      }
      return (long)count;
   }
   @Override
   protected void onProgressUpdate(Integer...values){
      int n = values[0] / 10;
      ratingbar.setRating(n);
   }
   @Override
   protected void onPostExecute(Long result){
      onResulted(result);
      dismissDialog();
   }
   @Override
   protected void onCancelled(){
      cancelFlag = true;
      dismissDialog();
      new AlertDialog.Builder(context)
      .setMessage("キャンセルされました")
      .setPositiveButton("OK",new DialogInterface.OnClickListener(){
         @Override
         public void onClick(DialogInterface _dialog,int which){
         }
      }).create().show();
   }

   private void showDialog(){
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      final View view = inflater.inflate(R.layout.custom3progress,null);
      final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
      layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
      layoutParams.format = PixelFormat.TRANSLUCENT;
      *1.setText("Loading ...");


      ratingbar = (RatingBar)view.findViewById(R.id.ratingBar1);
      ratingbar.setNumStars(5);
      ratingbar.setRating(0);
      dialog = new ProgressDialog(context){
         @Override
         protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(view,layoutParams);
         }
      };

      dialog.setIndeterminate(false);
      dialog.setCancelable(true);
      dialog.setMax(100);
      dialog.setProgress(0);
      dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
         @Override
         public void onCancel(DialogInterface _dialog){
            cancel(true);
         }
      });
      dialog.show();
   }
   protected void dismissDialog() {
      if (dialog != null){
         dialog.dismiss();
         dialog = null;
      }
   }
}

*1:TextView)view.findViewById(R.id.progressTextView