Skip to content

Instantly share code, notes, and snippets.

@AnwarShahriar
Created June 2, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnwarShahriar/e252e521db9b5f03c7a102c356d7b238 to your computer and use it in GitHub Desktop.
Save AnwarShahriar/e252e521db9b5f03c7a102c356d7b238 to your computer and use it in GitHub Desktop.
Image View size manipulation and conditional image loading with glide
package me.anwarshahriar.bitmapwithglide
import android.content.Context
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_main.*
val data = Array(1000) { i ->
when {
i % 2 == 0 -> "test_image_1.jpg"
i % 3 == 0 -> "test_image_2.jpg"
i % 5 == 0 -> "test_image_3.png"
i % 7 == 0 -> "test_image_4.png"
else -> "test_image_5.gif"
}
}
fun getDimens(context: Context, imageName: String): Pair<Int, Int> {
val decodeOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeStream(context.assets.open(imageName), null, decodeOptions)
return Pair(decodeOptions.outWidth, decodeOptions.outHeight)
}
fun getImagePath(imageName: String) = "//android_asset/$imageName"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
rvImages.setItemViewCacheSize(30)
rvImages.layoutManager = LinearLayoutManager(this)
val maxDimen = (displayMetrics.widthPixels * 0.6).toInt() // 60% of screen width
rvImages.adapter = ImageAdapter(maxDimen, maxDimen, displayMetrics.density)
}
}
class ImageAdapter(private val width: Int, private val height: Int, private val density: Float) :
RecyclerView.Adapter<ImageViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.image_item, parent, false)
return ImageViewHolder(view, maxWidth = width, maxHeight = height, density = density)
}
override fun getItemCount() = data.size
override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
holder.bind(data[position])
}
}
class ImageViewHolder(itemView: View, val maxWidth: Int, val maxHeight: Int, val density: Float) :
RecyclerView.ViewHolder(itemView) {
private val ivWallpaper: ImageView = itemView.findViewById(R.id.ivWallpaper)
private val layoutParams = ivWallpaper.layoutParams
private val glideRequestManager = Glide.with(itemView)
fun bind(imageName: String) {
val (width, height) = getDimens(itemView.context, imageName)
val scaleFactor = maxWidth / (width * 1f)
if (width * density <= maxWidth && height * density <= maxHeight) {
layoutParams.width = (width * density).toInt()
layoutParams.height = (height * density).toInt()
ivWallpaper.layoutParams = layoutParams
glideRequestManager.asBitmap().load(getImagePath(imageName)).into(ivWallpaper)
} else if (height * scaleFactor > maxHeight) {
layoutParams.width = maxWidth
layoutParams.height = maxHeight
ivWallpaper.layoutParams = layoutParams
glideRequestManager.asBitmap().load(getImagePath(imageName)).centerCrop().into(ivWallpaper)
} else {
layoutParams.width = maxWidth
layoutParams.height = (height * scaleFactor).toInt()
ivWallpaper.layoutParams = layoutParams
glideRequestManager.asBitmap().load(getImagePath(imageName)).into(ivWallpaper)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment