Giter Club home page Giter Club logo

skinsprite's Introduction

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文件夹内的用法.

skinsprite's People

Contributors

geminiwen avatar

Watchers

James Cloos avatar gaowei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.