B4A Library UniversalImageLoader V1.0.2

This is a wrapper for this Github Project.
Actually it only can work with Imageviews.

UniversalImageLoader
Version:
1.02
  • UniversalImageLoader
    Events:
    • onProgressUpdate (String imageUri As , View view As , int current As , int total As )
    • onloadingcomplete (imageUri As String, v as Object As , bmp as Object As )
    • onloadingfailed (imageUri As String, v as Object As , reason As String)
    • onloadingstarted (imageUri As String, v as Object As )
    Methods:
    • Initialize (EventName As String, displayoptions As DisplayImageOptions)
    • clearDiscCache
    • clearMemoryCache
    • loadimg (imageUri As String, iv As ImageView)
    • loadimg2 (imageUri As String, iv As ImageView)
    • loadimgSync (imageUri As String) As Bitmap
      Loads a image from the url imageUri and return a bitmap
      This methos is a Sync method.
    Permissions:
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.INTERNET
    • android.permission.WRITE_EXTERNAL_STORAGE
  • uilOptions
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • bitmapConfig565 As UilOptions
    • bitmapConfig8888 As UilOptions
    • build As DisplayImageOptions
    • cacheInMemory (cache As Boolean) As UilOptions
    • cacheOnDisc (disc As Boolean) As UilOptions
    • delayBeforeLoading (delay As Int) As UilOptions
    • resetViewBeforeLoading (reset As Boolean) As UilOptions
    • showImageForEmptyUri (d As Drawable) As UilOptions
    • showImageOnFail (d As Drawable) As UilOptions
    • showImageOnLoading (d As Drawable) As UilOptions

This library is Donationware. You can download the library, you can test the library. But if you want to USE the library in your App you need to Donate for it.
Please click here to donate (You can donate any amount you want to donate for the library (or my work) :)


Example 1
uil001.png


Example 2 is using the CustomListViewClass from erel and shows a list of 200+ Flags
uil002.png


The Flags used in this Example can be downloaded here.
 

Attachments

  • libUniversalImageLoader_V1.0.0.zip
    145.5 KB · Views: 557
  • UniversalImagLoaderEx2_Flags.zip
    17.5 KB · Views: 509
  • libUniversalImageLoader_V1.0.1.zip
    142.6 KB · Views: 461
  • UniversalImagLoaderEx.zip
    13.4 KB · Views: 471
  • libUniversalImageLoader_V1.0.2.zip
    144.3 KB · Views: 557
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Hi Great Don ...where's different bitmapConfig8888 and bitmapConfig565 ?
Thanks and nice day
 

MarcoRome

Expert
Licensed User
Longtime User
Found thank you anyway
If you often got OutOfMemoryError in your app using Universal Image Loader then:
  • Disable caching in memory. If OOM is still occurs then it seems your app has a memory leak. Use MemoryAnalyzer to detect it. Otherwise try the following steps (all of them or several):
  • Reduce thread pool size in configuration (.threadPoolSize(...)). 1 - 5 is recommended.
  • Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
  • Use .imageScaleType(ImageScaleType.EXACTLY)
  • Use .diskCacheExtraOptions(480, 320, null) in configuration
 

icefairy333

Active Member
Licensed User
Longtime User
please add:
B4X:
public uilOptions RoundedBitmapDisplayer(int cornerRadiusPixels,int marginPixels){
      getObject().displayer(new RoundedBitmapDisplayer(cornerRadiusPixels,marginPixels));
      return this;
  }
 
  public uilOptions CircleBitmapDisplayer(int strokeColor,float strokeWidth){
      getObject().displayer(new CircleBitmapDisplayer(strokeColor,strokeWidth));
      return this;
  }
to uilOptions to support round image
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Don, a curiosity, even if I use the following statement:

iloptions.bitmapConfig565.cacheInMemory(True).cacheOnDisc(True).delayBeforeLoading(0)

When i connect, it seems that in any case the images are reloaded. Using the same initial time.
Inserting Memory and Disc = True i would expect the same chje were loaded instantly.
Where i wrong?
Thank you
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
did you used this options in initialize of the imageloader?
My code is:
B4X:
iloptions.Initialize
iloptions.bitmapConfig565.cacheInMemory(True).cacheOnDisc(True).delayBeforeLoading(0)
Carica_Copertina.Initialize("Carica_Copertina",iloptions.build)
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Don. I have this code:

B4X:
iloptions_sottofondi.Initialize
    iloptions_sottofondi.bitmapConfig565.cacheInMemory(False).cacheOnDisc(False).delayBeforeLoading(0)
    Carica_Copertina_Sottofondi.Initialize("Carica_Copertina_Sottofondi",iloptions_sottofondi.build)
    Carica_Copertina_Sottofondi.loadimg(Starter.tipo_immagine_sottofondo, img_sottofondo_1)

and work, dowload picture web and i see... now if i wanted to change the PNG through this code:

B4X:
img_sottofondo_1.Bitmap = LoadBitmap(File.DirAssets, "papo26.png")

Nothing happens.
where i wrong ?
Any solution ?
 

icefairy333

Active Member
Licensed User
Longtime User
Try this version please
there are two kinds of cache dir:
1./data/data/package/cache/uil-images
2./sdcard/android/data/package/cache/uil-images
I've checked all of them cannot find any cache files,and I write an demo with eclipse,It works with cache,the cache files is in 2;
ToolImage.java
B4X:
package com.example.uildemo;

import java.io.File;

import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

/**
* 图片加载工具类
* @author 曾繁添
* @version 1.0
*
*/
public abstract class ToolImage {

    private static ImageLoader imageLoader;

    /**
     * 初始化ImageLoader
     */
    public static ImageLoader initImageLoader(Context context) {
        // 获取到缓存的目录地址
        File cacheDir = StorageUtils.getOwnCacheDirectory(context,"ImageLoader/Cache");
        // 创建配置ImageLoader,可以设定在Application,设置为全局的配置参数
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCacheExtraOptions(480, 800)//缓存文件的最大长宽
        // Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息,最好不要设置这个
        // .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,null)
        .threadPoolSize(3)// 线程池内加载的数量
        .threadPriority(Thread.NORM_PRIORITY - 2)// 线程优先级
        /*
         * When you display an image in a small ImageView and later you
         * try to display this image (from identical URI) in a larger
         * ImageView so decoded image of bigger size will be cached in
         * memory as a previous decoded image of smaller size. So the
         * default behavior is to allow to cache multiple sizes of one
         * image in memory. You can deny it by calling this method: so
         * when some image will be cached in memory then previous cached
         * size of this image (if it exists) will be removed from memory
         * cache before.
         */
        // .denyCacheImageMultipleSizesInMemory()
        // You can pass your own memory cache implementation
        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *1024))
        .memoryCacheSize(2 * 1024 * 1024)
        .diskCacheFileCount(1000)
        .diskCacheSize(20 * 1024 * 1024)// 硬盘缓存50MB
        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())// 将保存的时候的URI名称用MD5
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        //.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
        //.imageDownloader(new BaseImageDownloader(context, 5 * 1000,30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
        .build();

        // 全局初始化此配置
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
       
        return imageLoader;
    }
   
    /**
     * 获取渐现显示选项
     * @param loadingImageResId 加载期间显示的图片
     * @param errorImageResid 加载错误时显示的图片
     * @param emptyImageResId 空图片或者解析图片出错时显示的图片
     * @return
     */
    public static DisplayImageOptions getFadeOptions(int loadingImageResId,int errorImageResid,int emptyImageResId ) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
        //设置图片在下载期间显示的图片
        .showStubImage(loadingImageResId)
        // 设置图片加载/解码过程中错误时候显示的图片
        .showImageOnFail(errorImageResid)
        // 设置图片Uri为空或是错误的时候显示的图片
        .showImageForEmptyUri(emptyImageResId)
        // 设置下载的图片是否缓存在内存中
         .cacheInMemory()
        // 设置下载的图片是否缓存在SD卡中
        .cacheOnDisc()
        /**设置图片缩放方式:
        EXACTLY :图像将完全按比例缩小到目标大小
        EXACTLY_STRETCHED:图片会缩放到目标大小完全
        IN_SAMPLE_INT:图像将被二次采样的整数倍
        IN_SAMPLE_POWER_OF_2:图片将降低2倍,直到下一减少步骤,使图像更小的目标大小
        NONE:图片不会调整
        ***/
        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
        // 设置图片的解码类型
        .bitmapConfig(Bitmap.Config.RGB_565)
        // 设置图片下载前的延迟
        .delayBeforeLoading(100)
        // delayInMillis为你设置的延迟时间
        // 设置图片加入缓存前,对bitmap进行设置
        // .preProcessor(BitmapProcessor preProcessor)
       
        /**
         * 图片显示方式:
         *  RoundedBitmapDisplayer(int roundPixels)设置圆角图片
         *  FakeBitmapDisplayer()这个类什么都没做
         *  FadeInBitmapDisplayer(int durationMillis)设置图片渐显的时间
       * SimpleBitmapDisplayer()正常显示一张图片
         **/
        .displayer(new FadeInBitmapDisplayer(1*1000))// 渐显--设置图片渐显的时间
        .build();
        return options;
    }
   
    /**
     * 获取默认显示配置选项
     */
    public static DisplayImageOptions getDefaultOptions(){
        return DisplayImageOptions.createSimple();
    }

    /**
     * 清除缓存
     */
    public static void clearCache() {
        imageLoader.clearMemoryCache();
        imageLoader.clearDiscCache();
    }
   
    public static ImageLoader getImageLoader(){
        return imageLoader;
    }
    public static void LoadImageWithCache(String uri,ImageView v) {
        DisplayImageOptions.Builder dio=new DisplayImageOptions.Builder();
        dio.cacheOnDisk(true);
        imageLoader.displayImage(uri, v,dio.build());
    }
}
using with:
B4X:
ToolImage.initImageLoader(this);
iv=(ImageView) findViewById(R.id.iv);
       
        ToolImage.LoadImageWithCache("http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Bugs/H-P/monarch-butterfly-orange-flower.jpg", iv);
 

MarcoRome

Expert
Licensed User
Longtime User
you can download it(v1.2) from here it's path is:b4alibs\b4amyuil\libs in the same folder there is a demo show you how to use.
Hi icefairy is possible download this files in another place ( your link ask me credential to download )
Thank you
Marco
 
Top