Created
January 13, 2022 17:00
-
-
Save rodrigosimoesrosa/9e6bc6bf2fe677826fbeaba892f4b61a to your computer and use it in GitHub Desktop.
Drawable transition color using vectors
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
transition = imageView.setTransitionDrawable(resourceID, R.color.firstColor, R.color.secondColor) | |
transition?.startTransition(3000) |
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
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