Android进度没有相应地工作
Posted
技术标签:
【中文标题】Android进度没有相应地工作【英文标题】:Android progress not working accordingly 【发布时间】:2012-11-05 03:10:08 【问题描述】:我是 android 和 Java 新手。我一直在做我的任务,即图像下载器。我必须在哪里下载带有进度条的图像并在网格中显示它们。我创建了两个类 1. URLImageAdapter 2. 缓存活动。 一切正常,但在我点击下载按钮之前开始下载,当我点击下载按钮时,进度条出现并且进度条消失,但仍在下载。我要同步下载图片和进度条。
代码如下。任何帮助
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;
【问题讨论】:
问题:为什么需要休眠异步任务? @Neilers SystemClock.sleep(500); // 人为地造成延迟! 【参考方案1】:根据我的经验,进度条在列表视图或网格视图中时会出现刷新问题,如果您只想指示进度,我建议您开发自己的控件,该控件看起来像带有RelativeLayout的进度条。
采用 relativelayout 并在其中添加 imageview。并增加 imageview 的宽度以说明类似的进度。
如果你愿意,我可以发布代码,我已经为自己开发了一个这样的进展
更新:我找到了同样的好帖子,以下帖子表明我描述的技术相同。
http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/
可下载演示:http://blog.mediarain.com/wp-content/uploads/2011/04/RoundedProgressSample.zip
【讨论】:
请问你能做到吗?会很棒 KP 链接很有用,但不允许在我的给定任务中使用这些链接。【参考方案2】:在使用中下载图片
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 方法的末尾...
【讨论】:
你将获得带有微调器和文本的 ProgressDialog 是的,我知道。我被要求获得一个水平进度条。我的问题是如何同步下载和进度条。请帮助赛。如果您需要,我可以将我的整个代码邮寄给您,您可以检查一下。 关注这个网址androidhive.info/2012/04/… 已经关注了...!!但是,作为 java 和 android 的新手,我不知道如何进行更改,你能做一些更改吗?帮帮我? 发送给 saiyadamreddy@gmail.com 我会尝试现在做,否则星期一【参考方案3】: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
而不仅仅是像这样的原始值。
【讨论】:
【参考方案4】:问题是您没有将任何值传递给您的publishProgress()
的LoadThumbsTask
将一些值传递给此方法(例如,已下载的拇指数),然后在 onProgressUpdate()
中根据此值更新您的 ProgressDialog
。
【讨论】:
以上是关于Android进度没有相应地工作的主要内容,如果未能解决你的问题,请参考以下文章
如何停止 Android webView swipeToRefresh 加载进度条?