Skip to content

Yet another daynight switcher solution without recreating activity

License

Notifications You must be signed in to change notification settings

geminiwen/SkinSprite

Repository files navigation

Skin Sprite

Yet another solution for changing day-night mode without recreating activity

Inspired by appcompat-v7.

How it works

When we change the UiMode, let SkinnableActivity to notify every view in the hierarchy to call applyDayNight function if it implements the Skinnable interface.

We record the resource id in view constructor when inflating it, and get current resource after apply UiMode in applyDayNight function.

Preview

preview

Usage

Gradle:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    compile 'com.github.geminiwen:SkinSprite:v1.1'
}
  1. Define your Night Resource in drawable-night/values-night folder etc.
  2. Extends your activity with SkinnableActivity which extends AppCompatActivity
  3. add android:configChanges="uiMode" in your Activity Declare in AndroidManifest.xml
  4. Call setDayNightMode when you want change the DayNightMode
  5. Implement Skinnable interface when you create your own view

Sample

See in app folder.

TODO

Implement more components like AppCompat components.

中文说明

虽然好想说请忽略上面的语法错误 :P

Skin Sprite 是另一种不重启Activity 切换Android夜间模式的库, 只支持 Api 14以上

如何工作

我们把所有可以换肤的Activity都继承自SkinnableActivity, 每次需要换肤的时候就调用setDayNightMode函数, 它会去通知View层级中所有实现了Skinnable接口的对象. 调用他们的applyDayNight方法, 来切换他们的样式.

我们在View生成的时候, 记录下了它引用的一些资源id, 然后因为切换了UiMode后, 获取相同资源 id 得到的实质资源不一样的特性来完成夜间模式切换的方案.

如何使用

  1. 定义好所有夜间模式需要使用的资源和正常模式下使用的资源, 在res文件夹中分别使用drawable, drawable-night, values, values-night 等文件夹进行区分.
  2. 使你的Activity继承于 SkinnableActivity, 它本身是继承于AppCompatActivity的, 和appcompat-v7库深度整合.
  3. 记得在AndroidManifest.xml中的activity声明标签里加入 android:configChanges="uiMode", 这样才不会重启Activity
  4. 当你需要切换夜晚模式的时候, 调用setDayNightMode
  5. 如果你需要自定义View, 请继承Skinnable接口, 实现里面的方法, 具体可参照库内源码.

Demo

看本 repo 下面的app文件夹内的用法.

About

Yet another daynight switcher solution without recreating activity

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages