温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android多线程实例分析

发布时间:2022-03-29 16:22:47 来源:亿速云 阅读:126 作者:iii 栏目:移动开发

本文小编为大家详细介绍“Android多线程实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android多线程实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

在android程序中,会有一些耗时的操作,比如从网上抓取图片,下载文件,批量更新数据库等,这些操作对于手机而言会需要很长的时间,而应用程序界面又不能等到这些操作完成后再显示,所以要让界面各这些耗时的操作并行处理,用多线程可以解决这个问题。当然还有其它解决方案,比如用Service.

我们先作一个例子吧,大概是这样的:有一个列表,每行显示的一个图片,图片是存放在网上的。如果不用多线程,也是可以的,但是要等到所有图片下载完了才能展示出来。这种方式对用户体验很不友好,所以我们采用多线程的方式,对每一个图片开启一个线程,当其下载完数据后,在主线程中显示出来。

主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 = {"https://cache.yisu.com/upload/information/20210522/379/538936.jpg","https://cache.yisu.com/upload/information/20210522/379/538938.jpg"}; imageListAdapter = new ImageListAdapter(getApplicationContext(), images); setListAdapter(imageListAdapter); } }

适配器

  1. import java.util.ArrayList; 

  2. import java.util.List; 

  3. import android.content.Context; 

  4. import android.graphics.Bitmap; 

  5. import android.os.Handler; 

  6. import android.os.Message; 

  7. import android.view.View; 

  8. import android.view.ViewGroup; 

  9. import android.widget.BaseAdapter; 

  10. import android.widget.ImageView; 

  11. import android.widget.TextView; 

  12.  

  13. public class ImageListAdapter extends BaseAdapter { 

  14. private Context context; 

  15. private String[] myImages = null; 

  16. public ImageListAdapter(Context context, String[] myImages){ 

  17. this.context = context; 

  18. this.myImages = myImages; 

  19. @Override 

  20. public int getCount() { 

  21. if(myImages == null){ 

  22. return 0; 

  23. return myImages.length; 

  24. @Override 

  25. public String getItem(int position) { 

  26. if(position < 0 || myImages == null || position>myImages.length){ 

  27. return null; 

  28. return myImages[position]; 

  29. @Override 

  30. public long getItemId(int position) { 

  31. return position; 

  32. @Override 

  33. public View getView(int position, View convertView, ViewGroup parent) { 

  34. View item = null; 

  35. if(convertView != null){ 

  36. item = convertView; 

  37. } else { 

  38. item = View.inflate(context, R.layout.image_item, null); 

  39. final ImageView imageView = (ImageView)item.findViewById(R.id.image); 

  40. final String image = getItem(position); 

  41. if(image == null){ 

  42. return item; 

  43. //对每个图片地址创建一个线程, 

  44. new Thread(){ 

  45. public void run(){ 

  46. Message msg = new Message(); 

  47. msg.what = 0; 

  48. //获得图片的Bitmap对象。getBitmap省略了,就是从网上通过http下载图片然后转化成一个Bitmap 

  49. Bitmap bitmap = getBitmap(image); 

  50. List list = new ArrayList();//将bitmap和imageView包装成一个List传到线程外 

  51. list.add(bitmap); 

  52. list.add(imageView); 

  53. msg.obj = list; 

  54. handler.sendMessage(msg); 

  55. }.start(); 

  56. return item; 

  57. private Handler handler = new Handler() { 

  58. @Override 

  59. public void handleMessage(Message msg) { 

  60. switch (msg.what) { 

  61. case 0://接到从线程内传来的图片bitmap和imageView. 

  62. //这里只是将bitmap传到imageView中就行了。只所以不在线程中做是考虑到线程的安全性。 

  63. List list = (List)msg.obj; 

  64. Bitmap bitmap = (Bitmap)list.get(0); 

  65. ImageView iv = (ImageView)list.get(1); 

  66. iv.setImageBitmap(bitmap); 

  67. break; 

  68. default: 

  69. super.handleMessage(msg); 

  70. }; 

  71. }

布局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"/>

读到这里,这篇“Android多线程实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI