2010-12-15 5 views
1

내 안드로이드 앱에 progressbar를 구현하고 있습니다. 내 애플 리케이션에서 로더를 중지해야하는 모든 이미지를로드 한 후 이미지가 웹에서로드 될 때 진행률 표시 줄을 표시하려고합니다.Progressbar를 표시하는 방법 데이터가 웹에서로드되는 경우?

미리 정의 된 시간 동안 표시한다는 점에서 progressbar의 몇 가지 예를 보았습니다. 하지만 웹에서 이미지를로드 할 시간에 따라 진행 막대 표시가 동적으로 달라지기를 바랍니다.

답변

0

일반적으로 다운로드는 ASyncTask입니다. onPreExecute()를 덮어 쓰면 백그라운드 스레드가 시작되고 다운로드가 시작되기 바로 전에 진행 표시 줄을 표시 할 수 있습니다. AsyncTask의 onPostExecute 메소드에서 진행 표시 줄을 숨길 수 있습니다.

2

AsyncTask을 사용하십시오. 당신은 당신의 진행 표시 줄에서 진행 상태를 설정하는 setProgressPercent 메소드를 구현하고 onPostExecute에 숨길 필요가

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 

: 그것을위한 Javadoc은 당신이 원하는대로로의 예를 가지고있다.

0

또한 각 이미지에 대한 로딩보기가있을 수 있으며 사용자는 모든 이미지가로드되고 대화 상자가 제거 된 후 이미지가 로딩되는 동안 계속 스크롤 할 수 있습니다.

import java.io.IOException; 
import java.net.MalformedURLException; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Handler.Callback; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ProgressBar; 

/** 
* Free for anyone to use, just say thanks and share :-) 
* @author Blundell 
* 
*/ 
public class LoaderImageView extends LinearLayout{ 

    private static final int COMPLETE = 0; 
    private static final int FAILED = 1; 

    private Context mContext; 
    private Drawable mDrawable; 
    private ProgressBar mSpinner; 
    private ImageView mImage; 

    /** 
    * This is used when creating the view in XML 
    * To have an image load in XML use the tag 'image="http://developer.android.com/images/dialog_buttons.png"' 
    * Replacing the url with your desired image 
    * Once you have instantiated the XML view you can call 
    * setImageDrawable(url) to change the image 
    * @param context 
    * @param attrSet 
    */ 
    public LoaderImageView(final Context context, final AttributeSet attrSet) { 
      super(context, attrSet); 
      final String url = attrSet.getAttributeValue(null, "image"); 
      if(url != null){ 
        instantiate(context, url); 
      } else { 
        instantiate(context, null); 
      } 
    } 

    /** 
    * This is used when creating the view programatically 
    * Once you have instantiated the view you can call 
    * setImageDrawable(url) to change the image 
    * @param context the Activity context 
    * @param imageUrl the Image URL you wish to load 
    */ 
    public LoaderImageView(final Context context, final String imageUrl) { 
      super(context); 
      instantiate(context, imageUrl);   
    } 

    /** 
    * First time loading of the LoaderImageView 
    * Sets up the LayoutParams of the view, you can change these to 
    * get the required effects you want 
    */ 
    private void instantiate(final Context context, final String imageUrl) { 
      mContext = context; 

      mImage = new ImageView(mContext); 
      mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

      mSpinner = new ProgressBar(mContext); 
      mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

      mSpinner.setIndeterminate(true); 

      addView(mSpinner); 
      addView(mImage); 

      if(imageUrl != null){ 
        setImageDrawable(imageUrl); 
      } 
    } 

    /** 
    * Set's the view's drawable, this uses the internet to retrieve the image 
    * don't forget to add the correct permissions to your manifest 
    * @param imageUrl the url of the image you wish to load 
    */ 
    public void setImageDrawable(final String imageUrl) { 
      mDrawable = null; 
      mSpinner.setVisibility(View.VISIBLE); 
      mImage.setVisibility(View.GONE); 
      new Thread(){ 
        public void run() { 
          try { 
            mDrawable = getDrawableFromUrl(imageUrl); 
            imageLoadedHandler.sendEmptyMessage(COMPLETE); 
          } catch (MalformedURLException e) { 
            imageLoadedHandler.sendEmptyMessage(FAILED); 
          } catch (IOException e) { 
            imageLoadedHandler.sendEmptyMessage(FAILED); 
          } 
        }; 
      }.start(); 
    } 

    /** 
    * Callback that is received once the image has been downloaded 
    */ 
    private final Handler imageLoadedHandler = new Handler(new Callback() { 
      @Override 
      public boolean handleMessage(Message msg) { 
        switch (msg.what) { 
        case COMPLETE: 
          mImage.setImageDrawable(mDrawable); 
          mImage.setVisibility(View.VISIBLE); 
          mSpinner.setVisibility(View.GONE); 
          break; 
        case FAILED: 
        default: 
          // Could change image here to a 'failed' image 
          // otherwise will just keep on spinning 
          break; 
        } 
        return true; 
      }    
    }); 

    /** 
    * Pass in an image url to get a drawable object 
    * @return a drawable object 
    * @throws IOException 
    * @throws MalformedURLException 
    */ 
    private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException { 
      return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), "name"); 
    } 

} 

는 다음 사용할 수있는 이미지로드하도록하려면 :

http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html

프로젝트에이 클래스를 추가 (패키지에 패키지 이름을 변경)을

<com.blundell.tut.LoaderImageView 
android:id="@+id/loaderImageView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
image="http://developer.android.com/images/dialog_buttons.png" 
/> 

또는 코드에서 :

final LoaderImageView image = new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png"); 
    image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
관련 문제