私はAndroidとJavaを初めて使用します。私は自分のタスク、つまりImageDownloaderに取り組んできました。プログレスバー付きの画像をダウンロードしてグリッドに表示する必要がある場合。2つのクラスを作成しました1.URLImageAdapter2.CacheActivity。すべて正常に動作しますが、ダウンロードボタンをクリックする前にダウンロードが開始され、ダウンロードボタンをクリックすると、進行状況バーが表示され、100が消えるまで進行しますが、ダウンロードはオンのままです。画像のダウンロードとプログレスバーを同期させたい。
以下のコード。ヘルプ
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", };
}
CacheActivity
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;
}
}