public class MainActivity extends ActionBarActivity {
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AsyncTaskActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, GetImageAsyncTaskActivity.class);
startActivity(intent);
}
});
}
}
public class GetImageAsyncTaskActivity extends Activity {
private ImageView imageView;
private ProgressBar progressBar;
private MyAsyncTask myAsyncTask;
private static String URL = "http://www.logoing.net/wp-content/uploads/2012/08/baidulogo1.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getimage);
imageView = (ImageView) findViewById(R.id.imageView);
progressBar = (ProgressBar) findViewById(R.id.progressBar2);
myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(URL);
}
class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {//doInBackgroud方法前执行
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);//设置滚动条可见
}
@Override
protected void onPostExecute(Bitmap bitmap) {//doInBackgroud方法后执行
super.onPostExecute(bitmap);
progressBar.setVisibility(View.GONE);//设置滚动条不可见
imageView.setImageBitmap(bitmap);//任务执行完,设置图片
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
URLConnection connection;
InputStream inputStream;
try {
Thread.sleep(3000);//模拟耗时操作
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
connection = new URL(params[0]).openConnection();
inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
bitmap = BitmapFactory.decodeStream(bufferedInputStream);
inputStream.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
}
public class AsyncTaskActivity extends Activity {
private ProgressBar progressBar;
private MyAsyncTask myAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
myAsyncTask=new MyAsyncTask();
myAsyncTask.execute();
}
@Override
protected void onPause() {
super.onPause();
if (myAsyncTask != null && myAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
myAsyncTask.cancel(true);//设置取消,但不会终止线程
}
}
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < 100; i++) {
if (myAsyncTask.isCancelled()) {
break;//如果取消状态,跳出循环
}
onProgressUpdate(i);
try {
Thread.sleep(300);//模拟耗时操作
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (myAsyncTask.isCancelled()) {
return;//如果取消状态,返回
}
progressBar.setProgress(values[0]);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click to progressbar" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click to getImage" />
</LinearLayout>
getimage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
参考:慕课网