Skip to content

Instantly share code, notes, and snippets.

@sezabass
Last active October 22, 2019 12:30
Show Gist options
  • Save sezabass/267a73e739beb7a218867e47f0d704c6 to your computer and use it in GitHub Desktop.
Save sezabass/267a73e739beb7a218867e47f0d704c6 to your computer and use it in GitHub Desktop.
BindingAdapter for ImageView that adds transparency to a given bitmap
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="[package.path.]ImageViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="320dp"
android:layout_height="240dp"
app:transparentImage="@{viewModel.image}"
app:transparentColor="@{viewModel.invisibleColor}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
import android.graphics.Bitmap
import android.graphics.Color
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat.getColor
import androidx.databinding.BindingAdapter
import coil.api.load
object BindingAdapters {
@BindingAdapter(value = ["transparentImage", "transparentColor"], requireAll = true)
@JvmStatic
fun loadTransparentImage(
imageView: ImageView,
transparentImage: Bitmap?,
@ColorRes transparentColor: Int
) {
val context = imageView.context
val resolvedColor = getColor(context, transparentColor)
transparentImage?.let { bmp ->
val width = bmp.width
val height = bmp.height
val pixels = IntArray(width * height)
bmp.getPixels(pixels, 0, width, 0, 0, width, height)
val newPixels = mutableListOf<Int>()
pixels.mapTo(newPixels) { pixel ->
if (resolvedColor == pixel) Color.TRANSPARENT else pixel
}
val newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
newBmp.setPixels(newPixels.toIntArray(), 0, width, 0, 0, width, height)
bmp.recycle()
imageView.load(newBmp)
}
}
}
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.URL
internal class ImageViewModel : ViewModel() {
val image = MutableLiveData<Bitmap>()
fun fetchImage() {
viewModelScope.launch {
val bitmap = withContext(Dispatchers.IO) {
val url =
"https://cdn.dribbble.com/users/1787323/screenshots/5911202/megaman_dribbble-04_2x.png"
val urlValue = URL(url)
BitmapFactory.decodeStream(urlValue.openConnection().getInputStream())
}
image.postValue(bitmap)
}
}
}
@mugveiga
Copy link

excelente! funcionou aqui :D obrigado!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment