想要实现网络中图片的加载,主线程是不行的,方法有两个,一个是使用线程Tread(),另一个就是使用AsyncTask,AsyncTask其实也是线程。我看过慕课网里边一个叫做《Android必学-异步加载》的视频,部分代码:
1 //-----------------------AsyncTask异步访问图片----------------------- 2 3 public void showImageByAsyncTask(ImageView imageView,String url){ 4 new NewsAsyncTask(imageView).execute(url); 5 } 6 7 public class NewsAsyncTask extends AsyncTask{ 8 9 private ImageView mImageView;10 11 public NewsAsyncTask(ImageView imageView){12 mImageView = imageView;13 }14 15 @Override16 protected Bitmap doInBackground(String... params) {17 // TODO Auto-generated method stub18 return getBitmapFromUrl(params[0]);19 }20 21 @Override22 protected void onPostExecute(Bitmap bitmap) {23 // TODO Auto-generated method stub24 super.onPostExecute(bitmap);25 mImageView.setImageBitmap(bitmap);26 }27 }28 29 //------------------------------------------------------------------30 }
仅供参考学习!!!