Skip to content

Instantly share code, notes, and snippets.

@Evin1-
Last active August 6, 2020 03:33
Show Gist options
  • Save Evin1-/4c7130471c5665df5fa3d550bf3562a8 to your computer and use it in GitHub Desktop.
Save Evin1-/4c7130471c5665df5fa3d550bf3562a8 to your computer and use it in GitHub Desktop.
Loop Cupcakes. Image Loading Glide
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toTopOf="@id/main_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/main_image"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/main_text"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
<uses-permission android:name="android.permission.INTERNET" />
// build.gradle (:app)
dependencies {
/* This will be enough for simple loading of images, for advanced image loading
* the kapt dependency and generated API might be needed */
//.. https://github.com/bumptech/glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.main_image)
/* A very simple loading would be enough with the below line, Glide takes care of the
* lazy loading of the image, creating a worker thread, canceling the request, clearing the
* resource and a few other optimizations, it's recommended to use the Glide.with that
* receives either an Activity or a Fragment, since it can make it lifecycle aware. As mentioned
* in this answer in Stack Overflow. The best practice is to use the closest possible context or
* fragment to avoid unused request completions.
* https://stackoverflow.com/questions/31964737/glide-image-loading-with-application-context */
Glide.with(this).load("https://shorturl.at/lwGM8").into(imageView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment