public class MainActivity extends Activity {
private ImageView image1,image2,image3;
//进度条
private ProgressDialog progressDialog;
//图片uri
private static final String[] images =new String[]{
"https://img2.baidu.com/it/u=4172024400,264653010&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500",
"https://img1.baidu.com/it/u=662869083,853426838&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800",
"https://img0.baidu.com/it/u=2051155272,478750855&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
};
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
imageViewList = new ArrayList<>();
imageViewList.add(image1);
imageViewList.add(image2);
imageViewList.add(image3);
}
/**
* 1.异步子线程,处理主线程不愿意处理的业务
* 2.得到处理结果
* 3.可以控制自己线程的停止或启动
* 4.通知主线程更新UI
* AsyncTask
* Params 传入的业务参数
* Progress 异步任务执行的进度 10% 、20%
* Result 返回主线程的结果 List
* 必须在主线程中创建,4个方法
onPreExecute(), 执行之前 主线程
doInBackground(Params...),任务执行中-业务核心方法 子线程
onProgressUpdate(Progress...) 任务执行之中---进度更新方法 主线程
onPostExecute(Result), 任务执行结束 主线程
* */
class AsyncTaskHelper extends AsyncTask
@Override
protected List
//子线程
Thread thread = Thread.currentThread();
System.out.println(thread.getId()+"-----"+thread.getName()+"--onPostExecute-------------任务执行中-业务核心方法----------------");
List
for (int i = 0; i < params.length; i++) {
try {
URL url =new URL(images[i]);
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
bitmaps.add(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
//更新进度方法
publishProgress(i);
}
return bitmaps;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//主线程
Thread thread = Thread.currentThread();
System.out.println(thread.getId()+"-----"+thread.getName()+"--onProgressUpdate-------------任务执行之中---进度更新方法--------------"+ Arrays.toString(values));
if(values[0] == 0){
progressDialog.setProgress(30);
}else if(values[0] == 1){
progressDialog.setProgress(80);
}else{
Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//主线程
Thread thread = Thread.currentThread();
System.out.println(thread.getId()+"-----"+thread.getName()+"--onPreExecute-------------任务执行之前--------------");
}
@Override
protected void onPostExecute(List
super.onPostExecute(bitmaps);
//主线程
Thread thread = Thread.currentThread();
System.out.println(thread.getId()+"-----"+thread.getName()+"--onPostExecute-------------任务执行结束-----------------");
for (int i = 0; i < bitmaps.size(); i++) {
imageViewList.get(i).setImageBitmap(bitmaps.get(i));
}
}
}
//
public void getImage(View view){
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在加载");
progressDialog.setMax(100);
/** 进度弹出框是水平的 */
progressDialog.setIndeterminate(false); // 进度可见
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.show();
AsyncTaskHelper asyncTaskHelper =new AsyncTaskHelper();
asyncTaskHelper.execute(images);
}
}