中文 | English
Just one line of code to achieve the Circular Reveal Theme Switch Animation in Telegram
Add JitPack repository to your settings.gradle.kts
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url = uri("https://jitpack.io")
// groovy:
// url 'https://jitpack.io'
}
}
}
Add dependency in your module's build.gradle.kts
dependencies {
implementation("com.github.YenalyLiew:CircularRevealSwitch:0.4.6")
}
Day to Night Mode | Night to Day Mode | Switch to Yellow Theme | Switch to Red Theme |
---|---|---|---|
Minimum Android API level required is 24 (Android 7.0).
CircularRevealSwitch depends on DecorView recycling, which is supported in Android 7.0 and above.
- No need to nest or add new Views manually
- Provides certain extensibility, with methods being
protected open
- Achieve the simplest Day/Night mode switch animation with just one line of code
view.setDayNightModeSwitcher(
duration = 400L,
interpolator = FastOutSlowInInterpolator(),
animToDayMode = SwitchAnimation.SHRINK,
animToNightMode = SwitchAnimation.EXPAND,
onNightModeAnimStart = { /* Callback for Night mode animation start */ },
onNightModeAnimEnd = { /* Callback for Night mode animation end */ },
onDayModeAnimStart = { /* Callback for Day mode animation start */ },
onDayModeAnimEnd = { /* Callback for Day mode animation end */ },
onClick = { /* Click event */ },
)
DayNightModeCRSwitch builder = new DayNightModeCRSwitch.Builder(view)
.duration(400L)
.interpolator(new FastOutSlowInInterpolator())
.animToDayMode(SwitchAnimation.SHRINK)
.animToNightMode(SwitchAnimation.EXPAND)
.onNightModeAnimStart(() -> { /* Callback for Night mode animation start */ })
.onNightModeAnimEnd(() -> { /* Callback for Night mode animation end */ })
.onDayModeAnimStart(() -> { /* Callback for Day mode animation start */ })
.onDayModeAnimEnd(() -> { /* Callback for Day mode animation end */ })
.onClickListener((view) -> { /* Click event */ })
.build();
builder.setSwitcher();
⚠ Call the
setTheme
method beforesuper.onCreate(savedInstanceState)
to load the theme you want to switch to. If you just want the animation, you can directly callThemeCRSwitch.setTheme(int)
.⚠
ThemeCRSwitch.setTheme(int)
is just a self-satisfying method and not recommended. If you need to save the theme state to apply it next time you start the app or open a new Activity, please store and set it yourself using SharedPreferences or other libraries.🌰 Example:
override fun onCreate(savedInstanceState: Bundle?) { when (sharedPrefs().getString("theme", "default")) { "red" -> setTheme(R.style.Theme_CircularRevealSwitch_Red) "green" -> setTheme(R.style.Theme_CircularRevealSwitch_Green) "blue" -> setTheme(R.style.Theme_CircularRevealSwitch_Blue) "yellow" -> setTheme(R.style.Theme_CircularRevealSwitch_Yellow) else -> setTheme(R.style.Theme_CircularRevealSwitch) } super.onCreate(savedInstanceState) // view.setThemeSwitcher... }
view.setThemeSwitcher(
toTheme = R.style.new_theme,
duration = 400L,
interpolator = FastOutSlowInInterpolator(),
animToTheme = SwitchAnimation.EXPAND,
onAnimStart = { /* Callback for theme switch animation start */ },
onAnimEnd = { /* Callback for theme switch animation end */ },
onClick = { /* Click event */ },
)
ThemeCRSwitch builder = new ThemeCRSwitch.Builder(view, R.style.new_theme)
.duration(400L)
.interpolator(new FastOutSlowInInterpolator())
.animToTheme(SwitchAnimation.EXPAND)
.onAnimStart(() -> { /* Callback for theme switch animation start */ })
.onAnimEnd(() -> { /* Callback for theme switch animation end */ })
.onClickListener((view) -> { /* Click event */ })
.build();
builder.setSwitcher();
- This library heavily relies on
ActivityCompat#recreate
method. If your Activity changes its Views upon recreation, it might affect the user experience. - Avoid performing actions related to the current Activity in
onAnimStart
andonAnimEnd
callbacks because at that point, the old Activity is already destroyed, and any operations on components might be ineffective. However, you can execute some application-level tasks here. If you need to perform other tasks before the old Activity is destroyed, you can do so inonClick
. - ComponentActivity or higher is recommended.
API level 28 or higher is recommended due to reliance on(Resolved inHandler#post
inActivityCompat#recreate
. In versions below API 28, the order of posting to the main message queue might differ, potentially causing animation issues, though it's rare.v0.3
)- The
setSwitcher()
method overrides the originalonTouch()
andonClick()
methods. If you need click events, you can call them from the constructor or inherit from the CircularRevealSwitch class and override the respective methods. - This library only provides theme switching effects and does not handle theme persistence. If you need to save the theme for the next launch or opening of a new Activity, handle it yourself using SharedPreferences or other libraries.
-
Using PixelCopyCompat allows for the use of advanced PixelCopy when the API is less than 26. Although reflection calls are used, the overall speed is still much faster than
View#getDrawingCache()
.The top part of the image shows the performance of PixelCopy at API 24 (average time consumption around 40ms), while the bottom part shows the performance of
View#getDrawingCache()
(average time consumption around 60-70ms). -
The implementation of the
takeScreenshot()
method has been changed fromView#getDrawingCache()
toView#drawToBitmap()
, which is more than twice as fast (average time consumption around 30ms).
- Fixed the issue where the day-night switch animation cannot play normally under multiple Activities
-
Implemented PixelCopy for screen capture on API level 26 and above, preserving effects like shadows.
Removed
screenshot()
method, replaced withtakeScreenshotCompat()
. -
Fixed the issue where
isViewClickable
logic wasn't effective.
- Fixed #1: Some devices failing to start animations immediately due to delayed view attachment.
- Fixed animations not displaying on devices with API level less than 28.