写点什么

AsyncTask 相关知识

用户头像
Android架构
关注
发布于: 1 小时前

@BindView(R.id.progress_bar)


ProgressBar progressBar;


private static String url =


"http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg";


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(saved


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


InstanceState);


setContentView(R.layout.activity_async);


ButterKnife.bind(this);


initData();


}


private void initData() {


new TestAsyncTask().execute(url);


}


class TestAsyncTask extends AsyncTask<String, Void, Bitmap> {


@Override


protected void onPreExecute() {


super.onPreExecute();


progressBar.setVisibility(View.VISIBLE);


}


@Override


protected void onPostExecute(Bitmap bitmap) {


super.onPostExecute(bitmap);


//操作 UI,设置图像


progressBar.setVisibility(View.GONE);


imageView.setImageBitmap(bitmap);


}


@Override


protected Bitmap doInBackground(String... strings) {


//获取传递进来的参数


String url = strings[0];


Bitmap bitmap = null;


URLConnection connection;


InputStream inputStream;


try {


//获取网络连接对象


connection = new URL(url).openConnection();


//获取输入流


inputStream = connection.getInputStream();


BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);


Thread.sleep(2000);


//将输入流解析成 bitmap


bitmap = BitmapFactory.decodeStream(bufferedInputStream);


//关闭输入流


inputStream.close();


bufferedInputStream.close();


} catch (IOException e) {


e.printStackTrace();


} catch (InterruptedException e) {


e.printStackTrace();


}


return bitmap;


}


}


}


布局:


<?xml version="1.0" encoding="utf-8"?>


<LinearLayout


xmlns:android="http://schemas.android.com/apk/res/android"


xmlns:tools="http://schemas.android.com/tools"


android:layout_width="match_parent"


android:layout_height="match_parent"


tools:context="com.gs.sumok2.asynctask.AsyncActivity"


android:orientation="vertical">


<ImageView


android:id="@+id/image_view"


android:layout_width="200dp"


android:layout_height="200dp" />


<ProgressBar


android:id="@+id/progress_bar"


android:layout_gravity="center_horizontal"


android:visibility="gone"


android:layout_width="100dp"


android:layout_height="100dp" />


</LinearLayout>


代码编写实例之模拟水平进度加载条:


=================


Activity:


public class Async2Activity extends AppCompatActivity {


@BindView(R.id.progressBar)


ProgressBar progressBar;


private Test2AsyncTask mTask;


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_async2);


ButterKnife.bind(this);


mTask = new Test2AsyncTask();


mTask.execute();


}


@Override


protected void onPause() {


super.onPause();


//不为空而且处于 Running 状态


if (mTask != null && mTask.getStatus() == AsyncTask.Status.RUNNING) {


//cancel 方法只是将对应的 AsyncTask 标记为 cancel 状态,并不是真正地取消线程的执行


mTask.cancel(true);


}


}


class Test2AsyncTask extends AsyncTask<Void, Integer, Void> {


@Override


protected Void doInBackground(Void... voids) {


//模拟进度更新


for (int i = 0; i < 100; i++) {


if (isCancelled()){


break;


}


publishProgress(i);


try {


Thread.sleep(300);


} catch (InterruptedException e) {


e.printStackTrace();


}


}


return null;


}


@Override


protected void onProgressUpdate(Integer... values) {


super.onProgressUpdate(values);


if (isCancelled()){


return;


}


//获取进度更新值


progressBar.setProgress(values[0]);


}


}


}


布局:


<?xml version="1.0" encoding="utf-8"?>


<LinearLayout


xmlns:android="http://schemas.android.com/apk/res/android"


xmlns:tools="http://schemas.android.com/tools"


android:layout_width="match_parent"


android:layout_height="match_parent"


android:padding="16dp"


tools:context="com.gs.sumok2.asynctask.Async2Activity"


android:orientation="vertical">


<ProgressBar

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
AsyncTask相关知识