首页 > 代码库 > Android-异步加载图片

Android-异步加载图片

刚刚复习了之前学的异步加载图片,于是决定写下来,增强记忆

  想要加载图片,必须获得所有图片的URL地址。这里图片的URL地址存在了List数组。

  异步加载图片有两种方式(我这里使用两种方式):1.自己开线程来加载图片,然后使用Handler来更新UI;2.使用AsyncTask来加载图片。

1.使用子线程来加载图片。

 

 1 private Handler handler = new Handler()
 2     {
 3         @Override
 4         public void handleMessage(Message msg) {
 5             if(mImageView.getTag().equals(mUrlString))
 6             {
 7                 mImageView.setImageBitmap(mBitmap);
 8             }
 9         }
10     };
11     public  void loadImageByThread(ImageView imageView, final String urlString) {
12         mImageView = imageView;
13         mUrlString = urlString;
14         new Thread() {
15             @Override
16             public void run() {
17                 mBitmap = getBitmap(mUrlString);
18                 Message message = Message.obtain();
19                 message.obj = urlString;
20                 handler.sendMessage(message);
21             }
22         }.start();
23     }
 1 private  Bitmap getBitmap(String urlString)
 2     {
 3         Bitmap bitmap = null;
 4 
 5         URL url = null;
 6         HttpURLConnection httpURLConnection = null;
 7         InputStream is = null;
 8         try {
 9             url = new URL(urlString);
10             httpURLConnection = (HttpURLConnection) url.openConnection();
11             is = httpURLConnection.getInputStream();
12             bitmap = BitmapFactory.decodeStream(is);
13         } catch (MalformedURLException e) {
14             e.printStackTrace();
15         } catch (IOException e) {
16             e.printStackTrace();
17         }
18         finally {
19             try {
20                 is.close();
21             } catch (IOException e) {
22                 e.printStackTrace();
23             }
24         }
25         return bitmap;
26     }

2.使用AsyncTask来加载图片

 1 private class MyAsyncTask extends AsyncTask<String, Void, Bitmap>
 2     {
 3 
 4         protected Bitmap doInBackground(String... params) {
 5             return getBitmap(params[0]);
 6         }
 7 
 8         @Override
 9         protected void onPostExecute(Bitmap bitmap) {
10 
11             if(mImageView != null && bitmap != null && mUrlString.equals(mImageView.getTag()))
12             {
13                 mImageView.setImageBitmap(bitmap);
14                 Utils.addBitmapToLruCache(bitmap, mUrlString);
15             }
16         }
17     }

3.使用内存来缓存图片

 1     private static long mMaxMemory = Runtime.getRuntime().maxMemory();
 2     private static long mApplyMemory = mMaxMemory / 10;
 3     private static LruCache<String, Bitmap> mLruCache = new LruCache<String, Bitmap>((int)mApplyMemory)
 4     {
 5         @Override
 6         protected int sizeOf(String key, Bitmap value) {
 7             return value.getByteCount();
 8         }
 9     };
10     public static boolean isBitmapExistsInLruCache(String urlString)
11     {
12         return mLruCache.get(urlString) != null;
13     }
14     public static void addBitmapToLruCache(Bitmap bitmap, String urlString)
15     {
16         //Log.i("main", "图片大小 = " + bitmap.getByteCount()  * 1.0f/ 1024 / 1024);
17         mLruCache.put(urlString, bitmap);
18     }
19     public static Bitmap getBitmaoLruCache(String urlString)
20     {
21         return  mLruCache.get(urlString);
22     }

 

Android-异步加载图片