Kotlin code

We define a separate class called ImageLoaderGlideModule which extends AppGlideModule(). The annotation @GlideModule on the class enables the app to have access to the GlideApp instance. The GlideApp instance can be used across various activities in our application:

package com.natarajan.imageloader
/**
*
Created by admin on 4/14/2018.
*/
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

@GlideModule
class ImageLoaderGlideModule : AppGlideModule()

We need to make the following changes in the MainActivity Kotlin class to have the image loaded by Glide and display it when the app is launched.

Similar to Picasso, Glide also has a simple syntax for loading an image from a specified URL:

GlideApp.with(this).load("URL").into(imageView);

The modified MainActivity Kotlin class should look like the one shown here:

package com.natarajan.imageloader

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

if(imageView != null){

GlideApp.with(this).load("http://goo.gl/gEgYUd").into(imageView);
}
}
}

We have done all the changes required for Glidebuild.gradle, Proguard.rules, and the Kotlin class files. We should see the app load the image from the specified URL and display it in the ImageView as shown:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset