public class SecondActivity extends Activity {
String path="http://www.baidu.com/img/bd_logo1.png";
private ImageView img;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img = (ImageView)findViewById(R.id.img);
pb = (ProgressBar)findViewById(R.id.pb);
MyAsyncTask ma=new MyAsyncTask();
ma.execute(path);
}
/*
* 创建内部类 来继承AsyncTask
*
*/
class MyAsyncTask extends AsyncTask<String, Void, Bitmap>{
private URLConnection openConnection;
private InputStream inputStream;
private BufferedInputStream bis;
private Bitmap bit;
@Override
protected Bitmap doInBackground(String... params) {
String str=params[0];
try {
URL url=new URL(str);
openConnection = url.openConnection();
inputStream = openConnection.getInputStream();
bis = new BufferedInputStream(inputStream);
Thread.sleep(3000);
bit = BitmapFactory.decodeStream(bis);
inputStream.close();
bis.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bit;
}
/*
* 在耗时操作前执行。通常为初始化的操作
* 在这里 为将之前隐藏的ProgressBar显示出来
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
/*
* 接收doInBackground方法返回来的Bitmap值 可以直接在此方法中更新UI控件
* 同时隐藏之前的ProgressBar
*/
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
pb.setVisibility(View.GONE);
img.setImageBitmap(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
String path="http://www.baidu.com/img/bd_logo1.png";
private ImageView img;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img = (ImageView)findViewById(R.id.img);
pb = (ProgressBar)findViewById(R.id.pb);
MyAsyncTask ma=new MyAsyncTask();
ma.execute(path);
}
/*
* 创建内部类 来继承AsyncTask
*
*/
class MyAsyncTask extends AsyncTask<String, Void, Bitmap>{
private URLConnection openConnection;
private InputStream inputStream;
private BufferedInputStream bis;
private Bitmap bit;
@Override
protected Bitmap doInBackground(String... params) {
String str=params[0];
try {
URL url=new URL(str);
openConnection = url.openConnection();
inputStream = openConnection.getInputStream();
bis = new BufferedInputStream(inputStream);
Thread.sleep(3000);
bit = BitmapFactory.decodeStream(bis);
inputStream.close();
bis.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bit;
}
/*
* 在耗时操作前执行。通常为初始化的操作
* 在这里 为将之前隐藏的ProgressBar显示出来
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
/*
* 接收doInBackground方法返回来的Bitmap值 可以直接在此方法中更新UI控件
* 同时隐藏之前的ProgressBar
*/
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
pb.setVisibility(View.GONE);
img.setImageBitmap(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}