在imageview中显示微调器动画

时间:2012-07-24 15:44:55

标签: android android-asynctask android-animation android-progressbar

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

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();

    }   
} 

1 个答案:

答案 0 :(得分:3)

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

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);
   }

}