Created
August 17, 2020 16:14
-
-
Save PaulWoitaschek/b8f78cfabad832fcabf179cc30dbd2b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.example.outlinetransition | |
import android.graphics.Color | |
import android.graphics.Outline | |
import android.os.Bundle | |
import android.view.View | |
import android.view.ViewOutlineProvider | |
import android.widget.FrameLayout | |
import androidx.annotation.ColorInt | |
import androidx.annotation.Px | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.transition.TransitionManager | |
import com.google.android.material.transition.MaterialContainerTransform | |
import kotlinx.coroutines.MainScope | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val red = circleLayout(Color.BLUE, 300) | |
val blue = circleLayout(Color.RED, 600) | |
suspend fun swap(view: View) { | |
delay(2000) | |
val transition = MaterialContainerTransform() | |
TransitionManager.beginDelayedTransition(findViewById(android.R.id.content), transition) | |
setContentView(view) | |
} | |
MainScope().launch { | |
while (true) { | |
swap(red) | |
swap(blue) | |
} | |
} | |
} | |
private fun circleLayout(@ColorInt color: Int, @Px size: Int): View { | |
val circle = View(this).apply { | |
transitionName = "circle" | |
setBackgroundColor(color) | |
outlineProvider = object : ViewOutlineProvider() { | |
override fun getOutline(view: View, outline: Outline) { | |
outline.setOval(0, 0, view.measuredWidth, view.measuredHeight) | |
} | |
} | |
clipToOutline = true | |
} | |
return FrameLayout(this).apply { | |
addView(circle, size, size) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment