首页 > 其他分享 >使用AsyncTask异步加载图片

使用AsyncTask异步加载图片

时间:2022-12-06 14:39:30浏览次数:39  
标签:异步 http douban jpg AsyncTask img3 com public 加载


主要看看AsyncTask如何使用:

MainActivity.java




    1. public class MainActivity extends Activity {  
    2. private static final String TAG = "MainActivity";
    3. private List<String> datas;
    4. private LayoutInflater inflater;
    5. private Context context;
    6. private MyAdapter adapter;
    7. private ListView listview;
    8. private List<String> imgUrls;
    9. private List<TextView> list = new ArrayList<TextView>();
    10. private List<String> sortUrl = new ArrayList<String>();
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. requestWindowFeature(Window.FEATURE_NO_TITLE);
    15. setContentView(R.layout.activity_main);
    16. this);
    17. this;
    18. listview = (ListView) findViewById(R.id.listview);
    19. initData();
    20. new MyAdapter();
    21. listview.setAdapter(adapter);
    22. }
    23.
    24. private void initData() {
    25. new ArrayList<String>();
    26. "http://img5.douban.com/view/movie_poster_cover/mpst/public/p2199507156.jpg");
    27. "http://img5.douban.com/lpic/s27397768.jpg");
    28. "http://img3.douban.com/lpic/s27505695.jpg");
    29. "http://img3.douban.com/lpic/s27461340.jpg");
    30. "http://img3.douban.com/lpic/s27451442.jpg");
    31. "http://img5.douban.com/lpic/s27453059.jpg");
    32. "http://img3.douban.com/lpic/s27452925.jpg");
    33. "http://img5.douban.com/lpic/s27452846.jpg");
    34. "http://img3.douban.com/lpic/s27325332.jpg");
    35. "http://img3.douban.com/lpic/s27325315.jpg");
    36. "http://img5.douban.com/lpic/s27389537.jpg");
    37. "http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199638985.jpg");
    38. "http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199637691.jpg");
    39. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2195768476.jpg");
    40. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2180002996.jpg");
    41. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2182978810.jpg");
    42. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2164841498.jpg");
    43. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2161445839.jpg");
    44. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2183221886.jpg");
    45. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2164383301.jpg");
    46. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199638985.jpg");
    47. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199407040.jpg");
    48. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2176900061.jpg");
    49. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2180405730.jpg");
    50. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2197059721.jpg");
    51. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196353608.jpg");
    52. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2187391526.jpg");
    53. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2185073849.jpg");
    54. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196328516.jpg");
    55. "http://img5.douban.com/view/movie_poster_cover/lpst/public/p2166850749.jpg");
    56. "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2183957412.jpg");
    57. "http://img3.douban.com/spic/s27459164.jpg");
    58. "http://img3.douban.com/spic/s27480483.jpg");
    59. "http://img3.douban.com/spic/s27460951.jpg");
    60. "http://img3.douban.com/spic/s27453772.jpg");
    61. "http://img3.douban.com/spic/s27410904.jpg");
    62. "http://img5.douban.com/spic/s27466008.jpg");
    63. "http://img3.douban.com/spic/s3259484.jpg");
    64. "http://img3.douban.com/spic/s3081692.jpg");
    65. "http://img3.douban.com/spic/s1400630.jpg");
    66. "http://img5.douban.com/spic/s4715377.jpg");
    67. "http://img3.douban.com/spic/s6201192.jpg");
    68. "http://img5.douban.com/spic/s2650727.jpg");
    69. "http://img3.douban.com/spic/s3636124.jpg");
    70. "http://img5.douban.com/spic/s3595798.jpg");
    71. "http://img3.douban.com/spic/s27078194.jpg");
    72. "http://img3.douban.com/view/event_poster/large/public/2a35a85a873e8e7.jpg");
    73. "http://img3.douban.com/view/event_poster/large/public/ce66e0569742fd7.jpg");
    74. "http://img3.douban.com/view/event_poster/large/public/4c44752e83900d9.jpg");
    75. "http://img3.douban.com/view/event_poster/large/public/d22f7b9e5c8c92c.jpg");
    76. }
    77.
    78. private class MyAdapter extends BaseAdapter{
    79. @Override
    80. public int getCount() {
    81. return imgUrls.size();
    82. }
    83. @Override
    84. public Object getItem(int position) {
    85. return imgUrls.get(position);
    86. }
    87. @Override
    88. public long getItemId(int position) {
    89. return position;
    90. }
    91. @Override
    92. public View getView(int position, View convertView, ViewGroup arg2) {
    93. null;
    94. if(convertView == null){
    95. new ViewHolder();
    96. null);
    97. viewHolder.image = (ImageView) convertView.findViewById(R.id.img);
    98. convertView.setTag(viewHolder);
    99. else{
    100. viewHolder = (ViewHolder) convertView.getTag();
    101. }
    102. new ImageTask(viewHolder.image);
    103. iamgeTask.execute(imgUrls.get(position));
    104. return convertView;
    105. }
    106. class ViewHolder{
    107. ImageView image;
    108. }
    109. }
    110. }


    加载图片的类:

    ImageTask.java


    1. public class ImageTask extends AsyncTask<String, Void, Bitmap> {  
    2. private ImageView iv;
    3. publicImageTask(ImageView iv){
    4. this.iv = iv;
    5. }
    6. @Override
    7. protected Bitmap doInBackground(String... param) {
    8. 0];
    9. try {
    10. new URL(imgUrl);
    11. try {
    12. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    13. InputStream in = conn.getInputStream();
    14. Bitmap bitmap = BitmapFactory.decodeStream(in);
    15. if(bitmap!=null){
    16. return bitmap;
    17. }
    18. catch (IOException e) {
    19. // TODO Auto-generated catch block
    20. e.printStackTrace();
    21. }
    22. catch (MalformedURLException e) {
    23. // TODO Auto-generated catch block
    24. e.printStackTrace();
    25. }
    26. return null;
    27. }
    28.
    29. @Override
    30. protected void onPostExecute(Bitmap result) {
    31. super.onPostExecute(result);
    32. if(result != null){
    33. iv.setImageBitmap(result);
    34. }
    35. }
    36.
    37. @Override
    38. protected void onPreExecute() {
    39. super.onPreExecute();
    40. }
    41. }

    标签:异步,http,douban,jpg,AsyncTask,img3,com,public,加载
    From: https://blog.51cto.com/u_11407799/5915735

    相关文章