Android 图像异步加载之Android

概述:

项目地址:https://github.com/nostra13/Android-Universal-Image-Loader

UIL(Universal-Image-Loader)异步图像加载、缓存和显示.这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影。

同类类库(Picasso),尽管Picasso拥有更好的API,但其缺乏自定义。而使用UIL构建器几乎可以配置所有(其中最重要的就是在抓取和缓存大型图片时,Picasso会失败)。

特点:

简单描述一下这个项目的结构:每一个图片的加载和显示任务都运行在独立的线程中,除非这个图片缓存在内存中,这种情况下图片会立即显示。如果需要的图片缓存在本地,他们会开启一个独立的线程队列。如果在缓存中没有正确的图片,任务线程会从线程池中获取,因此,快速显示缓存图片时不会有明显的障碍。

准备工作安装:

maven:

<dependency><groupId>com.nostra13.universalimageloader</groupId><artifactId>universal-image-loader</artifactId><version>1.9.3</version></dependency>Gradle:

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'添加网络和SD卡权限:

由于是使用过程中会图片获取要通过网络,并且有缓存设置,所以这2个权限必须要有。

<uses-permission android:name="android.permission.INTERNET" /><!– Include following permission if you want to cache images on SD card –><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />预配置Application or Activity class (before the first usage of ImageLoader)// Create global configuration and initialize ImageLoader with this configImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)….build();ImageLoader.getInstance().init(config);Acceptable URIs examples

"" // from Web"file:///mnt/sdcard/image.png" // from SD card"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)"content://media/external/images/media/13" // from content provider"content://media/external/video/media/13" // from content provider (video thumbnail)"assets://image.png" // from assets"drawable://" + R.drawable.img // from drawables (non-9patch images)NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables – ImageView.setImageResource(…) instead of using of ImageLoader.

示例

imageLoader.displayImage(imageUri, imageView);imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {// Do whatever you want with Bitmap}});// Load image, decode it to Bitmap and return Bitmap synchronouslyBitmap bmp = imageLoader.loadImageSync(imageUri);// Load image, decode it to Bitmap and return Bitmap to callbackImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this sizeimageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {// Do whatever you want with Bitmap}});// Load image, decode it to Bitmap and return Bitmap synchronouslyImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this sizeBitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);还可以通过ImageLoadingProgressListener监听进度。配置

ImageLoaderConfiguration应该是一个对于Application的全局对象,你应该只配置一次。

// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.// See the sample project how to use ImageLoader correctly.File cacheDir = StorageUtils.getCacheDirectory(context);ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800) // default = device screen dimensions 推荐.diskCacheExtraOptions(480, 800, null) //.推荐diskCacheExtraOptions(480, 800, null).taskExecutor(…).taskExecutorForCachedImages(…).threadPoolSize(3) // default 推荐1-5.threadPriority(Thread.NORM_PRIORITY – 2) // default.tasksProcessingOrder(QueueProcessingType.FIFO) // default.denyCacheImageMultipleSizesInMemory().memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //使用强引用的缓存使用它,不过推荐使用weak与strong引用结合的UsingFreqLimitedMemoryCache或者使用全弱引用的WeakMemoryCache.memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13) // default.diskCache(new UnlimitedDiscCache(cacheDir)) // default.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100).diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default.imageDownloader(new BaseImageDownloader(context)) // default.imageDecoder(new BaseImageDecoder()) // default.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default.writeDebugLogs().build();示例配置缓存目录File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache"); .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径 配置Display Options// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.// See the sample project how to use ImageLoader correctly.DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub) // resource or drawable.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable.showImageOnFail(R.drawable.ic_error) // resource or drawable.resetViewBeforeLoading(false) // default.delayBeforeLoading(1000).cacheInMemory(false) // default.cacheOnDisk(false) // default.preProcessor(…).postProcessor(…).extraForDownloader(…).considerExifParams(false) // default.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 推荐.imageScaleType(ImageScaleType.EXACTLY) 节省内存.bitmapConfig(Bitmap.Config.ARGB_8888) // default 推荐.bitmapConfig(Bitmap.Config.RGB_565)节省内存.decodingOptions(…).displayer(new SimpleBitmapDisplayer()) // default //推荐使用RoundedBitmapDisplayer (Displays bitmap with rounded corners)和FadeInBitmapDisplayer (Displays image with "fade in" animation.handler(new Handler()) // default.build();以上配置中的:1).imageScaleType(ImageScaleType imageScaleType) 是设置 图片的缩放方式缩放类型mageScaleType:EXACTLY :图像将完全按比例缩小的目标大小EXACTLY_STRETCHED:图片会缩放到目标大小完全IN_SAMPLE_INT:图像将被二次采样的整数倍IN_SAMPLE_POWER_OF_2:图片将降低2倍,直到下一减少步骤,使图像更小的目标大小NONE:图片不会调整2).displayer(BitmapDisplayer displayer) 是设置 图片的显示方式显示方式displayer:RoundedBitmapDisplayer(int roundPixels)设置圆角图片FakeBitmapDisplayer()这个类什么都没做FadeInBitmapDisplayer(int durationMillis)设置图片渐显的时间SimpleBitmapDisplayer()正常显示一张图片  注意的问题

1、缓存默认情况下是没有启用的。可以通过配置DisplayImageOptions来启用。

觉得自己做的到和不做的到,其实只在一念之间

Android 图像异步加载之Android

相关文章:

你感兴趣的文章:

标签云: