本文介绍了没有相应的工作进度的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android和Java。我一直对我的任务即,图像下载。在那里我与进度栏下载图像,并在网格中显示它们。我创建了两个类
1. URLImageAdapter
2. CacheActivity。
一切工作正常,但之前,我点击下载按钮,当我在下载按钮会显示进度条和进度单击直到百消失,但仍然在下载是下载开始。我希望同步下载图像和进度条。
code以下。任何帮助
公共类URLImageAdapter延伸BaseAdapter {私有类图片{
字符串URL;
位图大拇指;
}私人图片[]图像;
私人语境myContext;
私人LoadThumbsTask thumbnailGen;
私有对象previousList;公共URLImageAdapter(上下文C){
myContext = C;
thumbnailGen =新LoadThumbsTask(); 如果(previousList!= NULL){
照片=(图片[])previousList;
thumbnailGen.execute(图像);
返回;
} 照片=新的图像[imageURLs.length] 的for(int i = 0,J = imageURLs.length; I<焦耳;我++){
图像[i] =新的图像();
图像[I]的.url = imageURLs [I]
} thumbnailGen.execute(图像);}公众诠释的getCount(){
返回images.length;
}公共对象的getItem(INT位置){
返回图像[位置]的.url;
}众长getItemId(INT位置){
返回的位置;
}公共对象的getData(){
如果(thumbnailGen!= NULL
&功放;&安培; thumbnailGen.getStatus()!= AsyncTask.Status.FINISHED){
thumbnailGen.cancel(真);
} 返回图像;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ImageView的imgView; 图像缓存=图像[位置] 如果(convertView == NULL){ imgView =新ImageView的(myContext);
imgView.setLayoutParams(新GridView.LayoutParams(100,100)); }其他{ imgView =(ImageView的)convertView; } 如果(cached.thumb == NULL){ imgView.setImageResource(R.drawable.ic_action_search);
imgView.setScaleType(ScaleType.CENTER); }其他{ imgView.setScaleType(ScaleType.FIT_CENTER);
imgView.setImageBitmap(cached.thumb); } 返回imgView;
}私人无效cacheUpdated(){
this.notifyDataSetChanged();
}
私人位图loadThumb(字符串URL){ 位图拇指= NULL; BitmapFactory.Options选择采用=新BitmapFactory.Options();
opts.inSampleSize = 4; 尝试{ URL U =新的URL(网址);
URLConnection的C = u.openConnection();
c.connect(); 的BufferedInputStream流=新的BufferedInputStream(
c.getInputStream()); 拇指= BitmapFactory.de codeStream(流空,选择采用);
stream.close(); }赶上(MalformedURLException的E){
Log.e(ERROR,恶意网址:+网址);
}赶上(IOException异常五){
Log.e(ERROR,发生了错误下载的形象:
+网址);
} 返回大拇指;
}私有类LoadThumbsTask扩展的AsyncTask<图像,太虚,太虚> { @覆盖
保护无效doInBackground(图像...缓存){ BitmapFactory.Options选择采用=新BitmapFactory.Options();
opts.inSampleSize = 4; 对于(图像I:缓存){ 如果(isCancelled())
返回null; 如果(i.thumb!= NULL)
继续; SystemClock.sleep(500); i.thumb = loadThumb(i.url); publishProgress();
} 返回null; } @覆盖
保护无效onProgressUpdate(无效...参数){
cacheUpdated();
}
}私有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;
}
}
解决方案
download image in using
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();
}
}
create object for these class in button click
LoginProgress task = new LoginProgress();
task.execute();
call ur doinbackground method in these doinbackground methode
otherwise do following two
increment time of Thread.sleep(1000); change to Thread.sleep(4000); Thread.sleep(200); change to Thread.sleep(450);
you set gridview.setAdapter(new URLImageAdapter(CacheActivity.this)); in oncreate , remove this adapter in oncreate and write it in end of on click method...
这篇关于没有相应的工作进度的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!