Skip to content

Instantly share code, notes, and snippets.

@rodrigosimoesrosa
Created January 13, 2022 17:00
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 rodrigosimoesrosa/9e6bc6bf2fe677826fbeaba892f4b61a to your computer and use it in GitHub Desktop.
Save rodrigosimoesrosa/9e6bc6bf2fe677826fbeaba892f4b61a to your computer and use it in GitHub Desktop.
Drawable transition color using vectors
transition = imageView.setTransitionDrawable(resourceID, R.color.firstColor, R.color.secondColor)
transition?.startTransition(3000)
fun Drawable.getBitmapDrawableFromVectorDrawable(context: Context): BitmapDrawable {
return BitmapDrawable(context.resources, getBitmapFromVectorDrawable())
}
fun Drawable.getBitmapFromVectorDrawable(): Bitmap {
val bitmap = Bitmap.createBitmap(
intrinsicWidth,
intrinsicHeight, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
setBounds(0, 0, canvas.width, canvas.height)
draw(canvas)
return bitmap
}
fun ImageView.setTransitionDrawable(vectorID: Int, firstColor: Int, secondColor: Int): TransitionDrawable? {
val drawables = arrayOfNulls<Drawable>(2)
val firstDrawable = ContextCompat.getDrawable(context, vectorID) ?: return null
DrawableCompat.setTint(firstDrawable, ContextCompat.getColor(context, firstColor))
drawables[0] = firstDrawable.getBitmapDrawableFromVectorDrawable(context)
val secondDrawable = ContextCompat.getDrawable(context, vectorID) ?: return null
DrawableCompat.setTint(secondDrawable, ContextCompat.getColor(context, secondColor))
drawables[1] = secondDrawable.getBitmapDrawableFromVectorDrawable(context)
val transition = TransitionDrawable(drawables)
transition.isCrossFadeEnabled = true
setImageDrawable(transition)
return transition
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment