3

我是 Android 和 Java 的新手。我一直在处理我的任务,即图像下载器。我必须在哪里下载带有进度条的图像并在网格中显示它们。我创建了两个类 1. URLImageAdapter 2. CacheActivity。一切正常,但是在我点击下载按钮之前开始下载,当我点击下载按钮时,进度条出现并且进度条消失,但仍然在下载。我想同步下载图像和进度条。

代码如下。任何帮助

    public class URLImageAdapter extends BaseAdapter {

private class Image {
    String url;
    Bitmap thumb;
}

private Image[] images;
private Context myContext;
private LoadThumbsTask thumbnailGen;
private Object previousList;

public URLImageAdapter(Context c) {
    myContext = c;
    thumbnailGen = new LoadThumbsTask();

    if (previousList != null) {
        images = (Image[]) previousList;
        thumbnailGen.execute(images);
        return;
    }

    images = new Image[imageURLs.length];

    for (int i = 0, j = imageURLs.length; i < j; i++) {
        images[i] = new Image();
        images[i].url = imageURLs[i];
    }

    thumbnailGen.execute(images);

}

public int getCount() {
    return images.length;
}

public Object getItem(int position) {
    return images[position].url;
}

public long getItemId(int position) {
    return position;
}

public Object getData() {
    if (thumbnailGen != null
            && thumbnailGen.getStatus() != AsyncTask.Status.FINISHED) {
        thumbnailGen.cancel(true);
    }

    return images;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imgView;

    Image cached = images[position];

    if (convertView == null) {

        imgView = new ImageView(myContext);
        imgView.setLayoutParams(new GridView.LayoutParams(100, 100));

    } else {

        imgView = (ImageView) convertView;

    }

    if (cached.thumb == null) {

        imgView.setImageResource(R.drawable.ic_action_search);
        imgView.setScaleType(ScaleType.CENTER);

    } else {

        imgView.setScaleType(ScaleType.FIT_CENTER);
        imgView.setImageBitmap(cached.thumb);

    }

    return imgView;
}

private void cacheUpdated() {
    this.notifyDataSetChanged();
}


private Bitmap loadThumb(String url) {

    Bitmap thumb = null;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 4;

    try {

        URL u = new URL(url);
        URLConnection c = u.openConnection();
        c.connect();

        BufferedInputStream stream = new BufferedInputStream(
                c.getInputStream());

        thumb = BitmapFactory.decodeStream(stream, null, opts);


        stream.close();

    } catch (MalformedURLException e) {
        Log.e("ERROR", "malformed url: " + url);
    } catch (IOException e) {
        Log.e("ERROR", "An error has occurred downloading the image: "
                + url);
    }

    return thumb;
}

private class LoadThumbsTask extends AsyncTask<Image, Void, Void> {

    @Override
    protected Void doInBackground(Image... cache) {

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 4;

        for (Image i : cache) {

            if (isCancelled())
                return null;

            if (i.thumb != null)
                continue;

            SystemClock.sleep(500);

            i.thumb = loadThumb(i.url);

            publishProgress();
        }

        return null;

    }

    @Override
    protected void onProgressUpdate(Void... param) {
        cacheUpdated();
    }
}

private String[] imageURLs = {
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2851.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2944.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2989.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3005.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3012.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3034.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3047.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3092.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3110.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3113.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3128.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3160.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3226.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3228.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3251.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3268.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3275.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3346.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3365.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3374.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3385.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3392.jpg", };
  }

缓存活动

public class CacheActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cache);
    GridView gridview = (GridView) findViewById(R.id.grid_view);
    gridview.setAdapter(new URLImageAdapter(this));
    addListenerOnButton();

    }



public void addListenerOnButton() {

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
    btnStartProgress.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("File downloading ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            progressBarStatus = 0;

            fileSize = 0;

            new Thread(new Runnable() {
                public void run() {
                    while (progressBarStatus < 100) {

                        progressBarStatus = doInBackground();

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    if (progressBarStatus >= 100) {

                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBar.dismiss();
                    }
                }
            }).start();

        }

    });

}

public int doInBackground() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;
        } else if (fileSize == 200000) {
            return 20;
        } else if (fileSize == 300000) {
            return 30;
        }else if (fileSize == 400000) {
            return 40;
        }else if (fileSize == 500000) {
            return 50;
        }else if (fileSize == 600000) {
            return 60;
        }else if (fileSize == 700000) {
            return 70;
        }else if (fileSize == 800000) {
            return 80;
        }else if (fileSize == 900000) {
            return 90;
        }

    }

    return 100;

}

}
4

4 回答 4

1

在使用中下载图像

public class LoginProgress extends AsyncTask<Object, Object, Object> {
       private ProgressDialog dialog;
          @Override
            protected void onPreExecute() {
            this.dialog = ProgressDialog.show(applicationContext, "Please wait down",
                "Loading .....", true);
                    }
                @Override
                protected Object doInBackground(Object... params) {
                    // TODO Auto-generated method stub
                    return null;
                }
                @Override
                protected void onPostExecute(Void unused) {
        //Intent for next activity
         this.dialog.dismiss();
        }

            }

在按钮单击中为这些类创建对象

  LoginProgress task = new LoginProgress();
    task.execute();

在这些 doinbackground 方法中调用你的 doinbackground 方法

否则做以下两个

Thread.sleep(1000) 的增量时间;更改为 Thread.sleep(4000); 线程.sleep(200); 更改为 Thread.sleep(450);

你设置 gridview.setAdapter(new URLImageAdapter(CacheActivity.this)); 在 oncreate 中,在 oncreate 中删除此适配器并将其写入 on click 方法的末尾...

于 2012-11-16T12:17:15.190 回答
0
public int doInBackground() {

while (fileSize <= 1000000) {

    fileSize++;

    if (fileSize == 100000) {
        return 10;
    } else if (fileSize == 200000) {
        return 20;
    } else if (fileSize == 300000) {
        return 30;
    }else if (fileSize == 400000) {
        return 40;
    }else if (fileSize == 500000) {
        return 50;
    }else if (fileSize == 600000) {
        return 60;
    }else if (fileSize == 700000) {
        return 70;
    }else if (fileSize == 800000) {
        return 80;
    }else if (fileSize == 900000) {
        return 90;
    }

}

return 100;

}

它与实际文件大小无关。它应该是这样的

return (currentFilesize/totalFilesize)*100 

而不仅仅是像这样的原始值。

于 2012-11-16T11:52:02.817 回答
0

问题是您没有将任何值传递给publishProgress()您将LoadThumbsTask 某些值传递给此方法(例如,已下载的拇指数),然后根据此值onProgressUpdate()更新您的值。ProgressDialog

于 2012-11-16T11:47:13.420 回答
0

根据我的经验,进度条在列表视图或网格视图中时会出现刷新问题,如果您只想指示进度,我建议您开发自己的控件,该控件看起来像带有 RelativeLayout 的进度条。

采用 relativelayout 并在其中添加 imageview。并增加图像视图的宽度以说明类似的进度。

如果您愿意,我可以发布代码,我已经为自己开发了一个这样的进展

更新:我找到了同样的好帖子,以下帖子指出了我描述的相同技术。

http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/

演示可下载:http ://blog.mediarain.com/wp-content/uploads/2011/04/RoundedProgressSample.zip

于 2012-11-16T11:41:36.617 回答