0

我有一个正从网上加载图像的imageview。我从网上加载图像,并使用异步任务将其转换为位图图像。我想在图像视图中显示静态图像,并在图像加载时在图像视图的顶部显示进度微调器。问题是我有我的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(); 

    } 
} 

回答

3

您可以将ImageView和ProgessBar包装到相对布局中。因此,当图像加载时,将progressbar的可见性设置为VISIBLE,ImageView将其设置为GONE。此刻,您已准备好位图,反转Visibilities。

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

} 
+0

我不会轻易地理解如何将它合并到我的代码中。我已经添加了我的异步任务类代码。我如何将这个并入我的异步任务类?而且我没有在我的程序中使用任何设置的属性。 – AndroidDev93 2012-07-24 16:22:31

+1

使用'LoaderImageView'而不是'ImageView'。如果你不想使用'AttributeSet',就不要使用它。添加另一个只需要一个Context的构造函数。参见[Compound Controls] [1] [1]:http://developer.android.com/guide/topics/ui/custom-components.html#compound http://developer.android.com/guide/topics/ui /custom-components.html#compound – 2012-07-24 17:05:02