├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_set_attr.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── iigo
│ │ │ └── daynightloadingview
│ │ │ ├── SetAttrsActivity.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── iigo
│ │ │ └── daynightloadingview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── iigo
│ │ └── daynightloadingview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ ├── strings.xml
│ │ │ └── attr.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── iigo
│ │ └── library
│ │ └── DayNightLoadingView.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── screenshots
├── screenshot1.gif
└── screenshot2.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── LICENSE
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------
/screenshots/screenshot1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/screenshots/screenshot1.gif
--------------------------------------------------------------------------------
/screenshots/screenshot2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/screenshots/screenshot2.gif
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DayNightLoadingView
3 |
4 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samlss/DayNightLoadingView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Jul 29 11:34:46 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | group='com.github.samlss'
3 | android {
4 | compileSdkVersion 27
5 |
6 | defaultConfig {
7 | minSdkVersion 19
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | }
12 | }
13 |
14 | dependencies {
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attr.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/iigo/daynightloadingview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.iigo.daynightloadingview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_set_attr.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
18 |
19 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/iigo/daynightloadingview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.iigo.daynightloadingview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.iigo.daynightloadingview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.iigo.daynightloadingview"
7 | minSdkVersion 19
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | implementation project(':library')
29 | }
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 samlss
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/iigo/daynightloadingview/SetAttrsActivity.java:
--------------------------------------------------------------------------------
1 | package com.iigo.daynightloadingview;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 |
7 | import com.iigo.library.DayNightLoadingView;
8 |
9 | /**
10 | * @author SamLeung
11 | * @e-mail 729717222@qq.com
12 | * @github https://github.com/samlss
13 | * @csdn https://blog.csdn.net/Samlss
14 | * @description Use the colors of the loading view.
15 | */
16 | public class SetAttrsActivity extends AppCompatActivity {
17 | private DayNightLoadingView dayNightLoadingView;
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_set_attr);
23 |
24 | dayNightLoadingView = findViewById(R.id.dn_loading);
25 | }
26 |
27 | @Override
28 | protected void onDestroy() {
29 | super.onDestroy();
30 | dayNightLoadingView.stop();
31 | }
32 |
33 | public void onResume(View view) {
34 | dayNightLoadingView.resume();
35 | }
36 |
37 | public void onPause(View view) {
38 | dayNightLoadingView.pause();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/iigo/daynightloadingview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.iigo.daynightloadingview;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.view.KeyEvent;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.iigo.library.DayNightLoadingView;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 | private DayNightLoadingView dayNightLoadingView;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_main);
19 |
20 | dayNightLoadingView = findViewById(R.id.dn_loading);
21 | }
22 |
23 | @Override
24 | protected void onDestroy() {
25 | super.onDestroy();
26 | dayNightLoadingView.stop();
27 | }
28 |
29 | public void onResume(View view) {
30 | dayNightLoadingView.resume();
31 | }
32 |
33 | public void onPause(View view) {
34 | dayNightLoadingView.pause();
35 | }
36 |
37 | public void onStartSetAttrs(View view) {
38 | startActivity(new Intent(this, SetAttrsActivity.class));
39 | }
40 |
41 | @Override
42 | public boolean onKeyDown(int keyCode, KeyEvent event) {
43 | //Manually change the size
44 | //手动改变大小
45 | ViewGroup.LayoutParams layoutParams = dayNightLoadingView.getLayoutParams();
46 | layoutParams.width = 100;
47 | layoutParams.height = 50;
48 | dayNightLoadingView.setLayoutParams(layoutParams);
49 | return super.onKeyDown(keyCode, event);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
12 |
13 |
19 |
20 |
27 |
28 |
35 |
36 |
37 |
38 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DayNightLoadingView
2 | A day night loading view(一个日夜切换的loading view).
3 |
4 |
5 | [](https://github.com/samlss/SignalLoadingView) [](https://github.com/samlss/DayNightLoadingView/blob/master/LICENSE) [](https://blog.csdn.net/Samlss)
6 |
7 |
8 |
9 | * [中文](#%E4%B8%AD%E6%96%87)
10 | * [English](#english)
11 | * [License](#license)
12 |
13 |
14 |
15 | 
16 |
17 |
18 | ---
19 |
20 | ## 中文
21 |
22 | ### 使用
23 | 在根目录的build.gradle添加这一句代码:
24 | ```
25 | allprojects {
26 | repositories {
27 | //...
28 | maven { url 'https://jitpack.io' }
29 | }
30 | }
31 | ```
32 |
33 | 在app目录下的build.gradle添加依赖使用:
34 | ```
35 | dependencies {
36 | implementation 'com.github.samlss:DayNightLoadingView:1.1'
37 | }
38 | ```
39 |
40 | 布局中使用:
41 | ```
42 |
51 | ```
52 |
53 |
54 |
55 | 代码中使用:
56 | ```
57 | dayNightLoadingView.resume(); //恢复动画
58 | dayNightLoadingView.pause(); //暂停动画
59 |
60 | dayNightLoadingView.stop(); //停止和释放动画,可在activity销毁时调用
61 | ```
62 |
63 |
64 |
65 | 属性说明:
66 |
67 | | 属性 | 说明 |
68 | | ------------- |:-------------:|
69 | | background_color | 背景颜色 |
70 | | sun_color | 太阳颜色 |
71 | | sunshine_color | 阳光颜色 |
72 | | moon_color | 月亮颜色 |
73 | | star_color | 星星颜色 |
74 |
75 |
76 |
77 | 由于使用了Animator和AnimaterSet的resume和pause方法,因此api要求为 >= 19,如果你想兼容更低的api,
78 | 方法有:
79 | - 不调用该库的resume和pause接口,最低可兼容到api12
80 | - 你自己修改代码
81 | - 联系我,等我有时间的时候尽量完善一下
82 |
83 |
84 |
85 | ---
86 |
87 | ## English
88 |
89 |
90 | ### Use
91 | Add it in your root build.gradle at the end of repositories:
92 | ```
93 | allprojects {
94 | repositories {
95 | //...
96 | maven { url 'https://jitpack.io' }
97 | }
98 | }
99 | ```
100 |
101 | Add it in your app build.gradle at the end of repositories:
102 | ```
103 | dependencies {
104 | implementation 'com.github.samlss:DayNightLoadingView:1.1'
105 | }
106 | ```
107 |
108 |
109 | in layout.xml:
110 | ```
111 |
120 | ```
121 |
122 |
123 |
124 | in java code:
125 | ```
126 | dayNightLoadingView.resume(); //resume animator
127 | dayNightLoadingView.pause(); //pause animator
128 |
129 | dayNightLoadingView.stop(); //Stop and release the animation, can be called when the activity is destroyed
130 | ```
131 |
132 |
133 |
134 | Attributes description:
135 |
136 | | attr | description |
137 | | ------------- |:-------------:|
138 | | background_color | the background color |
139 | | sun_color | the sun color |
140 | | sunshine_color | the sunshine color |
141 | | moon_color | the moon color |
142 | | star_color | the stars color |
143 |
144 |
145 |
146 | Due to use of the resume and pause methods of Animator and AnimalSet, the api requirement is >= 19,
147 | if you want to be compatible with lower apis,
148 | the methods are:
149 | - Do not call the resume and pause method of this library, the minimum compatible with api 12.
150 | - Modify the code by yourself.
151 | - Contact me, i will try to improve it when I have time.
152 |
153 | [id]: http://example.com/ "Optional Title Here"
154 |
155 | ---
156 |
157 | ## [LICENSE](https://github.com/samlss/DayNightLoadingView/blob/master/LICENSE)
158 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/library/src/main/java/com/iigo/library/DayNightLoadingView.java:
--------------------------------------------------------------------------------
1 | package com.iigo.library;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.AnimatorSet;
6 | import android.animation.ValueAnimator;
7 | import android.annotation.TargetApi;
8 | import android.content.Context;
9 | import android.content.res.TypedArray;
10 | import android.graphics.Canvas;
11 | import android.graphics.Color;
12 | import android.graphics.CornerPathEffect;
13 | import android.graphics.Matrix;
14 | import android.graphics.Paint;
15 | import android.graphics.Path;
16 | import android.graphics.PointF;
17 | import android.graphics.RectF;
18 | import android.os.Build;
19 | import android.util.AttributeSet;
20 | import android.util.Log;
21 | import android.view.View;
22 | import android.view.animation.AccelerateInterpolator;
23 | import android.view.animation.DecelerateInterpolator;
24 | import android.view.animation.LinearInterpolator;
25 |
26 | import java.io.File;
27 | import java.lang.reflect.Field;
28 | import java.lang.reflect.InvocationTargetException;
29 | import java.lang.reflect.Method;
30 | import java.util.ArrayList;
31 | import java.util.Collection;
32 | import java.util.List;
33 | import java.util.Random;
34 |
35 | /**
36 | * @author SamLeung
37 | * @e-mail 729717222@qq.com
38 | * @github https://github.com/samlss
39 | * @csdn https://blog.csdn.net/Samlss
40 | * @description The day and night loading view.
41 | */
42 | public class DayNightLoadingView extends View {
43 | //sun or moon state: raise
44 | private static final int RAISE = 0;
45 | //sun or moon state: fall
46 | private static final int FALL = 1;
47 |
48 | //alpha component [0..255]
49 | private static final int MIN_ALPHA_COMPONENT = 0;
50 | private static final int MAX_ALPHA_COMPONENT = 255;
51 |
52 | //the default background color
53 | private int DEFAULT_BG_COLOR = Color.parseColor("#062734");
54 | //the default color of sun , moon, stars
55 | private int DEFAULT_MAIN_COLOR = Color.parseColor("#00f99e");
56 |
57 | //the stars count
58 | private final static int STAR_COUNT = 20;
59 |
60 | // to record the end position of the stars
61 | private List starsEndPosPointList = new ArrayList(STAR_COUNT);
62 |
63 | //This defines the animators duration.
64 | private static final int MOON_ANIMATOR_DURATION = 2000; //2000ms
65 | private static final int SUN_ANIMATOR_DURATION = 2000; //2000ms
66 | private static final int STAR_ANIMATOR_DURATION = 2000; //2000ms
67 | private static final int STAR_SHOW_START_DELAY = 400; //400ms
68 | private static final int STAR_FLASH_ANIMATOR_DURATION = 400; //400ms
69 |
70 | private static final int SUNSHINE_ROTATE_ANIMATOR_DURATION = 3000; //3000ms
71 |
72 | //The ARC constant
73 | private static final double SUN_ARC_LENGTH = Math.PI * 2;
74 | private static final double SUN_ARC_LENGTH_OFFSET = Math.PI * 2 / 12d;
75 |
76 | //the start moving distance of sun or moon
77 | private float sunMoonStartMovingDistance;
78 |
79 | //The gap between the sun and the light
80 | private float sunshineGap;
81 |
82 | //The max length of the line of sunshine
83 | private float maxSunshineLineLength;
84 |
85 | //the path of stars, they have the same path
86 | private Path starPath;
87 |
88 | //the radius of sun and moon
89 | private float sunAndMoonRadius;
90 |
91 | //the computational star radius, the true star radius is 2 * starRadius
92 | private float starRadius;
93 | private int sunAndMoonStroke;
94 |
95 | //this defines the paints
96 | private Paint sunPaint;
97 | private Paint moonPaint;
98 | private Paint sunShinePaint;
99 | private Paint starPaint;
100 |
101 | //center position
102 | private float centerX;
103 | private float centerY;
104 |
105 | // Used to calculate the start and end points of each line of sunshine.
106 | private double sunshineStartX, sunshineStartY, sunshineStopX, sunshineStopY;
107 |
108 | //The animators
109 | // private ValueAnimator sunMoonAnimator;
110 | // private ValueAnimator starAnimator;
111 |
112 | private AnimatorActuator sunMoonAnimatorActuator;
113 | private AnimatorActuator starAnimatorActuator;
114 |
115 | //The attr's defines
116 | private int sunColor = DEFAULT_MAIN_COLOR;
117 | private int sunshineColor = DEFAULT_MAIN_COLOR;
118 | private int moonColor = DEFAULT_MAIN_COLOR;
119 | private int starColor = DEFAULT_MAIN_COLOR;
120 | private int bgColor = DEFAULT_BG_COLOR;
121 |
122 | public DayNightLoadingView(Context context) {
123 | super(context);
124 |
125 | init();
126 | }
127 |
128 | public DayNightLoadingView(Context context, AttributeSet attrs) {
129 | super(context, attrs);
130 |
131 | parseAttr(attrs);
132 | init();
133 | }
134 |
135 | public DayNightLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
136 | super(context, attrs, defStyleAttr);
137 |
138 | parseAttr(attrs);
139 | init();
140 | }
141 |
142 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
143 | public DayNightLoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
144 | super(context, attrs, defStyleAttr, defStyleRes);
145 |
146 | parseAttr(attrs);
147 | init();
148 | }
149 |
150 | private void parseAttr(AttributeSet attrs) {
151 | TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.DayNightLoadingView);
152 | sunColor = typedArray.getColor(R.styleable.DayNightLoadingView_sun_color, DEFAULT_MAIN_COLOR);
153 | sunshineColor = typedArray.getColor(R.styleable.DayNightLoadingView_sunshine_color, DEFAULT_MAIN_COLOR);
154 | moonColor = typedArray.getColor(R.styleable.DayNightLoadingView_moon_color, DEFAULT_MAIN_COLOR);
155 | starColor = typedArray.getColor(R.styleable.DayNightLoadingView_star_color, DEFAULT_MAIN_COLOR);
156 | bgColor = typedArray.getColor(R.styleable.DayNightLoadingView_background_color, DEFAULT_BG_COLOR);
157 | typedArray.recycle();
158 | }
159 |
160 | private void init(){
161 | sunPaint = new Paint();
162 | sunPaint.setColor(sunColor);
163 | sunPaint.setAntiAlias(true);
164 | sunPaint.setStyle(Paint.Style.STROKE);
165 |
166 | moonPaint = new Paint();
167 | moonPaint.setColor(moonColor);
168 | moonPaint.setAntiAlias(true);
169 | moonPaint.setStyle(Paint.Style.STROKE);
170 |
171 | sunShinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
172 | sunShinePaint.setStyle(Paint.Style.STROKE);
173 | sunShinePaint.setStrokeCap(Paint.Cap.ROUND);
174 | sunShinePaint.setStrokeJoin(Paint.Join.ROUND);
175 | sunShinePaint.setColor(sunshineColor);
176 |
177 | starPaint = new Paint();
178 | starPaint.setColor(starColor);
179 | starPaint.setAntiAlias(true);
180 | starPaint.setStyle(Paint.Style.FILL);
181 | starPaint.setPathEffect(new CornerPathEffect(8));
182 |
183 | starPath = new Path();
184 | }
185 |
186 | @Override
187 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
188 | super.onSizeChanged(w, h, oldw, oldh);
189 | stop();
190 |
191 | int minSize = Math.min(w, h);
192 |
193 | sunAndMoonRadius = minSize / 12f;
194 | sunAndMoonStroke = (int) (sunAndMoonRadius / 10f);
195 | sunAndMoonRadius -= sunAndMoonStroke * 2;
196 |
197 | starRadius = sunAndMoonRadius / 5f;
198 | initStarPath();
199 |
200 | sunshineGap = sunAndMoonRadius / 3f;
201 | maxSunshineLineLength = sunAndMoonRadius / 2.5f;
202 |
203 | sunMoonStartMovingDistance = sunAndMoonRadius + sunAndMoonStroke + 10;
204 |
205 | sunPaint.setStrokeWidth(sunAndMoonStroke);
206 | moonPaint.setStrokeWidth(sunAndMoonStroke);
207 | sunShinePaint.setStrokeWidth(sunAndMoonStroke);
208 |
209 | centerX = w / 2;
210 | centerY = h / 2;
211 | }
212 |
213 | @Override
214 | protected void onDraw(Canvas canvas) {
215 | super.onDraw(canvas);
216 | canvas.drawColor(bgColor);
217 |
218 | if(sunMoonAnimatorActuator == null){
219 | sunMoonAnimatorActuator = new SunTranslateAnimatorActuator(RAISE, 0);
220 | }
221 |
222 | //draw stars firstly.
223 | if (starAnimatorActuator != null){
224 | starAnimatorActuator.draw(canvas);
225 | }
226 |
227 | sunMoonAnimatorActuator.draw(canvas);
228 | }
229 |
230 | private void drawSun(Canvas canvas, float y){
231 | canvas.drawCircle(centerX, y, sunAndMoonRadius, sunPaint);
232 | }
233 |
234 | private void drawSunshine(Canvas canvas, float startLength, float stopLength, float currentRotation, float y){
235 | for (float a = 0; a <= SUN_ARC_LENGTH; a += SUN_ARC_LENGTH_OFFSET) {
236 | sunshineStartX = startLength * Math.cos(a+currentRotation) + centerX;
237 | sunshineStartY = startLength * Math.sin(a+currentRotation) + y;
238 |
239 | sunshineStopX = stopLength * Math.cos(a+currentRotation) + centerX;
240 | sunshineStopY = stopLength * Math.sin(a+currentRotation) + y;
241 |
242 | canvas.drawLine((float) sunshineStartX, (float) sunshineStartY, (float) sunshineStopX, (float) sunshineStopY, sunShinePaint);
243 | }
244 | }
245 |
246 | private class SunTranslateAnimatorActuator extends AnimatorActuator{
247 | private float animatedValue;
248 | private float currentRotation;
249 |
250 | public SunTranslateAnimatorActuator(final int action, float currentRotation) {
251 | this.currentRotation = currentRotation;
252 |
253 | releaseSunMoonAnimator();
254 |
255 | valueAnimator = ValueAnimator.ofFloat(action == RAISE ? 0f : 1f,
256 | action == RAISE ? 1f : 0f);
257 |
258 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
259 | @Override
260 | public void onAnimationUpdate(ValueAnimator valueAnimator) {
261 | animatedValue = (float) valueAnimator.getAnimatedValue();
262 | invalidate();
263 | }
264 | });
265 |
266 | valueAnimator.addListener(new AnimatorListenerAdapter() {
267 | @Override
268 | public void onAnimationEnd(Animator animation) {
269 | if (valueAnimator != null && valueAnimator.getAnimatedFraction() < 1){
270 | return;
271 | }
272 |
273 | if (action == RAISE) {
274 | sunMoonAnimatorActuator = new SunshineRotationAnimatorActuator();
275 | }else{
276 | sunMoonAnimatorActuator = new MoonAnimatorActuator(RAISE);
277 | }
278 | }
279 | });
280 |
281 | if (action == RAISE) {
282 | valueAnimator.setInterpolator(new DecelerateInterpolator());
283 | }else{
284 | valueAnimator.setInterpolator(new AccelerateInterpolator());
285 | }
286 |
287 | valueAnimator.setDuration(SUN_ANIMATOR_DURATION);
288 | valueAnimator.start();
289 | }
290 |
291 | @Override
292 | public void performDraw(Canvas canvas) {
293 | float y = (centerY * 2 + sunMoonStartMovingDistance) - animatedValue * (centerY + sunMoonStartMovingDistance);
294 |
295 | drawSun(canvas, y);
296 |
297 | float startLength = sunshineGap + sunAndMoonRadius;
298 | float stopLength = sunshineGap + animatedValue * maxSunshineLineLength + sunAndMoonRadius;
299 |
300 | drawSunshine(canvas, startLength, stopLength, currentRotation, y);
301 | }
302 | }
303 |
304 | private class SunshineRotationAnimatorActuator extends AnimatorActuator{
305 | private float animatedValue;
306 |
307 | public SunshineRotationAnimatorActuator() {
308 | releaseSunMoonAnimator();
309 |
310 | valueAnimator = ValueAnimator.ofFloat(0, (float) SUN_ARC_LENGTH);
311 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
312 | @Override
313 | public void onAnimationUpdate(ValueAnimator valueAnimator) {
314 | animatedValue = (float) valueAnimator.getAnimatedValue();
315 | if (valueAnimator.getAnimatedFraction() >= 0.6f){
316 | sunMoonAnimatorActuator = new SunTranslateAnimatorActuator(FALL, animatedValue);
317 | starAnimatorActuator = new StarTranslateAnimatorActuator(RAISE);
318 | }else{
319 | invalidate();
320 | }
321 | }
322 | });
323 |
324 | valueAnimator.setInterpolator(new LinearInterpolator());
325 | valueAnimator.setDuration(SUNSHINE_ROTATE_ANIMATOR_DURATION);
326 | valueAnimator.start();
327 | }
328 |
329 |
330 | @Override
331 | public void performDraw(Canvas canvas) {
332 | drawSun(canvas,centerY);
333 |
334 | float startLength = sunshineGap + sunAndMoonRadius;
335 | float stopLength = startLength + maxSunshineLineLength;
336 |
337 | drawSunshine(canvas, startLength, stopLength, animatedValue, centerY);
338 | }
339 | }
340 |
341 | private class MoonAnimatorActuator extends AnimatorActuator{
342 | private float animatedFraction;
343 | private float animatedValue;
344 | private Path moonPath = new Path();
345 | private Matrix moonMatrix = new Matrix();
346 | private float currentRotation = 0;
347 | private int action;
348 | private RectF rectFOuter = new RectF();
349 | private RectF rectFInner = new RectF();
350 |
351 | public MoonAnimatorActuator(final int action) {
352 | this.action = action;
353 | createAnimator();
354 | }
355 |
356 | private void createAnimator(){
357 | releaseSunMoonAnimator();
358 |
359 | valueAnimator = ValueAnimator.ofFloat(action == RAISE ? 0f : 1f,
360 | action == RAISE ? 1f : 0f);
361 |
362 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
363 | @Override
364 | public void onAnimationUpdate(ValueAnimator valueAnimator) {
365 | animatedValue = (float) valueAnimator.getAnimatedValue();
366 | animatedFraction = valueAnimator.getAnimatedFraction();
367 | invalidate();
368 | }
369 | });
370 |
371 | valueAnimator.addListener(new AnimatorListenerAdapter() {
372 | @Override
373 | public void onAnimationEnd(Animator animation) {
374 | if (valueAnimator != null && valueAnimator.getAnimatedFraction() < 1){
375 | return;
376 | }
377 |
378 | if (action == RAISE) {
379 | action = FALL;
380 | animatedFraction = 0;
381 | createAnimator();
382 | starAnimatorActuator = new StarTranslateAnimatorActuator(FALL);
383 | }else{
384 | releaseStarAnimator();
385 | sunMoonAnimatorActuator = new SunTranslateAnimatorActuator(RAISE, 0);
386 | }
387 | }
388 | });
389 |
390 | if (action == RAISE) {
391 | valueAnimator.setInterpolator(new DecelerateInterpolator());
392 | }else{
393 | valueAnimator.setInterpolator(new AccelerateInterpolator());
394 | }
395 | valueAnimator.setDuration(MOON_ANIMATOR_DURATION);
396 | valueAnimator.start();
397 | }
398 |
399 | @Override
400 | public void performDraw(Canvas canvas) {
401 | moonPath.reset();
402 | moonMatrix.reset();
403 |
404 | float y = centerY * 2 + sunAndMoonRadius + sunAndMoonStroke + 10 - animatedValue * (centerY + sunAndMoonRadius + sunAndMoonStroke + 10);
405 |
406 | rectFOuter.set(centerX - sunAndMoonRadius, y - sunAndMoonRadius, centerX + sunAndMoonRadius, y + sunAndMoonRadius);
407 | rectFInner.set(centerX - sunAndMoonRadius / 3, y - sunAndMoonRadius, centerX + sunAndMoonRadius / 3, y + sunAndMoonRadius);
408 |
409 | moonPath.addArc(rectFInner, -90, 180);
410 | moonPath.addArc(rectFOuter, -90, 180);
411 |
412 | if (action == RAISE && animatedFraction > 0.8f) {
413 | currentRotation+=0.6f;
414 | }
415 |
416 | if (action == FALL && animatedFraction < 0.2f){
417 | currentRotation += 0.35f;
418 | }
419 |
420 | moonMatrix.postRotate(currentRotation, centerX, y);
421 | moonPath.transform(moonMatrix);
422 |
423 | canvas.drawPath(moonPath, moonPaint);
424 | }
425 | }
426 |
427 | private void initStarPath(){
428 | starPath.reset();
429 | List starPoints = new ArrayList<>();
430 |
431 | float R = starRadius * 2;
432 | float r = starRadius;
433 |
434 | for (int i = 0; i < 5; i++){
435 | //outer
436 | double outerDoc = (18 + 72 * i) / 180d * Math.PI;
437 | PointF pointF1 = new PointF((float) (Math.cos(outerDoc) * R) ,
438 | - (float) (Math.sin(outerDoc) * R));
439 |
440 | //inner
441 | double innerDoc = (54 + 72 * i) / 180d * Math.PI;
442 | PointF pointF2 = new PointF((float)(Math.cos(innerDoc) * r) ,
443 | -(float) (Math.sin(innerDoc) * r));
444 |
445 | starPoints.add(pointF1);
446 | starPoints.add(pointF2);
447 | }
448 |
449 | starPath.moveTo(starPoints.get(0).x, starPoints.get(0).y);
450 | starPath.lineTo(starPoints.get(1).x, starPoints.get(1).y);
451 | starPath.lineTo(starPoints.get(2).x, starPoints.get(2).y);
452 | starPath.lineTo(starPoints.get(3).x, starPoints.get(4).y);
453 | starPath.lineTo(starPoints.get(4).x, starPoints.get(4).y);
454 | starPath.lineTo(starPoints.get(5).x, starPoints.get(5).y);
455 | starPath.lineTo(starPoints.get(6).x, starPoints.get(6).y);
456 | starPath.lineTo(starPoints.get(7).x, starPoints.get(7).y);
457 | starPath.lineTo(starPoints.get(8).x, starPoints.get(8).y);
458 | starPath.lineTo(starPoints.get(9).x, starPoints.get(9).y);
459 | starPath.lineTo(starPoints.get(0).x, starPoints.get(0).y);
460 |
461 | Matrix matrix = new Matrix();
462 | matrix.postTranslate(0, -R);
463 | starPath.transform(matrix);
464 | }
465 |
466 | private class StarTranslateAnimatorActuator extends AnimatorActuator{
467 | private int action;
468 |
469 | private float maxY = 0;
470 | private float[] starsTranslateArr = new float[20];
471 |
472 | private AnimatorSet animatorSet = new AnimatorSet();
473 |
474 | public StarTranslateAnimatorActuator(int action){
475 | this.action = action;
476 | releaseStarAnimator();
477 |
478 | if (action == RAISE){
479 | initStars();
480 | }
481 |
482 | setupAnimator();
483 | }
484 |
485 | private void setupAnimator(){
486 | List valueAnimators = new ArrayList<>();
487 |
488 | for (int i = 0; i < STAR_COUNT; i++){
489 | ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
490 | if (action == RAISE) {
491 | long duration = (long) (STAR_ANIMATOR_DURATION * starsEndPosPointList.get(i).y / maxY);
492 | if (duration < STAR_ANIMATOR_DURATION - 500){
493 | valueAnimator.setStartDelay(getRandom(100, 500));
494 | }
495 | valueAnimator.setDuration(duration);
496 | }else{
497 | valueAnimator.setDuration(getRandom(STAR_ANIMATOR_DURATION - 500, STAR_ANIMATOR_DURATION));
498 | }
499 |
500 | final int finalI = i;
501 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
502 | @Override
503 | public void onAnimationUpdate(ValueAnimator animation) {
504 | float value = (float) animation.getAnimatedValue();
505 | if (action == RAISE) {
506 | starsTranslateArr[finalI] = value * starsEndPosPointList.get(finalI).y;
507 | }else{
508 | starsTranslateArr[finalI] = starsEndPosPointList.get(finalI).y + value * (getHeight() - starsEndPosPointList.get(finalI).y + sunMoonStartMovingDistance * 2);
509 | }
510 | invalidate();
511 | }
512 | });
513 |
514 | valueAnimators.add(valueAnimator);
515 | }
516 |
517 | if (action == RAISE){
518 | animatorSet.addListener(new AnimatorListenerAdapter() {
519 | @Override
520 | public void onAnimationEnd(Animator animation) {
521 | starAnimatorActuator = new StarFlashAnimatorActuator();
522 | }
523 | });
524 |
525 | animatorSet.setStartDelay(STAR_SHOW_START_DELAY);
526 | animatorSet.setInterpolator(new DecelerateInterpolator());
527 | }else{
528 | animatorSet.setInterpolator(new AccelerateInterpolator());
529 | }
530 |
531 |
532 | animatorSet.playTogether(valueAnimators);
533 | animatorSet.start();
534 | }
535 |
536 | private void initStars(){
537 | starsEndPosPointList.clear();
538 | float averageWidth = getMeasuredWidth() * 1.0f / STAR_COUNT;
539 | float firstX = averageWidth / 2;
540 |
541 | for (int i = 0; i < STAR_COUNT; i++){
542 | float x = firstX + i * averageWidth;
543 |
544 | PointF endPos;
545 | if (isInMoonRect(x)){
546 | int startY = (int) starRadius * 2 * 2;
547 | int endY = (int) (centerY - (sunAndMoonRadius + sunAndMoonRadius));
548 |
549 | endPos = new PointF(x, getRandom(startY, endY));
550 | }else{
551 | endPos = new PointF(x, getRandom(getHeight() / 4, getHeight() * 3 / 4));
552 | }
553 |
554 | if (endPos.y > maxY){
555 | maxY = endPos.y;
556 | }
557 |
558 | starsEndPosPointList.add(endPos);
559 | }
560 | }
561 |
562 | private int getRandom(int start, int end){
563 | if (start >= end){
564 | return 0;
565 | }
566 | return start + new Random().nextInt(end - start);
567 | }
568 |
569 | private boolean isInMoonRect(float x){
570 | return x >= (centerX - (sunAndMoonRadius + sunAndMoonRadius))
571 | && x <= (centerX + sunAndMoonRadius + sunAndMoonRadius);
572 | }
573 |
574 | @Override
575 | public void performDraw(Canvas canvas) {
576 | for (int i = 0; i < STAR_COUNT; i++) {
577 | canvas.save();
578 | starPaint.setAlpha(MAX_ALPHA_COMPONENT);
579 |
580 | canvas.translate(starsEndPosPointList.get(i).x, starsTranslateArr[i]);
581 |
582 | canvas.drawPath(starPath, starPaint);
583 | canvas.restore();
584 | }
585 | }
586 |
587 | @Override
588 | public void resume() {
589 | if (animatorSet != null){
590 | animatorSet.resume();
591 | }
592 | }
593 |
594 | @Override
595 | public void pause() {
596 | if (animatorSet != null){
597 | animatorSet.pause();
598 | }
599 | }
600 |
601 |
602 | @Override
603 | public void release() {
604 | if (animatorSet != null) {
605 | animatorSet.removeAllListeners();
606 | animatorSet.cancel();
607 | animatorSet = null;
608 | }
609 | }
610 | }
611 |
612 | private class StarFlashAnimatorActuator extends AnimatorActuator{
613 | private int animatedValue;
614 | private int index;
615 |
616 | public StarFlashAnimatorActuator(){
617 | index = getRandomIndex();
618 |
619 | setupAnimator();
620 | }
621 |
622 | private int getRandomIndex(){
623 | return new Random().nextInt(STAR_COUNT);
624 | }
625 |
626 | private void setupAnimator(){
627 | releaseStarAnimator();
628 |
629 | valueAnimator = ValueAnimator.ofInt(MAX_ALPHA_COMPONENT, MIN_ALPHA_COMPONENT);
630 |
631 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
632 | @Override
633 | public void onAnimationUpdate(ValueAnimator valueAnimator) {
634 | animatedValue = (int) valueAnimator.getAnimatedValue();
635 | invalidate();
636 | }
637 | });
638 |
639 | valueAnimator.addListener(new AnimatorListenerAdapter() {
640 | @Override
641 | public void onAnimationRepeat(Animator animation) {
642 | int curIndex = getRandomIndex();
643 | if (curIndex == index){
644 | if (index == STAR_COUNT - 1){
645 | index--;
646 | }else{
647 | index++;
648 | }
649 | }else{
650 | index = curIndex;
651 | }
652 | }
653 | });
654 |
655 | valueAnimator.setDuration(STAR_FLASH_ANIMATOR_DURATION);
656 | valueAnimator.setInterpolator(new LinearInterpolator());
657 | valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
658 | valueAnimator.start();
659 | }
660 |
661 | @Override
662 | public void performDraw(Canvas canvas) {
663 | for (int i = 0; i < STAR_COUNT; i++){
664 | canvas.save();
665 | canvas.translate(starsEndPosPointList.get(i).x,starsEndPosPointList.get(i).y);
666 |
667 |
668 | if (i == index){
669 | starPaint.setAlpha(animatedValue);
670 | }else{
671 | starPaint.setAlpha(255);
672 | }
673 |
674 | canvas.drawPath(starPath, starPaint);
675 | canvas.restore();
676 | }
677 | }
678 | }
679 |
680 | private abstract class AnimatorActuator{
681 | public ValueAnimator valueAnimator;
682 |
683 | public void draw(Canvas canvas){
684 | //you can do check here.
685 | performDraw(canvas);
686 | }
687 |
688 | public abstract void performDraw(Canvas canvas);
689 |
690 | public void pause(){
691 | if (valueAnimator != null) {
692 | valueAnimator.pause();
693 | }
694 | }
695 |
696 | public void resume(){
697 | if (valueAnimator != null) {
698 | valueAnimator.resume();
699 | }
700 | }
701 |
702 | public void release(){
703 | if (valueAnimator != null) {
704 | valueAnimator.removeAllUpdateListeners();
705 | valueAnimator.removeAllListeners();
706 | valueAnimator.cancel();
707 | valueAnimator = null;
708 | }
709 | }
710 | }
711 |
712 | private void releaseSunMoonAnimator(){
713 | if (sunMoonAnimatorActuator != null) {
714 | sunMoonAnimatorActuator.release();
715 | }
716 | }
717 |
718 | private void releaseStarAnimator(){
719 | if (starAnimatorActuator != null){
720 | starAnimatorActuator.release();
721 | }
722 | }
723 |
724 | //pause the animators
725 | public void pause(){
726 | if (sunMoonAnimatorActuator != null) {
727 | sunMoonAnimatorActuator.pause();
728 | }
729 |
730 | if (starAnimatorActuator != null) {
731 | starAnimatorActuator.pause();
732 | }
733 | }
734 |
735 | //resume the animators
736 | public void resume(){
737 | if (sunMoonAnimatorActuator != null) {
738 | sunMoonAnimatorActuator.resume();
739 | }
740 |
741 | if (starAnimatorActuator != null) {
742 | starAnimatorActuator.resume();
743 | }
744 | }
745 |
746 | //Stop and release.
747 | public void stop(){
748 | starsEndPosPointList.clear();
749 | releaseSunMoonAnimator();
750 | sunMoonAnimatorActuator = null;
751 |
752 |
753 | releaseStarAnimator();
754 | starAnimatorActuator = null;
755 | }
756 | }
757 |
758 |
--------------------------------------------------------------------------------