Skip to content

@LoadImage annotation

crypticminds edited this page Feb 15, 2020 · 3 revisions

@LoadImage annotation can be used over an ImageView or any of its variants (Eg. CircularImageView ) to load an image from a URL and set it into the view while caching it for future use.

Parameters

  • imageViewResourceId (Int) -> The resource id of the ImageView . This is a mandatory parameter that will be used to bind the view from the layout to the annotated variable.
  • url (String) -> The Url from where the image will be downloaded. This is a mandatory parameter.
  • placeHolder (Int)-> The resource id of the placeholder image that will be displayed until the image is downloaded and loaded into the ImageView. The id should be of a drawable resource. This is an optional parameter.
  • enableLoadingAnimation (Boolean) -> A boolean value representing if a loading animation should be shown or not. The loading animation will only work if a placeholder image has been provided. The default animation will rotate the placeholder image. In upcoming releases, there will be an option to provide custom animations such as fade-in, scale-up, etc.
  • persistImageToDisk (Boolean) -> A boolean value representing if the image should be stored into the disk after it has been downloaded from the URL. The default value is false. To control how long any data should be kept in the disk use the timeToLiveForDiskStorage parameter in the Cache.initialize method and pass the number of days data should be kept in the memory of the device.

Usage

  • Annotate the image view with @LoadImage. The variable needs to be public so that ColdStorae can access it and initialize it.

    @LoadImage(
         R.id.image_1,
         "https://images.unsplash.com/photo-1549740425-5e9ed4d8cd34?ixlib=rb-1.2.1&w=1000&q=80",
         placeHolder = R.drawable.loading, enableLoadingAnimation = true , persistImageToDisk = true
     )
     lateinit var imageWithAnimation: ImageView
    
  • Bind the class that contains the ImageView to the Cache. The cache binding is currently supported for Activity, Fragment and other Views (For example if you have created a custom view that contains ImageViews).

    • For activities, the binding should be done after setContentView
     override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.load_image_example)
            Cache.bind(this)
        }
    • For fragments, the binding should be done in onViewCreatedMethod
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         Cache.bind(this)
     }
Clone this wiki locally