Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从web加载数据时如何显示Progressbar?_Android_Android Widget - Fatal编程技术网

Android 从web加载数据时如何显示Progressbar?

Android 从web加载数据时如何显示Progressbar?,android,android-widget,Android,Android Widget,我正在我的android应用程序中实现progressbar。在我的应用程序中,当从web加载图像时,我想显示progressbar。加载所有图像后,加载程序应该停止 我看到了progressbar的一些示例,它显示预定义的时间段。但是我希望progressbar的显示动态地取决于从web加载图像的时间。通常,您会在一个浏览器中进行下载。覆盖onPreExecute()将允许您在后台线程启动和下载开始之前显示progressbar。在AsyncTask的onPostExecute方法中,您可以隐

我正在我的android应用程序中实现progressbar。在我的应用程序中,当从web加载图像时,我想显示progressbar。加载所有图像后,加载程序应该停止


我看到了progressbar的一些示例,它显示预定义的时间段。但是我希望progressbar的显示动态地取决于从web加载图像的时间。

通常,您会在一个浏览器中进行下载。覆盖onPreExecute()将允许您在后台线程启动和下载开始之前显示progressbar。在AsyncTask的onPostExecute方法中,您可以隐藏progressbar。

您应该使用。它的Javadoc有一个示例,说明了您想要做什么:

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");
     }
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i

您只需实现
setProgressPercent
方法来设置进度条中的进度,并将其隐藏在
onPostExecute

上,您还可以拥有每个图像的加载视图,然后用户仍然可以在图像加载时滚动,而不仅仅是在加载所有图像并删除对话框之后

将此类添加到项目中:

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

}
要加载图像,您可以使用:(将包名称更改为您的包)

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