Android多線程,讓耗時(shí)的操作去后臺(tái)運(yùn)行吧
在android程序中,會(huì)有一些耗時(shí)的操作,比如從網(wǎng)上抓取圖片,下載文件,批量更新數(shù)據(jù)庫等,這些操作對(duì)于手機(jī)而言會(huì)需要很長(zhǎng)的時(shí)間,而應(yīng)用程序界面又不能等到這些操作完成后再顯示,所以要讓界面各這些耗時(shí)的操作并行處理,用多線程可以解決這個(gè)問題。當(dāng)然還有其它解決方案,比如用Service.
我們先作一個(gè)例子吧,大概是這樣的:有一個(gè)列表,每行顯示的一個(gè)圖片,圖片是存放在網(wǎng)上的。如果不用多線程,也是可以的,但是要等到所有圖片下載完了才能展示出來。這種方式對(duì)用戶體驗(yàn)很不友好,所以我們采用多線程的方式,對(duì)每一個(gè)圖片開啟一個(gè)線程,當(dāng)其下載完數(shù)據(jù)后,在主線程中顯示出來。
主Activity
- public class TestListActivity extends ListActivity {
- private ImageListAdapter imageListAdapter = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.imagelist);
- String[] images = {"http://image.baidu.com/image1.jpg","http://image.baidu.com/image2.jpg"};
- imageListAdapter = new ImageListAdapter(getApplicationContext(), images);
- setListAdapter(imageListAdapter);
- }
- }
適配器
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ImageListAdapter extends BaseAdapter {
- private Context context;
- private String[] myImages = null;
- public ImageListAdapter(Context context, String[] myImages){
- this.context = context;
- this.myImages = myImages;
- }
- @Override
- public int getCount() {
- if(myImages == null){
- return 0;
- }
- return myImages.length;
- }
- @Override
- public String getItem(int position) {
- if(position < 0 || myImages == null || position>myImages.length){
- return null;
- }
- return myImages[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View item = null;
- if(convertView != null){
- item = convertView;
- } else {
- item = View.inflate(context, R.layout.image_item, null);
- }
- final ImageView imageView = (ImageView)item.findViewById(R.id.image);
- final String image = getItem(position);
- if(image == null){
- return item;
- }
- //對(duì)每個(gè)圖片地址創(chuàng)建一個(gè)線程,
- new Thread(){
- public void run(){
- Message msg = new Message();
- msg.what = 0;
- //獲得圖片的Bitmap對(duì)象。getBitmap省略了,就是從網(wǎng)上通過http下載圖片然后轉(zhuǎn)化成一個(gè)Bitmap
- Bitmap bitmap = getBitmap(image);
- List list = new ArrayList();//將bitmap和imageView包裝成一個(gè)List傳到線程外
- list.add(bitmap);
- list.add(imageView);
- msg.obj = list;
- handler.sendMessage(msg);
- }
- }.start();
- return item;
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 0://接到從線程內(nèi)傳來的圖片bitmap和imageView.
- //這里只是將bitmap傳到imageView中就行了。只所以不在線程中做是考慮到線程的安全性。
- List list = (List)msg.obj;
- Bitmap bitmap = (Bitmap)list.get(0);
- ImageView iv = (ImageView)list.get(1);
- iv.setImageBitmap(bitmap);
- break;
- default:
- super.handleMessage(msg);
- }
- }
- };
- }
布局xml
imagelist.xml
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding = "10px"
- android:gravity="center_horizontal"
- android:background="#ffffff">
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- image_item.xml
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- android:id="@+id/image"
- android:layout_width="70px"
- android:layout_height="50px"
- android:paddingRight="5px"/>
轉(zhuǎn)載請(qǐng)標(biāo)明出處:3G Study :http://blog.3gstdy.com/archives/27