[Android] exercise using ListView (2), androidlistview

Source: Internet
Author: User

[Android] exercise using ListView (2), androidlistview

Exercise asynchronous tasks and LrcCache caching

Package com. android. test; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. imageLoaderConfiguration; import com. nostra13.universalimageloader. core. assist. failReason; import com. nostra13.universalimageloader. core. listener. imageLoadingListener; import android. app. activit Y; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. drawable. bitmapDrawable; import android. OS. asyncTask; import android. OS. bundle; import android. support. v4.util. lruCache; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listAdapter; import android. widget. listView; im Port android. widget. textView; public class MainActivity extends Activity {private ListView lv_images; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv_images = (ListView) findViewById (R. id. lv_images); ImageLoaderConfiguration configuration = ImageLoaderConfiguration. createDefault (this); ImageLoader. getInstan Ce (). init (configuration); ListAdapter adapter = new ImageAdapter (); lv_images.setAdapter (adapter );} /*** adapter * @ author taoshihan **/class ImageAdapter extends BaseAdapter {private LruCache <String, BitmapDrawable> mImageCache; public ImageAdapter () {int maxSize = (int) (Runtime. getRuntime (). maxMemory ()/8); mImageCache = new LruCache <String, BitmapDrawable> (maxSize) {protected int sizeOf (String ke Y, BitmapDrawable drawable) {return drawable. getBitmap (). getByteCount () ;};}}@ Override public View getView (int position, View convertView, ViewGroup parent) {String url = (String) getItem (position); View view; if (convertView! = Null) {view = convertView;} else {view = View. inflate (getApplicationContext (), R. layout. image_item, null);} ImageView imageView = (ImageView) view. findViewById (R. id. TV _image); BitmapDrawable bitmapDrawable = getBitmapFromMemoryCache (url); if (bitmapDrawable! = Null) {// read the cached image imageView. setImageDrawable (bitmapDrawable);} else {// read network image ImageWorkerTask task = new ImageWorkerTask (imageView); task.exe cute (url);} return view ;} /*** read the image from the cache * @ param key * @ return */public BitmapDrawable getBitmapFromMemoryCache (String key) {if (mImageCache. get (key )! = Null) {return mImageCache. get (key) ;}return null;} public void addBitmapToMemoryCache (String key, BitmapDrawable drawable) {if (getBitmapFromMemoryCache (key) = null) {mImageCache. put (key, drawable) ;}@ Override public int getCount () {// TODO Auto-generated method stub return Images. IMAGE_URLS.length ;}@ Override public Object getItem (int position) {return Images. IMAGE_URLS [position];} @ Overrid E public long getItemId (int position) {// TODO Auto-generated method stub return position ;} /*** asynchronous task ** @ author taoshihan **/class ImageWorkerTask extends AsyncTask <String, Void, BitmapDrawable> {private ImageView imageView; public ImageWorkerTask (ImageView imageView) {this. imageView = imageView;} @ Override protected BitmapDrawable doInBackground (String... params) {String imageUrl = params [0]; Bitmap bitmap = downLoadBitmap (imageUrl); BitmapDrawable drawable = new BitmapDrawable (getApplicationContext (). getResources (), bitmap); addBitmapToMemoryCache (imageUrl, drawable); return drawable ;}// download the image private Bitmap downLoadBitmap (String imageUrl) {Bitmap bitmap = null; try {URL url = new URL (imageUrl); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setConnectTimeout (10000 ); InputStream is = conn. getInputStream (); bitmap = BitmapFactory. decodeStream (is);} catch (Exception e) {} return bitmap;} // The downloaded @ Override protected void onPostExecute (BitmapDrawable result) {if (imageView! = Null & result! = Null) {imageView. setImageDrawable (result) ;}}} static class Images {public final static String [] IMAGE_URLS = new String [] {"http://img.my.csdn.net/uploads/201508/05/1438760758_3497.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760758_6667.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg ", "http://img.my.csdn.net/uploads/201508/05/1438760756_3304.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760755_6715.jpeg", "http://img.my.csdn.net/uploads/201508/05/1438760726_5120.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760726_8364.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760725_4031.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760724_9463.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760724_2371.jpg", "http://img.my.csdn.net/uploads/201508/05/1438760707_4653.jpg "};}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.