# ohos-Universal-Image-Loader **Repository Path**: HarmonyOS-tpc/ohos-Universal-Image-Loader ## Basic Information - **Project Name**: ohos-Universal-Image-Loader - **Description**: 现在图像加载库的伟大祖先 UIL旨在为图像加载、缓存和显示提供一个强大、灵活和高度可定制的工具。它提供了大量的配置选项和良好的控制图像加载和缓存过程 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2021-04-01 - **Last Updated**: 2025-02-21 ## Categories & Tags **Categories**: harmonyos-loading, harmonyos-image **Tags**: None ## README # Universal Image Loader 现在图像加载库的伟大祖先 UIL旨在为图像加载、缓存和显示提供一个强大、灵活和高度可定制的工具。它提供了大量的配置选项和良好的控制图像加载和缓存过程。 ![image](https://gitee.com/openharmony-tpc/ohos-Universal-Image-Loader/blob/master/gif/imageloader.gif) ## 概述 * 多线程镜像加载(异步或同步) * ImageLoader配置(线程执行器、下载器、解码器、内存和磁盘缓存、显示图像选项等)的广泛定制 * 每个显示图像调用都有很多定制选项(存根图像、缓存开关、解码选项、位图处理和显示等) * 图像缓存在内存和/或磁盘(设备的文件系统或SD卡) * 监听加载过程(包括下载进度) 支持openHarmony 1.0+ ## 集成 ### 方案一 ``` 添加har包到lib文件夹内 在entry的gradle内添加如下代码 implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) ``` ### 方案二 ``` implementation project(':library') ``` ### 方案三 ``` implementation 'io.openharmony.tpc.thirdlib:ohos-Universal-Image-Loader:1.0.2' ``` ### 可接受的URI示例 ``` java "http://site.com/image.png" // from Web "dataability:///media/external/images/media/92" // from SD card "resources://resources/rawfile/UniversalImageLoader.png",//from reaources/rawfile "resources://resources/base/media/icon.png",//from reaources/base/media ``` **注意:**只有在您真正需要时才使用`resources://`!始终**考虑使用本地方式**加载resources - `Image.setImageResource(...)` 代替使用 `ImageLoader.loadImage(...)` ### 示例 ``` java //此配置调优为自定义配置。你可以调整每一个选项,你可以调整其中的一些选项 //也可以创建缺省配置 //ImageLoaderConfiguration.createDefault(this);方法。 ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context); config.threadPriority(Thread.NORM_PRIORITY - 2); config.denyCacheImageMultipleSizesInMemory(); config.diskCacheFileNameGenerator(new Md5FileNameGenerator()); config.diskCacheSize(50 * 1024 * 1024); // 50 MiB config.tasksProcessingOrder(QueueProcessingType.LIFO); config.writeDebugLogs(); // Remove for release app //使用配置初始化ImageLoader。 ImageLoader.getInstance().init(config.build()); ``` ``` java ImageLoader imageLoader = ImageLoader.getInstance(); //获取单例实例 ``` ``` java //加载图像,将其解码为PixelMap并在Image(或其他任何视图)中显示PixelMap //实现ImageAware接口) imageLoader.displayImage(imageUri, image); ``` ``` java //加载图片,解码为PixelMap,返回PixelMap回调 imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, Component component, PixelMap loadedImage) { // Do whatever you want with PixelMap } }); ``` ``` java //加载图片,解码为PixelMap,同步返回PixelMap PixelMap bmp = imageLoader.loadImageSync(imageUri); ``` ### 完成代码 ``` java //设置options DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(ResourceTable.Media_ic_stub)//加载中默认图片 .showImageForEmptyUri(ResourceTable.Media_ic_empty)//加载为空默认图片 .showImageOnFail(ResourceTable.Media_ic_error)//加载失败默认图片 .cacheInMemory(true)//是否开启内存缓存 .cacheOnDisk(true)//是否开启存储缓存 .considerExifParams(true) .displayer(new CircleBitmapDisplayer(Color.WHITE.getValue(), 5))//new RoundedBitmapDisplayer()、new FadeInBitmapDisplayer() .build(); //加载图像,将其解码为PixelMap并在Image(或其他任何视图)中显示PixelMap //实现ImageAware接口) imageLoader.displayImage(imageUri, image, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, Component component) { ... } @Override public void onLoadingFailed(String imageUri, Component component, FailReason failReason) { ... } @Override public void onLoadingComplete(String imageUri, Component component, PixelMap loadedImage) { ... } @Override public void onLoadingCancelled(String imageUri, Component component) { ... } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, Component component, int current, int total) { ... } }); ``` ``` java //加载图片,解码为PixelMap,返回PixelMap回调 ImageSize targetSize = new ImageSize(80, 50); // 结果PixelMap将适合此大小 imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, Component component, PixelMap loadedImage) { // Do whatever you want with PixelMap } }); ``` ``` java //加载图片,解码为PixelMap,同步返回PixelMap ImageSize targetSize = new ImageSize(80, 50); // 结果PixelMap将适合此大小 PixelMap bmp = imageLoader.loadImageSync(imageUri, targetSize, options); ``` ## 加载和显示任务流 ![image](https://gitee.com/openharmony-tpc/ohos-Universal-Image-Loader/blob/master/pic/UIL_Flow.png) ## 备选库 * [Fresco](https://github.com/facebook/fresco) * [Glide](https://github.com/bumptech/glide) * [Picasso](https://github.com/square/picasso) ## License Copyright 2011-2015 Sergey Tarasevich Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.