Skip to content
Advertisement

what is the best way to show a multiple images in android studio?

I have a multiple images about 600 image I want to use it for page in viewPager. I put it all in drawable but it’s take space what is the best way to store image?

Advertisement

Answer

Uploading them to a Cloudstorage (or Discord) and then loading the images with their links using Coil or Glide would be a way.

As a Kotlin-User Coil is the way to go. It’s very lightweight, for additional functionalities (like animating Gifs, loading SVGs) take a look here.

Dependency:

implementation("io.coil-kt:coil:1.2.1")

Load image from Url (trailing Lambda is optional):

imageView.load(imageUrl){
crossfade(true)
placeholder(R.drawable.loading)
transformations(CircleCropTransformation())
}

If You only have one Imageview per Page inside your Viewpager, You might create an Array with all the Urls given and then load them like so:

private var pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
         imageView.load(image_urls[position])
     }
 }

If you prefer using Java, then go for Glide:

Dependency:

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

Load image from Url (the options are optional):

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.loading)
                    .error(R.drawable.error);

 Glide.with(this).load(image_url).apply(options).into(imageView);

Load the image whenever another page is selected.

viewPager.addOnPageChangeListener(new OnPageChangeListener() {
     override void onPageSelected(int position) {
     Glide.with(this).load(image_urls[position]).apply(options).into(imageView);
     }
 }

Hope it helps 🙂

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement