Created
August 6, 2020 04:28
-
-
Save Evin1-/21651c639091c1fde298f64d98016d26 to your computer and use it in GitHub Desktop.
Loop Cupcakes. Advanced image loading Glide.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<ImageView | |
android:id="@+id/main_image_one" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginStart="16dp" | |
android:layout_marginTop="16dp" | |
android:layout_marginEnd="39dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_four" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_two" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_two" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginTop="16dp" | |
android:layout_marginEnd="40dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_five" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_three" | |
app:layout_constraintStart_toEndOf="@+id/main_image_one" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_three" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginTop="16dp" | |
android:layout_marginEnd="16dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_six" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/main_image_two" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_four" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginStart="16dp" | |
android:layout_marginEnd="39dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_seven" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_five" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_one" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_five" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginEnd="40dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_eight" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_six" | |
app:layout_constraintStart_toEndOf="@+id/main_image_four" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_two" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_six" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginEnd="16dp" | |
android:layout_marginBottom="17dp" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_nine" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/main_image_five" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_three" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_seven" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginStart="16dp" | |
android:layout_marginEnd="39dp" | |
android:layout_marginBottom="18dp" | |
android:adjustViewBounds="true" | |
app:layout_constraintBottom_toTopOf="@+id/main_image_ten" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_eight" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_four" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_eight" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginEnd="40dp" | |
android:layout_marginBottom="381dp" | |
android:adjustViewBounds="true" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/main_image_nine" | |
app:layout_constraintStart_toEndOf="@+id/main_image_seven" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_five" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_nine" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginEnd="16dp" | |
android:layout_marginBottom="381dp" | |
android:adjustViewBounds="true" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/main_image_eight" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_six" | |
tools:ignore="ContentDescription" /> | |
<ImageView | |
android:id="@+id/main_image_ten" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginBottom="263dp" | |
android:adjustViewBounds="true" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="@+id/main_image_seven" | |
app:layout_constraintStart_toStartOf="@+id/main_image_seven" | |
app:layout_constraintTop_toBottomOf="@+id/main_image_seven" | |
tools:ignore="ContentDescription" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.INTERNET" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// build.gradle (:app) | |
apply plugin: 'kotlin-kapt' | |
dependencies { | |
//.. https://github.com/bumptech/glide | |
//.. https://developer.android.com/jetpack/androidx/releases/swiperefreshlayout | |
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" | |
implementation 'com.github.bumptech.glide:glide:4.11.0' | |
kapt 'com.github.bumptech.glide:compiler:4.11.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
loadUsingGeneratedClass() | |
loadShowingProgressBar() | |
loadIntoCircle() | |
loadImageWithoutCaching() | |
loadGifIntoImage() | |
loadImageWithErrorDrawable() | |
loadAndResizeImage() | |
loadWithListeners() | |
loadAndCenterCrop() | |
loadWithCrossFade() | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
menuInflater.inflate(R.menu.main_menu, menu) | |
val menuItem = menu.findItem(R.id.main_menu_two) | |
loadIntoMenuItem(menuItem) | |
return true | |
} | |
private fun loadUsingGeneratedClass() { | |
val imageView = findViewById<ImageView>(R.id.main_image_one) | |
GlideApp.with(this).load("https://shorturl.at/lwGM8").into(imageView) | |
} | |
private fun loadShowingProgressBar() { | |
val imageView = findViewById<ImageView>(R.id.main_image_two) | |
val circularProgress = CircularProgressDrawable(this) | |
circularProgress.strokeWidth = 5f | |
circularProgress.centerRadius = 30f | |
circularProgress.start() | |
GlideApp.with(this) | |
.load("https://shorturl.at/htL12") | |
.placeholder(circularProgress) | |
.into(imageView) | |
} | |
private fun loadIntoCircle() { | |
val imageView = findViewById<ImageView>(R.id.main_image_three) | |
GlideApp.with(this) | |
.load("https://shorturl.at/corV0") | |
.circleCrop() | |
.into(imageView) | |
} | |
private fun loadImageWithoutCaching() { | |
val imageView = findViewById<ImageView>(R.id.main_image_four) | |
GlideApp.with(this) | |
.load("https://shorturl.at/fmns8") | |
.diskCacheStrategy(DiskCacheStrategy.NONE) | |
.into(imageView) | |
} | |
private fun loadGifIntoImage() { | |
val imageView = findViewById<ImageView>(R.id.main_image_five) | |
GlideApp.with(this) | |
.asGif() | |
.load("https://shorturl.at/gvJP4") | |
.into(imageView) | |
} | |
private fun loadImageWithErrorDrawable() { | |
val imageView = findViewById<ImageView>(R.id.main_image_six) | |
GlideApp.with(this) | |
.load("https://shorturl.at/CJMR6") | |
.error(R.drawable.ic_launcher_background) | |
.into(imageView) | |
} | |
private fun loadAndResizeImage() { | |
val imageView = findViewById<ImageView>(R.id.main_image_seven) | |
GlideApp.with(this) | |
.load("https://shorturl.at/amHRU") | |
.override(50, 50) | |
.into(imageView) | |
} | |
private fun loadWithListeners() { | |
val imageView = findViewById<ImageView>(R.id.main_image_eight) | |
GlideApp.with(this) | |
.load("https://shorturl.at/kALW1") | |
.listener(CustomListener) | |
.into(imageView) | |
} | |
private fun loadAndCenterCrop() { | |
val imageView = findViewById<ImageView>(R.id.main_image_nine) | |
GlideApp.with(this) | |
.load("https://shorturl.at/svDST") | |
.centerCrop() | |
.into(imageView) | |
} | |
private fun loadWithCrossFade() { | |
val imageView = findViewById<ImageView>(R.id.main_image_ten) | |
GlideApp.with(this) | |
.load("https://shorturl.at/yJOV8") | |
.transition(withCrossFade()) | |
.into(imageView) | |
} | |
private fun loadIntoMenuItem(menuItem: MenuItem) { | |
GlideApp.with(this) | |
.asBitmap() | |
.load("https://shorturl.at/biIW7") | |
.into(MenuTarget(menuItem, resources)) | |
} | |
} | |
@GlideModule | |
class MyGlideModule : AppGlideModule() | |
private class MenuTarget( | |
private val menuItem: MenuItem, | |
private val resources: Resources | |
) : CustomTarget<Bitmap>(100, 100) { | |
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) { | |
menuItem.icon = BitmapDrawable(resources, resource) | |
} | |
override fun onLoadCleared(placeholder: Drawable?) { | |
menuItem.icon = null | |
} | |
} | |
private object CustomListener : RequestListener<Drawable> { | |
override fun onLoadFailed( | |
e: GlideException?, | |
model: Any?, | |
target: Target<Drawable>?, | |
isFirstResource: Boolean | |
): Boolean { | |
Log.e("TAG_ ", "Error loading image $e") | |
e?.printStackTrace() | |
return false | |
} | |
override fun onResourceReady( | |
resource: Drawable?, | |
model: Any?, | |
target: Target<Drawable>?, | |
dataSource: DataSource?, | |
isFirstResource: Boolean | |
): Boolean { | |
Log.d("TAG_ ", "Image loaded successfully $resource") | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment