0

我有一个从互联网加载图像的图像视图。我正在从网络加载图像并使用异步任务将其转换为位图图像。我想在我的图像视图中显示一个静态图像,并在图像加载时在图像视图顶部有一个进度微调器。问题是我的 xml 中有进度微调器,我无法在异步任务中使用它,而且我不知道如何使进度微调器在图像视图中也可见。我该怎么做呢?这是我的异步任务类:

public class LoadImage extends AsyncTask<Void, Void, Bitmap>{

    Context callingContext = null;

    ImageView view;
    String b = null;
    ListView lv;
    ProgressDialog p;

    public LoadImage(Context c, ImageView view, String b){

        this.view = view;
        this.b = b;
        this.callingContext = c;
    }


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    //  p = (ProgressDialog) findViewById(R.id.progressBar1);
        p = ProgressDialog.show(callingContext, "Loading Events", "Retrieving data from server, please wait...", true);
    }



    public Bitmap getBit(String data){

        Bitmap bitmap;
        BitmapFactory.Options bmOptions;
        bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        //from web
        try {
            bitmap=null;
            InputStream is=new URL(data).openStream();
            BitmapFactory.decodeStream(is, null, bmOptions);
            is.close();
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = 20;
            is = new URL(data).openStream();
            bitmap = BitmapFactory.decodeStream(is, null, o2);
            is.close();
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }

    }


    @Override
    protected Bitmap doInBackground(String... arg0) {
        // TODO Auto-generated method stub  
        Bitmap b = null;    
        URL imageUrl = null;

        b = getBit(this.b);

            return b;  
    }



    @Override
    protected void onPostExecute(Bitmap result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if(result == null)
            view.setImageResource(R.drawable.ic_launcher);

        p.dismiss();

    }   
} 
4

1 回答 1

4

您可以将 ImageView 和 ProgessBar 包装到相对布局中。因此,在加载图像时,将进度条的可见性设置为 VISIBLE,并将 ImageView 的可见性设置为 GONE。在您准备好位图的那一刻,反转可见性。

public class LoaderImageView extends RelativeLayout {
    private Context     context;
    private ProgressBar progressBar;
    private ImageView   imageView;

    public LoaderImageView(final Context context, final AttributeSet attrSet) {
    super(context, attrSet);
    instantiate(context, attrSet);
    }

   private void instantiate(final Context _context, AttributeSet attrSet) {
       context = _context;
       imageView = new ImageView(context, attrSet);
       progressBar = new ProgressBar(context, attrSet);
       progressBar.setIndeterminate(true);

       addView(progressBar);
       addView(imageView);

       this.setGravity(Gravity.CENTER);
   }

   // ...

   // Then, play with this method to show or hide your progressBar
   public LoaderImageView(final Context context, final AttributeSet attrSet) {
    super(context, attrSet);
    instantiate(context, attrSet);
   }

}
于 2012-07-24T15:48:30.890 回答