├── .gitattributes ├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── LoadManagerLib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhangyf │ │ └── loadmanagerlib │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zhangyf │ │ │ └── loadmanagerlib │ │ │ ├── OnPrePageListener.java │ │ │ ├── PreLayout.java │ │ │ └── PrePageManager.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── zhangyf │ └── loadmanagerlib │ └── ExampleUnitTest.java ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhangyf │ │ └── loadmanager │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zhangyf │ │ │ └── loadmanager │ │ │ ├── AnimActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── NormalFragment.java │ │ │ ├── base │ │ │ ├── BaseActivity.java │ │ │ ├── BaseLazyFragment.java │ │ │ └── MApplication.java │ │ │ ├── listener │ │ │ └── DefaultLoadListener.java │ │ │ └── widget │ │ │ └── loadingdrawable │ │ │ ├── DensityUtil.java │ │ │ ├── ElectricFanLoadingRenderer.java │ │ │ ├── LoadingDrawable.java │ │ │ ├── LoadingRenderer.java │ │ │ ├── LoadingRendererFactory.java │ │ │ └── LoadingView.java │ └── res │ │ ├── layout │ │ ├── activity_anim.xml │ │ ├── activity_main.xml │ │ ├── empty_view.xml │ │ ├── fragment_anim.xml │ │ ├── layout_load_view.xml │ │ └── layout_reload_view.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ ├── ic_eletric_fan.png │ │ ├── ic_launcher.png │ │ ├── ic_leaf.png │ │ └── ic_loading.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zhangyf │ └── loadmanager │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots └── video1.gif └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /LoadManagerLib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /LoadManagerLib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.Yuphee' 5 | 6 | android { 7 | compileSdkVersion 25 8 | buildToolsVersion "25.0.1" 9 | 10 | defaultConfig { 11 | minSdkVersion 9 12 | targetSdkVersion 25 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 | 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 30 | exclude group: 'com.android.support', module: 'support-annotations' 31 | }) 32 | compile 'com.android.support:appcompat-v7:25.0.+' 33 | testCompile 'junit:junit:4.12' 34 | } 35 | 36 | // 打包源码jar 37 | task sourcesJar(type: Jar) { 38 | from android.sourceSets.main.java.srcDirs 39 | classifier = 'sources' 40 | } 41 | 42 | task javadoc(type: Javadoc) { 43 | failOnError false 44 | source = android.sourceSets.main.java.sourceFiles 45 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 46 | classpath += configurations.compile 47 | } 48 | 49 | // 打包文档jar 50 | task javadocJar(type: Jar, dependsOn: javadoc) { 51 | classifier = 'javadoc' 52 | from javadoc.destinationDir 53 | } 54 | 55 | artifacts { 56 | archives sourcesJar 57 | archives javadocJar 58 | } 59 | -------------------------------------------------------------------------------- /LoadManagerLib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\AndroidSdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /LoadManagerLib/src/androidTest/java/com/zhangyf/loadmanagerlib/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanagerlib; 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 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zhangyf.loadmanagerlib.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LoadManagerLib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LoadManagerLib/src/main/java/com/zhangyf/loadmanagerlib/OnPrePageListener.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanagerlib; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * Created by zhangyf on 16/12/20. 7 | */ 8 | public abstract class OnPrePageListener { 9 | public abstract void onRetryStart(View retryView); 10 | 11 | public void onLoadingStart(View loadingView) { 12 | } 13 | 14 | public void onLoadingEnd(View loadingView) { 15 | 16 | } 17 | 18 | public void onEmptyStart(View emptyView) { 19 | } 20 | 21 | public void onContentStart(View allView) { 22 | 23 | } 24 | 25 | public int generateLoadingLayoutId() { 26 | return PrePageManager.NO_LAYOUT_ID; 27 | } 28 | 29 | public int generateRetryLayoutId() { 30 | return PrePageManager.NO_LAYOUT_ID; 31 | } 32 | 33 | public int generateEmptyLayoutId() { 34 | return PrePageManager.NO_LAYOUT_ID; 35 | } 36 | 37 | public View generateLoadingLayout() { 38 | return null; 39 | } 40 | 41 | public View generateRetryLayout() { 42 | return null; 43 | } 44 | 45 | public View generateEmptyLayout() { 46 | return null; 47 | } 48 | 49 | public boolean isSetLoadingLayout() { 50 | if (generateLoadingLayoutId() != PrePageManager.NO_LAYOUT_ID || generateLoadingLayout() != null) 51 | return true; 52 | return false; 53 | } 54 | 55 | public boolean isSetRetryLayout() { 56 | if (generateRetryLayoutId() != PrePageManager.NO_LAYOUT_ID || generateRetryLayout() != null) 57 | return true; 58 | return false; 59 | } 60 | 61 | public boolean isSetEmptyLayout() { 62 | if (generateEmptyLayoutId() != PrePageManager.NO_LAYOUT_ID || generateEmptyLayout() != null) 63 | return true; 64 | return false; 65 | } 66 | 67 | 68 | } -------------------------------------------------------------------------------- /LoadManagerLib/src/main/java/com/zhangyf/loadmanagerlib/PreLayout.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanagerlib; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.AnimatorSet; 6 | import android.content.Context; 7 | import android.os.Looper; 8 | import android.util.AttributeSet; 9 | import android.util.Log; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.widget.FrameLayout; 13 | 14 | /** 15 | * Created by zhangyf on 16/12/20 16 | */ 17 | public class PreLayout extends FrameLayout { 18 | private View mLoadingView; 19 | private View mRetryView; 20 | private View mContentView; 21 | private View mEmptyView; 22 | private LayoutInflater mInflater; 23 | public static final long DEFAULT_DURATION = 500; 24 | 25 | private static final String TAG = PreLayout.class.getSimpleName(); 26 | 27 | 28 | public PreLayout(Context context, AttributeSet attrs, int defStyleAttr) { 29 | super(context, attrs, defStyleAttr); 30 | mInflater = LayoutInflater.from(context); 31 | } 32 | 33 | 34 | public PreLayout(Context context, AttributeSet attrs) { 35 | this(context, attrs, -1); 36 | } 37 | 38 | public PreLayout(Context context) { 39 | this(context, null); 40 | } 41 | 42 | private boolean isMainThread() { 43 | return Looper.myLooper() == Looper.getMainLooper(); 44 | } 45 | 46 | public void showLoading() { 47 | if (isMainThread()) { 48 | showView(mLoadingView); 49 | } else { 50 | post(new Runnable() { 51 | @Override 52 | public void run() { 53 | showView(mLoadingView); 54 | } 55 | }); 56 | } 57 | } 58 | 59 | @Deprecated 60 | public void showRetry() { 61 | showRetry(null,DEFAULT_DURATION); 62 | } 63 | 64 | public void showRetry(final AnimatorsListener anim, final long duration) { 65 | if (isMainThread()) { 66 | showView(mRetryView,anim,duration); 67 | } else { 68 | post(new Runnable() { 69 | @Override 70 | public void run() { 71 | showView(mRetryView,anim,duration); 72 | } 73 | }); 74 | } 75 | 76 | } 77 | 78 | @Deprecated 79 | public void showContent() { 80 | showContent(null,DEFAULT_DURATION); 81 | } 82 | 83 | public void showContent(final AnimatorsListener anim, final long duration) { 84 | if (isMainThread()) { 85 | showView(mContentView,anim,duration); 86 | } else { 87 | post(new Runnable() { 88 | @Override 89 | public void run() { 90 | showView(mContentView,anim,duration); 91 | } 92 | }); 93 | } 94 | } 95 | 96 | 97 | public void showEmpty() { 98 | if (isMainThread()) { 99 | showView(mEmptyView); 100 | } else { 101 | post(new Runnable() { 102 | @Override 103 | public void run() { 104 | showView(mEmptyView); 105 | } 106 | }); 107 | } 108 | } 109 | 110 | 111 | private void showView(View view) { 112 | showView(view, null, DEFAULT_DURATION); 113 | } 114 | 115 | private void showView(final View view, AnimatorsListener mListener, long duration) { 116 | if (view == null) { 117 | return; 118 | } 119 | if (mListener != null) { 120 | if(duration <= 0) { 121 | throw new IllegalArgumentException("durantion must more than 0"); 122 | } 123 | AnimatorSet animSet = new AnimatorSet(); 124 | animSet.playTogether(mListener.onAnimators(mContentView)); 125 | animSet.addListener(new AnimatorListenerAdapter() { 126 | @Override 127 | public void onAnimationStart(Animator animation) { 128 | super.onAnimationStart(animation); 129 | changeViewVisible(view); 130 | } 131 | }); 132 | animSet.setDuration(duration); 133 | animSet.start(); 134 | }else { 135 | changeViewVisible(view); 136 | } 137 | 138 | } 139 | 140 | private void changeViewVisible(View view) { 141 | if (view == mLoadingView) { 142 | mLoadingView.setVisibility(View.VISIBLE); 143 | if (mRetryView != null) 144 | mRetryView.setVisibility(View.GONE); 145 | if (mContentView != null) 146 | mContentView.setVisibility(View.GONE); 147 | if (mEmptyView != null) 148 | mEmptyView.setVisibility(View.GONE); 149 | } else if (view == mRetryView) { 150 | mRetryView.setVisibility(View.VISIBLE); 151 | if (mLoadingView != null) 152 | mLoadingView.setVisibility(View.GONE); 153 | if (mContentView != null) 154 | mContentView.setVisibility(View.GONE); 155 | if (mEmptyView != null) 156 | mEmptyView.setVisibility(View.GONE); 157 | } else if (view == mContentView) { 158 | mContentView.setVisibility(View.VISIBLE); 159 | if (mLoadingView != null) 160 | mLoadingView.setVisibility(View.GONE); 161 | if (mRetryView != null) 162 | mRetryView.setVisibility(View.GONE); 163 | if (mEmptyView != null) 164 | mEmptyView.setVisibility(View.GONE); 165 | } else if (view == mEmptyView) { 166 | mEmptyView.setVisibility(View.VISIBLE); 167 | if (mLoadingView != null) 168 | mLoadingView.setVisibility(View.GONE); 169 | if (mRetryView != null) 170 | mRetryView.setVisibility(View.GONE); 171 | if (mContentView != null) 172 | mContentView.setVisibility(View.GONE); 173 | } 174 | } 175 | 176 | public View setContentView(int layoutId) { 177 | return setContentView(mInflater.inflate(layoutId, this, false)); 178 | } 179 | 180 | public View setLoadingView(int layoutId) { 181 | return setLoadingView(mInflater.inflate(layoutId, this, false)); 182 | } 183 | 184 | public View setEmptyView(int layoutId) { 185 | return setEmptyView(mInflater.inflate(layoutId, this, false)); 186 | } 187 | 188 | public View setRetryView(int layoutId) { 189 | return setRetryView(mInflater.inflate(layoutId, this, false)); 190 | } 191 | 192 | public View setLoadingView(View view) { 193 | View loadingView = mLoadingView; 194 | if (loadingView != null) { 195 | Log.w(TAG, "you have already set a loading view and would be instead of this new one."); 196 | } 197 | removeView(loadingView); 198 | addView(view); 199 | mLoadingView = view; 200 | return mLoadingView; 201 | } 202 | 203 | public View setEmptyView(View view) { 204 | View emptyView = mEmptyView; 205 | if (emptyView != null) { 206 | Log.w(TAG, "you have already set a empty view and would be instead of this new one."); 207 | } 208 | removeView(emptyView); 209 | addView(view); 210 | mEmptyView = view; 211 | return mEmptyView; 212 | } 213 | 214 | public View setRetryView(View view) { 215 | View retryView = mRetryView; 216 | if (retryView != null) { 217 | Log.w(TAG, "you have already set a retry view and would be instead of this new one."); 218 | } 219 | removeView(retryView); 220 | addView(view); 221 | mRetryView = view; 222 | return mRetryView; 223 | 224 | } 225 | 226 | public View setContentView(View view) { 227 | View contentView = mContentView; 228 | if (contentView != null) { 229 | Log.w(TAG, "you have already set a retry view and would be instead of this new one."); 230 | } 231 | removeView(contentView); 232 | addView(view); 233 | mContentView = view; 234 | return mContentView; 235 | } 236 | 237 | public View getRetryView() { 238 | return mRetryView; 239 | } 240 | 241 | public View getLoadingView() { 242 | return mLoadingView; 243 | } 244 | 245 | public View getContentView() { 246 | return mContentView; 247 | } 248 | 249 | public View getEmptyView() { 250 | return mEmptyView; 251 | } 252 | 253 | public interface AnimatorsListener{ 254 | Animator[] onAnimators(View view); 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /LoadManagerLib/src/main/java/com/zhangyf/loadmanagerlib/PrePageManager.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanagerlib; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.os.Handler; 6 | import android.support.v4.app.Fragment; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | /** 11 | * Created by zhangyf on 16/12/20 12 | */ 13 | public class PrePageManager { 14 | public static final int NO_LAYOUT_ID = 0; 15 | public static int BASE_LOADING_LAYOUT_ID = NO_LAYOUT_ID; 16 | public static int BASE_RETRY_LAYOUT_ID = NO_LAYOUT_ID; 17 | public static int BASE_EMPTY_LAYOUT_ID = NO_LAYOUT_ID; 18 | private OnPrePageListener mListener; 19 | private Context mContext; 20 | 21 | public PreLayout mPreLayout; 22 | 23 | 24 | public OnPrePageListener DEFAULT_LISTENER = new OnPrePageListener() { 25 | @Override 26 | public void onRetryStart(View retryView) { 27 | 28 | } 29 | }; 30 | 31 | public static void initManagerLayout(int loading_layout, int retry_layout, int empty_layout){ 32 | BASE_LOADING_LAYOUT_ID = loading_layout; 33 | BASE_RETRY_LAYOUT_ID = retry_layout; 34 | BASE_EMPTY_LAYOUT_ID = empty_layout; 35 | } 36 | 37 | 38 | public PrePageManager(Object activityOrFragmentOrView, OnPrePageListener listener) { 39 | mListener = listener; 40 | if (mListener == null) { 41 | mListener = DEFAULT_LISTENER; 42 | } 43 | 44 | ViewGroup contentParent = null; 45 | if (activityOrFragmentOrView instanceof Activity) { 46 | Activity activity = (Activity) activityOrFragmentOrView; 47 | mContext = activity; 48 | contentParent = (ViewGroup) activity.findViewById(android.R.id.content); 49 | } else if (activityOrFragmentOrView instanceof Fragment) { 50 | Fragment fragment = (Fragment) activityOrFragmentOrView; 51 | mContext = fragment.getActivity(); 52 | contentParent = (ViewGroup) (fragment.getView().getParent()); 53 | } else if (activityOrFragmentOrView instanceof View) { 54 | View view = (View) activityOrFragmentOrView; 55 | contentParent = (ViewGroup) (view.getParent()); 56 | mContext = view.getContext(); 57 | } else { 58 | throw new IllegalArgumentException("the argument's type must be Fragment or Activity: init(context)"); 59 | } 60 | int childCount = contentParent.getChildCount(); 61 | //get contentParent 62 | int index = 0; 63 | View oldContent; 64 | if (activityOrFragmentOrView instanceof View) { 65 | oldContent = (View) activityOrFragmentOrView; 66 | for (int i = 0; i < childCount; i++) { 67 | if (contentParent.getChildAt(i) == oldContent) { 68 | index = i; 69 | break; 70 | } 71 | } 72 | } else { 73 | oldContent = contentParent.getChildAt(0); 74 | } 75 | contentParent.removeView(oldContent); 76 | //setup content layout 77 | PreLayout preLayout = new PreLayout(mContext); 78 | 79 | ViewGroup.LayoutParams lp = oldContent.getLayoutParams(); 80 | contentParent.addView(preLayout, index, lp); 81 | preLayout.setContentView(oldContent); 82 | // setup loading,retry,empty layout为了让Loading默认显示,则最后add它 83 | setupRetryLayout(listener, preLayout); 84 | setupEmptyLayout(listener, preLayout); 85 | setupLoadingLayout(listener, preLayout); 86 | //callback 87 | // listener.setRetryEvent(loadingAndRetryLayout.getRetryView()); 88 | // listener.setLoadingEvent(loadingAndRetryLayout.getLoadingView()); 89 | // listener.setEmptyEvent(loadingAndRetryLayout.getEmptyView()); 90 | mPreLayout = preLayout; 91 | } 92 | 93 | private void setupEmptyLayout(OnPrePageListener listener, PreLayout preLayout) { 94 | if (listener.isSetEmptyLayout()) { 95 | int layoutId = listener.generateEmptyLayoutId(); 96 | if (layoutId != NO_LAYOUT_ID) { 97 | preLayout.setEmptyView(layoutId); 98 | } else { 99 | preLayout.setEmptyView(listener.generateEmptyLayout()); 100 | } 101 | } else { 102 | if (BASE_EMPTY_LAYOUT_ID != NO_LAYOUT_ID) 103 | preLayout.setEmptyView(BASE_EMPTY_LAYOUT_ID); 104 | } 105 | } 106 | 107 | private void setupLoadingLayout(OnPrePageListener listener, PreLayout preLayout) { 108 | if (listener.isSetLoadingLayout()) { 109 | int layoutId = listener.generateLoadingLayoutId(); 110 | if (layoutId != NO_LAYOUT_ID) { 111 | preLayout.setLoadingView(layoutId); 112 | } else { 113 | preLayout.setLoadingView(listener.generateLoadingLayout()); 114 | } 115 | } else { 116 | if (BASE_LOADING_LAYOUT_ID != NO_LAYOUT_ID) 117 | preLayout.setLoadingView(BASE_LOADING_LAYOUT_ID); 118 | } 119 | } 120 | 121 | private void setupRetryLayout(OnPrePageListener listener, PreLayout preLayout) { 122 | if (listener.isSetRetryLayout()) { 123 | int layoutId = listener.generateRetryLayoutId(); 124 | if (layoutId != NO_LAYOUT_ID) { 125 | preLayout.setLoadingView(layoutId); 126 | } else { 127 | preLayout.setLoadingView(listener.generateRetryLayout()); 128 | } 129 | } else { 130 | if (BASE_RETRY_LAYOUT_ID != NO_LAYOUT_ID) 131 | preLayout.setRetryView(BASE_RETRY_LAYOUT_ID); 132 | } 133 | } 134 | 135 | public static PrePageManager generate(Object activityOrFragment, OnPrePageListener listener) { 136 | return new PrePageManager(activityOrFragment, listener); 137 | } 138 | 139 | public void showLoading() { 140 | showLoading(0); 141 | } 142 | 143 | public void showLoading(long delay) { 144 | new Handler().postDelayed(new Runnable() { 145 | @Override 146 | public void run() { 147 | mPreLayout.showLoading(); 148 | mListener.onLoadingStart(mPreLayout.getLoadingView()); 149 | } 150 | }, delay); 151 | 152 | } 153 | 154 | /** 155 | * default 156 | */ 157 | public void showRetry() { 158 | showRetry(0); 159 | } 160 | 161 | /** 162 | * 163 | * @param delay 164 | */ 165 | public void showRetry(long delay) { 166 | showRetry(delay,null, PreLayout.DEFAULT_DURATION); 167 | } 168 | 169 | /** 170 | * 171 | * @param listener anim 172 | */ 173 | public void showRetry(final PreLayout.AnimatorsListener listener) { 174 | showRetry(0,listener, PreLayout.DEFAULT_DURATION); 175 | 176 | } 177 | 178 | /** 179 | * 180 | * @param listener 181 | * @param duration 182 | */ 183 | public void showRetry(final PreLayout.AnimatorsListener listener, long duration) { 184 | showRetry(0,listener,duration); 185 | 186 | } 187 | 188 | /** 189 | * 设置重载页 190 | * @param delay 191 | * @param listener anim 192 | */ 193 | public void showRetry(long delay, final PreLayout.AnimatorsListener listener, final long duration) { 194 | new Handler().postDelayed(new Runnable() { 195 | @Override 196 | public void run() { 197 | mPreLayout.showRetry(listener,duration); 198 | mListener.onLoadingEnd(mPreLayout.getLoadingView()); 199 | mListener.onRetryStart(mPreLayout.getRetryView()); 200 | } 201 | }, delay); 202 | } 203 | 204 | 205 | /** 206 | * default 207 | */ 208 | public void showContent() { 209 | showContent(0); 210 | } 211 | 212 | /** 213 | * 延时显示内容 214 | * 215 | * @param delay 216 | */ 217 | public void showContent(long delay) { 218 | showContent(delay,null, PreLayout.DEFAULT_DURATION); 219 | } 220 | 221 | /** 222 | * 223 | * @param listener anim 224 | */ 225 | public void showContent(final PreLayout.AnimatorsListener listener) { 226 | showContent(0,listener, PreLayout.DEFAULT_DURATION); 227 | 228 | } 229 | 230 | /** 231 | * 232 | * @param listener 233 | * @param duration 234 | */ 235 | public void showContent(final PreLayout.AnimatorsListener listener, long duration) { 236 | showContent(0,listener,duration); 237 | 238 | } 239 | 240 | /** 241 | *设置内容页 242 | * @param delay 243 | * @param listener anim 244 | */ 245 | public void showContent(long delay, final PreLayout.AnimatorsListener listener, final long duration) { 246 | new Handler().postDelayed(new Runnable() { 247 | @Override 248 | public void run() { 249 | mPreLayout.showContent(listener,duration); 250 | mListener.onLoadingEnd(mPreLayout.getLoadingView()); 251 | mListener.onContentStart(mPreLayout); 252 | } 253 | }, delay); 254 | 255 | } 256 | 257 | /** 258 | * default 259 | */ 260 | public void showEmpty() { 261 | showEmpty(0); 262 | } 263 | 264 | /** 265 | * 设置空白页 266 | * @param delay 267 | */ 268 | public void showEmpty(long delay) { 269 | new Handler().postDelayed(new Runnable() { 270 | @Override 271 | public void run() { 272 | mPreLayout.showEmpty(); 273 | mListener.onLoadingEnd(mPreLayout.getLoadingView()); 274 | mListener.onEmptyStart(mPreLayout.getEmptyView()); 275 | } 276 | }, delay); 277 | 278 | } 279 | 280 | 281 | } 282 | -------------------------------------------------------------------------------- /LoadManagerLib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LoadManager 3 | 4 | -------------------------------------------------------------------------------- /LoadManagerLib/src/test/java/com/zhangyf/loadmanagerlib/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanagerlib; 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() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LoadManager 2 | 网络请求前,页面加载等待页(Loading Page)重载(Reload Page)空白页(Empty Page)一键集成 3 | 4 | > 关于我,欢迎关注 5 |  邮箱:437220638@qq.com 6 | 7 | # Screenshots 8 | ![image](/screenshots/video1.gif) 9 | 10 | ## Getting started 11 | Add it in your root build.gradle at the end of repositories: 12 | ```java 13 | allprojects { 14 | repositories { 15 | ... 16 | maven { url "https://jitpack.io" } 17 | } 18 | } 19 | ``` 20 | Add the dependency 21 | ```java 22 | dependencies { 23 | compile 'com.github.Yuphee:LoadManager:VERSION_CODE' 24 | } 25 | ``` 26 | replace VERSION_CODE with real version name such as released in [Here](https://github.com/Yuphee/LoadManager/releases) 27 | 28 | ### notice 29 | 30 | Both have to write, or else can not be loaded successfully. 31 | 32 | ##版本  33 | - v1.0.1 增加页面显示过渡动画,可自定义 34 | - v1.0.0 增加loading基础框架 35 | 36 | ##特性 37 | - 支持Loading页的动画 38 | - 一键切换Loading,Retry,Content,Empty页面 39 | - 重载方法即可,方便集成开发 40 | 41 | ##原理说明 42 | - 动态为主页面(内容页)加上Loading,Retry,Empty页面 43 | - 支持Activity,Fragment,任何View(Fragment还有待完善) 44 | 45 | 46 | ##使用方法 47 | - 在Application中初始化布局 48 | ```java 49 | public class MApplication extends Application{ 50 | 51 | private static MApplication instance = null; 52 | 53 | public static MApplication getInstance() { 54 | if (null == instance) { 55 | instance = new MApplication(); 56 | } 57 | return instance; 58 | } 59 | 60 | public MApplication() { 61 | 62 | } 63 | 64 | @Override 65 | public void onCreate() { 66 | super.onCreate(); 67 | instance = this; 68 | initLoadManager(); 69 | } 70 | 71 | private void initLoadManager() { 72 | PrePageManager.initManagerLayout(R.layout.layout_load_view,R.layout.layout_reload_view, 73 | R.layout.empty_view); 74 | } 75 | } 76 | ``` 77 |  - 在指定Activity或者Fragment或者View中注册Manager,前提是继承BaseActivity,你可以根据具体项目已有的BaseActivity或者BaseFragment模仿Demo里面的写法,加入到你的项目中 78 | 79 | ```java 80 | @Override 81 | protected void initPreManager() { 82 | super.initPreManager(); 83 | mPrePageManager = PrePageManager.generate(this, new DefaultLoadListener() { 84 | @Override 85 | public void onRetryClick(View retryView) { 86 | // Do Something 87 | loadData(); 88 | } 89 | 90 | }); 91 | } 92 | ``` 93 | 注: DefaultLoadListener已经经过另一层封装自定义了,原始的回调接口是OnLoadingAndRetryListener,在各个界面的显示前后都会有回调,如果想针对具体的某个Activity或者Fragment或者View进行布局,可以重载下面这些方法来替换Application中默认的布局 94 | 95 | ```java 96 | @Override 97 | public int generateLoadingLayoutId() { 98 | return super.generateLoadingLayoutId(); 99 | } 100 | 101 | @Override 102 | public int generateRetryLayoutId() { 103 | return super.generateRetryLayoutId(); 104 | } 105 | 106 | @Override 107 | public int generateEmptyLayoutId() { 108 | return super.generateEmptyLayoutId(); 109 | } 110 | ``` 111 | 112 | - 通过
113 | mPrePageManager.showLoading(delay,anim)
114 | mPrePageManager.showContent(delay,anim)
115 | mPrePageManager.showRetry(delay,anim)
116 | 去显示指定页面 117 | 118 | ##TODO 119 | -集成更多动画,增加页面切换过渡效果支持,优化Fragment下集成,注:上面的gif中activity之间的过渡动画是transition framework的动画并非框架动画(最近兴趣刚研究的),框架的动画添加是在Loading结束时显示内容页或者重载页的时候出现的动画。可以自定义成你想要的效果,demo中只是简陋的平移和渐变,后续会加入一些好看的默认动画!谢谢大家支持! 120 | 121 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.1" 6 | defaultConfig { 7 | applicationId "com.zhangyf.loadmanager" 8 | minSdkVersion 9 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.0.+' 28 | compile 'com.jakewharton:butterknife:7.0.1' 29 | testCompile 'junit:junit:4.12' 30 | compile project(':LoadManagerLib') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\AndroidSdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zhangyf/loadmanager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager; 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 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zhangyf.loadmanager", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/AnimActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager; 2 | 3 | import android.os.Build; 4 | import android.os.Bundle; 5 | import android.os.Handler; 6 | import android.support.annotation.Nullable; 7 | import android.support.annotation.RequiresApi; 8 | import android.support.v4.app.Fragment; 9 | import android.transition.Fade; 10 | import android.view.Gravity; 11 | import android.view.Window; 12 | import android.view.animation.DecelerateInterpolator; 13 | 14 | import com.zhangyf.loadmanager.base.BaseActivity; 15 | 16 | import java.util.Random; 17 | 18 | /** 19 | * Created by zhangyf on 2016/12/23. 20 | */ 21 | 22 | public class AnimActivity extends BaseActivity{ 23 | 24 | @Override 25 | protected void onCreate(@Nullable Bundle savedInstanceState) { 26 | getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); 27 | setLayoutId(R.layout.activity_anim); 28 | super.onCreate(savedInstanceState); 29 | } 30 | 31 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 32 | @Override 33 | protected void onInit(Bundle savedInstanceState) { 34 | Fade fade = new Fade(); 35 | fade.setInterpolator(new DecelerateInterpolator()); 36 | fade.setDuration(500);//动画持续时间 37 | getWindow().setEnterTransition(fade); 38 | 39 | Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fm_content); 40 | 41 | if (fragment == null) 42 | { 43 | getSupportFragmentManager().beginTransaction().add(R.id.fm_content, new NormalFragment()).commit(); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager; 2 | 3 | import android.animation.Animator; 4 | import android.animation.ObjectAnimator; 5 | import android.app.ActivityOptions; 6 | import android.content.Intent; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | import android.os.Handler; 10 | import android.support.annotation.Nullable; 11 | import android.support.annotation.RequiresApi; 12 | import android.support.v4.content.ContextCompat; 13 | import android.transition.Slide; 14 | import android.view.Gravity; 15 | import android.view.View; 16 | import android.view.Window; 17 | import android.view.animation.DecelerateInterpolator; 18 | import android.widget.Button; 19 | import android.widget.LinearLayout; 20 | 21 | import com.zhangyf.loadmanager.base.BaseActivity; 22 | import com.zhangyf.loadmanager.listener.DefaultLoadListener; 23 | import com.zhangyf.loadmanagerlib.PreLayout; 24 | import com.zhangyf.loadmanagerlib.PrePageManager; 25 | 26 | import java.util.Random; 27 | 28 | import butterknife.Bind; 29 | import butterknife.OnClick; 30 | 31 | /** 32 | * Created by zhangyf on 16/12/22 33 | * fragment可能会存在问题建议使用LoadingAndRetryManager.generate(View or Activity, new DefaultLoadListener() 34 | */ 35 | public class MainActivity extends BaseActivity { 36 | 37 | @Bind(R.id.rl_main) 38 | LinearLayout rl_main; 39 | @Bind(R.id.bt_test) 40 | Button bt_test; 41 | private boolean isload; 42 | 43 | @Override 44 | protected void onCreate(@Nullable Bundle savedInstanceState) { 45 | getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); 46 | setLayoutId(R.layout.activity_main); 47 | super.onCreate(savedInstanceState); 48 | } 49 | 50 | 51 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 52 | @Override 53 | protected void onInit(Bundle savedInstanceState) { 54 | Slide slide = new Slide(); 55 | slide.setSlideEdge(Gravity.LEFT);//滑出的方向 56 | slide.setInterpolator(new DecelerateInterpolator()); 57 | slide.setDuration(500);//动画持续时间 58 | getWindow().setExitTransition(slide); 59 | // init your data and call mLoadingAndRetryManager.xxx to control the view show and gone 60 | loadData(); 61 | ContextCompat.getColor(this, R.color.colorPrimary); 62 | } 63 | 64 | private void loadData() { 65 | mPrePageManager.showLoading(); 66 | new Handler().postDelayed(new Runnable() { 67 | @Override 68 | public void run() { 69 | Random random = new Random(); 70 | int te = random.nextInt(10); 71 | 72 | if(!isload){ 73 | mPrePageManager.showRetry(); 74 | isload = true; 75 | }else{ 76 | // you can set showContent(delay) or set the showview entering animator 77 | mPrePageManager.showContent(new PreLayout.AnimatorsListener(){ 78 | @Override 79 | public Animator[] onAnimators(View view) { 80 | return new Animator[] { ObjectAnimator.ofFloat(view, "alpha", 0f, 1f) 81 | ,ObjectAnimator.ofFloat(view, "translationY", 2000, 0)}; 82 | } 83 | }); 84 | } 85 | // if (te < 5) { 86 | // mLoadingAndRetryManager.showRetry(); 87 | // } else { 88 | // // you can set showContent(delay) or set the showview entering animator 89 | // mLoadingAndRetryManager.showContent(new LoadingAndRetryLayout.AnimatorsListener(){ 90 | // @Override 91 | // public Animator[] onAnimators(View view) { 92 | // return new Animator[] { ObjectAnimator.ofFloat(view, "alpha", 0f, 1f) 93 | // ,ObjectAnimator.ofFloat(view, "translationY", 2000, 0)}; 94 | // } 95 | // }); 96 | // } 97 | 98 | } 99 | }, 3000); 100 | } 101 | 102 | @Override 103 | protected void initPreManager() { 104 | super.initPreManager(); 105 | mPrePageManager = PrePageManager.generate(this, new DefaultLoadListener() { 106 | @Override 107 | public void onRetryClick(View retryView) { 108 | // Do Something 109 | loadData(); 110 | } 111 | 112 | }); 113 | } 114 | 115 | 116 | @OnClick(R.id.rl_main) 117 | public void onClick(View view) { 118 | switch (view.getId()) { 119 | case R.id.rl_main: 120 | Intent intent = new Intent(this,AnimActivity.class); 121 | startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,bt_test,"shareName").toBundle()); 122 | break; 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/NormalFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.support.annotation.Nullable; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.zhangyf.loadmanager.base.BaseLazyFragment; 11 | import com.zhangyf.loadmanager.listener.DefaultLoadListener; 12 | import com.zhangyf.loadmanagerlib.PrePageManager; 13 | 14 | import java.util.Random; 15 | 16 | /** 17 | * Created by zhangyf on 16/12/26. 18 | */ 19 | public class NormalFragment extends BaseLazyFragment 20 | { 21 | 22 | private boolean isload; 23 | 24 | @Nullable 25 | @Override 26 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 27 | { 28 | setLayoutId(R.layout.fragment_anim); 29 | return super.onCreateView(inflater, container, savedInstanceState); 30 | } 31 | 32 | @Override 33 | protected void onInit() { 34 | loadData(); 35 | } 36 | 37 | @Override 38 | protected void onLazyLoad() { 39 | 40 | } 41 | 42 | @Override 43 | protected void initPreManager() { 44 | super.initPreManager(); 45 | mPrePageManager = PrePageManager.generate(this, new DefaultLoadListener() 46 | { 47 | @Override 48 | public void onRetryClick(View retryView) { 49 | loadData(); 50 | } 51 | 52 | }); 53 | } 54 | 55 | private void loadData() { 56 | mPrePageManager.showLoading(); 57 | new Handler().postDelayed(new Runnable() { 58 | @Override 59 | public void run() { 60 | Random random = new Random(); 61 | int te = random.nextInt(10); 62 | if(!isload){ 63 | mPrePageManager.showRetry(); 64 | isload = true; 65 | }else{ 66 | mPrePageManager.showContent(); 67 | } 68 | // if (te < 5) { 69 | // mLoadingAndRetryManager.showRetry(); 70 | // } else { 71 | // // you can set showContent(delay) 72 | // mLoadingAndRetryManager.showContent(); 73 | // } 74 | 75 | } 76 | }, 3000); 77 | } 78 | 79 | } 80 | 81 | 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.base; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | 8 | import com.zhangyf.loadmanagerlib.PrePageManager; 9 | 10 | import butterknife.ButterKnife; 11 | 12 | /** 13 | * Created by zhangyf on 2016/12/22. 14 | */ 15 | 16 | public abstract class BaseActivity extends AppCompatActivity { 17 | 18 | protected PrePageManager mPrePageManager; 19 | protected int mLayoutId; 20 | public Context mContext; 21 | 22 | @Override 23 | protected void onCreate(@Nullable Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | createView(mLayoutId,savedInstanceState); 26 | mContext = this; 27 | } 28 | 29 | protected void createView(int layoutId, Bundle savedInstanceState) { 30 | if (layoutId != 0) { 31 | setContentView(layoutId); 32 | ButterKnife.bind(this); 33 | } 34 | // 页面加载管理器初始化 35 | initPreManager(); 36 | onInit(savedInstanceState); 37 | } 38 | 39 | protected void setLayoutId(int layoutId) { 40 | this.mLayoutId = layoutId; 41 | } 42 | 43 | protected abstract void onInit(Bundle savedInstanceState); 44 | 45 | protected void initPreManager(){}; 46 | 47 | @Override 48 | protected void onDestroy() { 49 | super.onDestroy(); 50 | ButterKnife.unbind(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/base/BaseLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.base; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | 13 | import com.zhangyf.loadmanagerlib.PrePageManager; 14 | 15 | import butterknife.ButterKnife; 16 | 17 | /** 18 | * Created by zhangyf on 2016/12/26. 19 | */ 20 | public abstract class BaseLazyFragment extends Fragment { 21 | 22 | private boolean isVisible, isPrepared, isLazyLoaded; 23 | protected AppCompatActivity mActivity; 24 | public View root; 25 | protected int layoutId; 26 | protected PrePageManager mPrePageManager; 27 | 28 | @Override 29 | public void onAttach(Context context) { 30 | super.onAttach(context); 31 | mActivity = (AppCompatActivity) context; 32 | } 33 | 34 | @Nullable 35 | @Override 36 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 37 | if (layoutId != 0) { 38 | root = inflater.inflate(layoutId, container, false); 39 | ButterKnife.bind(this, root); 40 | } 41 | return root; 42 | } 43 | 44 | @Override 45 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 46 | super.onActivityCreated(savedInstanceState); 47 | // 页面加载管理器初始化 48 | initPreManager(); 49 | onInit(); 50 | isPrepared = true; 51 | lazyLoad(); 52 | } 53 | 54 | @Override 55 | public void onDestroyView() { 56 | isPrepared = false; 57 | super.onDestroyView(); 58 | ButterKnife.unbind(this); 59 | } 60 | 61 | @Override 62 | public void onResume() { 63 | super.onResume(); 64 | } 65 | 66 | @Override 67 | public void onPause() { 68 | super.onPause(); 69 | } 70 | 71 | protected void setLayoutId(int layoutId) { 72 | this.layoutId = layoutId; 73 | } 74 | 75 | protected abstract void onInit(); 76 | 77 | protected void onRepeatLazyLoad(){} 78 | 79 | protected abstract void onLazyLoad(); 80 | 81 | @Override 82 | public void setUserVisibleHint(boolean isVisibleToUser) { 83 | super.setUserVisibleHint(isVisibleToUser); 84 | if (isVisibleToUser) { 85 | isVisible = true; 86 | lazyLoad(); 87 | } else { 88 | isVisible = false; 89 | } 90 | } 91 | 92 | public void lazyLoad() { 93 | if(isPrepared && isVisible) { 94 | if(!isLazyLoaded){ 95 | isLazyLoaded = true; 96 | onLazyLoad(); 97 | } 98 | else { 99 | onRepeatLazyLoad(); 100 | } 101 | } 102 | } 103 | 104 | protected void initPreManager(){} 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/base/MApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.base; 2 | 3 | import android.app.Application; 4 | 5 | import com.zhangyf.loadmanager.R; 6 | import com.zhangyf.loadmanagerlib.PrePageManager; 7 | 8 | /** 9 | * Created by zhangyf on 2016/12/22. 10 | */ 11 | 12 | public class MApplication extends Application{ 13 | 14 | private static MApplication instance = null; 15 | 16 | public static MApplication getInstance() { 17 | if (null == instance) { 18 | instance = new MApplication(); 19 | } 20 | return instance; 21 | } 22 | 23 | public MApplication() { 24 | 25 | } 26 | 27 | @Override 28 | public void onCreate() { 29 | super.onCreate(); 30 | instance = this; 31 | initLoadManager(); 32 | } 33 | 34 | private void initLoadManager() { 35 | PrePageManager.initManagerLayout(R.layout.layout_load_view,R.layout.layout_reload_view, 36 | R.layout.empty_view); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/listener/DefaultLoadListener.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.listener; 2 | 3 | import android.os.Handler; 4 | import android.view.View; 5 | 6 | import com.zhangyf.loadmanager.R; 7 | import com.zhangyf.loadmanager.widget.loadingdrawable.LoadingView; 8 | import com.zhangyf.loadmanagerlib.OnPrePageListener; 9 | 10 | 11 | /** 12 | * Created by zhangyf on 2016/12/21. 13 | * 默认叶子加载动画Loading页 14 | */ 15 | 16 | public abstract class DefaultLoadListener extends OnPrePageListener { 17 | 18 | public abstract void onRetryClick(View retryView); 19 | 20 | @Override 21 | public void onRetryStart(final View retryView) { 22 | View view = retryView.findViewById(R.id.btn_reload); 23 | view.setOnClickListener(new View.OnClickListener() 24 | { 25 | @Override 26 | public void onClick(View v) 27 | { 28 | onRetryClick(retryView); 29 | } 30 | }); 31 | } 32 | 33 | @Override 34 | public void onLoadingStart(View loadingView) { 35 | super.onLoadingStart(loadingView); 36 | final LoadingView view = (LoadingView) loadingView.findViewById(R.id.electric_fan_view); 37 | new Handler().postDelayed(new Runnable() { 38 | @Override 39 | public void run() { 40 | view.startAnimation(); 41 | } 42 | },200); 43 | 44 | } 45 | 46 | @Override 47 | public void onLoadingEnd(View loadingView) { 48 | super.onLoadingEnd(loadingView); 49 | final LoadingView view = (LoadingView) loadingView.findViewById(R.id.electric_fan_view); 50 | view.stopAnimation(); 51 | } 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/DensityUtil.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.content.Context; 4 | 5 | public class DensityUtil { 6 | 7 | public static float dip2px(Context context, float dpValue) { 8 | float scale = context.getResources().getDisplayMetrics().density; 9 | return dpValue * scale; 10 | } 11 | } -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/ElectricFanLoadingRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.AnimatorSet; 6 | import android.animation.TypeEvaluator; 7 | import android.animation.ValueAnimator; 8 | import android.content.Context; 9 | import android.graphics.Canvas; 10 | import android.graphics.Color; 11 | import android.graphics.ColorFilter; 12 | import android.graphics.Paint; 13 | import android.graphics.Path; 14 | import android.graphics.PointF; 15 | import android.graphics.Rect; 16 | import android.graphics.RectF; 17 | import android.graphics.drawable.Drawable; 18 | import android.support.annotation.IntDef; 19 | import android.support.v4.view.animation.FastOutLinearInInterpolator; 20 | import android.support.v4.view.animation.FastOutSlowInInterpolator; 21 | import android.view.animation.AccelerateInterpolator; 22 | import android.view.animation.DecelerateInterpolator; 23 | import android.view.animation.Interpolator; 24 | import android.view.animation.LinearInterpolator; 25 | 26 | 27 | import com.zhangyf.loadmanager.R; 28 | 29 | import java.lang.annotation.Retention; 30 | import java.lang.annotation.RetentionPolicy; 31 | import java.util.ArrayList; 32 | import java.util.List; 33 | import java.util.Random; 34 | 35 | 36 | public class ElectricFanLoadingRenderer extends LoadingRenderer { 37 | private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator(); 38 | private static final Interpolator MATERIAL_INTERPOLATOR = new FastOutSlowInInterpolator(); 39 | private static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(); 40 | private static final Interpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator(); 41 | private static final Interpolator FASTOUTLINEARIN_INTERPOLATOR = new FastOutLinearInInterpolator(); 42 | 43 | private static final Interpolator[] INTERPOLATORS = new Interpolator[]{LINEAR_INTERPOLATOR, 44 | DECELERATE_INTERPOLATOR, ACCELERATE_INTERPOLATOR, FASTOUTLINEARIN_INTERPOLATOR, MATERIAL_INTERPOLATOR}; 45 | private static final List mLeafHolders = new ArrayList<>(); 46 | private static final Random mRandom = new Random(); 47 | private final Animator.AnimatorListener mAnimatorListener = new AnimatorListenerAdapter() { 48 | @Override 49 | public void onAnimationRepeat(Animator animator) { 50 | super.onAnimationRepeat(animator); 51 | reset(); 52 | } 53 | }; 54 | 55 | public static final int MODE_NORMAL = 0; 56 | public static final int MODE_LEAF_COUNT = 1; 57 | 58 | @IntDef({MODE_NORMAL, MODE_LEAF_COUNT}) 59 | @Retention(RetentionPolicy.SOURCE) 60 | public @interface MODE { 61 | } 62 | 63 | private static final String PERCENTAGE_100 = "100%"; 64 | 65 | private static final long ANIMATION_DURATION = 3333; 66 | 67 | private static final int LEAF_COUNT = 28; 68 | private static final int DEGREE_180 = 180; 69 | private static final int DEGREE_360 = 360; 70 | private static final int FULL_GROUP_ROTATION = (int) (5.25f * DEGREE_360); 71 | 72 | private static final int DEFAULT_PROGRESS_COLOR = 0xfffca72e; 73 | private static final int DEFAULT_PROGRESS_BGCOLOR = 0xfffcd49f; 74 | private static final int DEFAULT_ELECTRIC_FAN_BGCOLOR = 0xfffccc59; 75 | private static final int DEFAULT_ELECTRIC_FAN_OUTLINE_COLOR = Color.WHITE; 76 | 77 | private static final float DEFAULT_WIDTH = 182.0f; 78 | private static final float DEFAULT_HEIGHT = 65.0f; 79 | private static final float DEFAULT_TEXT_SIZE = 11.0f; 80 | private static final float DEFAULT_STROKE_WIDTH = 2.0f; 81 | private static final float DEFAULT_STROKE_INTERVAL = .2f; 82 | private static final float DEFAULT_CENTER_RADIUS = 16.0f; 83 | private static final float DEFAULT_PROGRESS_CENTER_RADIUS = 11.0f; 84 | 85 | private static final float DEFAULT_LEAF_FLY_DURATION_FACTOR = 0.1f; 86 | 87 | private static final float LEAF_CREATE_DURATION_INTERVAL = 1.0f / LEAF_COUNT; 88 | private static final float DECELERATE_DURATION_PERCENTAGE = 0.4f; 89 | private static final float ACCELERATE_DURATION_PERCENTAGE = 0.6f; 90 | 91 | private final Paint mPaint = new Paint(); 92 | private final RectF mTempBounds = new RectF(); 93 | private final RectF mCurrentProgressBounds = new RectF(); 94 | 95 | private float mTextSize; 96 | private float mStrokeXInset; 97 | private float mStrokeYInset; 98 | private float mProgressCenterRadius; 99 | 100 | private float mScale; 101 | private float mRotation; 102 | private float mProgress; 103 | 104 | private float mNextLeafCreateThreshold; 105 | 106 | private int mProgressColor; 107 | private int mProgressBgColor; 108 | private int mElectricFanBgColor; 109 | private int mElectricFanOutlineColor; 110 | 111 | private float mStrokeWidth; 112 | private float mCenterRadius; 113 | 114 | @MODE 115 | private int mMode; 116 | private int mCurrentLeafCount; 117 | 118 | private Drawable mLeafDrawable; 119 | private Drawable mLoadingDrawable; 120 | private Drawable mElectricFanDrawable; 121 | 122 | private ElectricFanLoadingRenderer(Context context) { 123 | super(context); 124 | init(context); 125 | setupPaint(); 126 | addRenderListener(mAnimatorListener); 127 | } 128 | 129 | private void init(Context context) { 130 | mMode = MODE_NORMAL; 131 | 132 | mWidth = DensityUtil.dip2px(context, DEFAULT_WIDTH); 133 | mHeight = DensityUtil.dip2px(context, DEFAULT_HEIGHT); 134 | mTextSize = DensityUtil.dip2px(context, DEFAULT_TEXT_SIZE); 135 | mStrokeWidth = DensityUtil.dip2px(context, DEFAULT_STROKE_WIDTH); 136 | mCenterRadius = DensityUtil.dip2px(context, DEFAULT_CENTER_RADIUS); 137 | mProgressCenterRadius = DensityUtil.dip2px(context, DEFAULT_PROGRESS_CENTER_RADIUS); 138 | 139 | mProgressColor = DEFAULT_PROGRESS_COLOR; 140 | mProgressBgColor = DEFAULT_PROGRESS_BGCOLOR; 141 | mElectricFanBgColor = DEFAULT_ELECTRIC_FAN_BGCOLOR; 142 | mElectricFanOutlineColor = DEFAULT_ELECTRIC_FAN_OUTLINE_COLOR; 143 | 144 | mLeafDrawable = context.getResources().getDrawable(R.mipmap.ic_leaf); 145 | mLoadingDrawable = context.getResources().getDrawable(R.mipmap.ic_loading); 146 | mElectricFanDrawable = context.getResources().getDrawable(R.mipmap.ic_eletric_fan); 147 | 148 | mDuration = ANIMATION_DURATION; 149 | setInsets((int) mWidth, (int) mHeight); 150 | } 151 | 152 | private void setupPaint() { 153 | mPaint.setAntiAlias(true); 154 | mPaint.setStrokeWidth(mStrokeWidth); 155 | mPaint.setStyle(Paint.Style.STROKE); 156 | mPaint.setStrokeCap(Paint.Cap.ROUND); 157 | } 158 | 159 | @Override 160 | protected void draw(Canvas canvas, Rect bounds) { 161 | int saveCount = canvas.save(); 162 | 163 | RectF arcBounds = mTempBounds; 164 | arcBounds.set(bounds); 165 | arcBounds.inset(mStrokeXInset, mStrokeYInset); 166 | 167 | mCurrentProgressBounds.set(arcBounds.left, arcBounds.bottom - 2 * mCenterRadius, 168 | arcBounds.right, arcBounds.bottom); 169 | 170 | //draw loading drawable 171 | mLoadingDrawable.setBounds((int) arcBounds.centerX() - mLoadingDrawable.getIntrinsicWidth() / 2, 172 | 0, 173 | (int) arcBounds.centerX() + mLoadingDrawable.getIntrinsicWidth() / 2, 174 | mLoadingDrawable.getIntrinsicHeight()); 175 | mLoadingDrawable.draw(canvas); 176 | 177 | //draw progress background 178 | float progressInset = mCenterRadius - mProgressCenterRadius; 179 | RectF progressRect = new RectF(mCurrentProgressBounds); 180 | //sub DEFAULT_STROKE_INTERVAL, otherwise will have a interval between progress background and progress outline 181 | progressRect.inset(progressInset - DEFAULT_STROKE_INTERVAL, progressInset - DEFAULT_STROKE_INTERVAL); 182 | mPaint.setColor(mProgressBgColor); 183 | mPaint.setStyle(Paint.Style.FILL); 184 | canvas.drawRoundRect(progressRect, mProgressCenterRadius, mProgressCenterRadius, mPaint); 185 | 186 | //draw progress 187 | mPaint.setColor(mProgressColor); 188 | mPaint.setStyle(Paint.Style.FILL); 189 | canvas.drawPath(createProgressPath(mProgress, mProgressCenterRadius, progressRect), mPaint); 190 | 191 | //draw leaves 192 | for (int i = 0; i < mLeafHolders.size(); i++) { 193 | int leafSaveCount = canvas.save(); 194 | LeafHolder leafHolder = mLeafHolders.get(i); 195 | Rect leafBounds = leafHolder.mLeafRect; 196 | 197 | canvas.rotate(leafHolder.mLeafRotation, leafBounds.centerX(), leafBounds.centerY()); 198 | mLeafDrawable.setBounds(leafBounds); 199 | mLeafDrawable.draw(canvas); 200 | 201 | canvas.restoreToCount(leafSaveCount); 202 | } 203 | 204 | //draw progress background outline, 205 | //after drawing the leaves and then draw the outline of the progress background can 206 | //prevent the leaves from flying to the outside 207 | RectF progressOutlineRect = new RectF(mCurrentProgressBounds); 208 | float progressOutlineStrokeInset = (mCenterRadius - mProgressCenterRadius) / 2.0f; 209 | progressOutlineRect.inset(progressOutlineStrokeInset, progressOutlineStrokeInset); 210 | mPaint.setStyle(Paint.Style.STROKE); 211 | mPaint.setColor(mProgressBgColor); 212 | mPaint.setStrokeWidth(mCenterRadius - mProgressCenterRadius); 213 | canvas.drawRoundRect(progressOutlineRect, mCenterRadius, mCenterRadius, mPaint); 214 | 215 | //draw electric fan outline 216 | float electricFanCenterX = arcBounds.right - mCenterRadius; 217 | float electricFanCenterY = arcBounds.bottom - mCenterRadius; 218 | 219 | mPaint.setColor(mElectricFanOutlineColor); 220 | mPaint.setStyle(Paint.Style.STROKE); 221 | mPaint.setStrokeWidth(mStrokeWidth); 222 | canvas.drawCircle(arcBounds.right - mCenterRadius, arcBounds.bottom - mCenterRadius, 223 | mCenterRadius - mStrokeWidth / 2.0f, mPaint); 224 | 225 | //draw electric background 226 | mPaint.setColor(mElectricFanBgColor); 227 | mPaint.setStyle(Paint.Style.FILL); 228 | canvas.drawCircle(arcBounds.right - mCenterRadius, arcBounds.bottom - mCenterRadius, 229 | mCenterRadius - mStrokeWidth + DEFAULT_STROKE_INTERVAL, mPaint); 230 | 231 | //draw electric fan 232 | int rotateSaveCount = canvas.save(); 233 | canvas.rotate(mRotation, electricFanCenterX, electricFanCenterY); 234 | mElectricFanDrawable.setBounds((int) (electricFanCenterX - mElectricFanDrawable.getIntrinsicWidth() / 2 * mScale), 235 | (int) (electricFanCenterY - mElectricFanDrawable.getIntrinsicHeight() / 2 * mScale), 236 | (int) (electricFanCenterX + mElectricFanDrawable.getIntrinsicWidth() / 2 * mScale), 237 | (int) (electricFanCenterY + mElectricFanDrawable.getIntrinsicHeight() / 2 * mScale)); 238 | mElectricFanDrawable.draw(canvas); 239 | canvas.restoreToCount(rotateSaveCount); 240 | 241 | //draw 100% text 242 | if (mScale < 1.0f) { 243 | // mPaint.setTextSize(mTextSize * (1 - mScale)); 244 | // mPaint.setColor(mElectricFanOutlineColor); 245 | // Rect textRect = new Rect(); 246 | // mPaint.getTextBounds(PERCENTAGE_100, 0, PERCENTAGE_100.length(), textRect); 247 | // canvas.drawText(PERCENTAGE_100, electricFanCenterX - textRect.width() / 2.0f, 248 | // electricFanCenterY + textRect.height() / 2.0f, mPaint); 249 | } 250 | 251 | canvas.restoreToCount(saveCount); 252 | } 253 | 254 | private Path createProgressPath(float progress, float circleRadius, RectF progressRect) { 255 | RectF arcProgressRect = new RectF(progressRect.left, progressRect.top, progressRect.left + circleRadius * 2, progressRect.bottom); 256 | RectF rectProgressRect = null; 257 | 258 | float progressWidth = progress * progressRect.width(); 259 | float progressModeWidth = mMode == MODE_LEAF_COUNT ? 260 | (float) mCurrentLeafCount / (float) LEAF_COUNT * progressRect.width() : progress * progressRect.width(); 261 | 262 | float swipeAngle = DEGREE_180; 263 | //the left half circle of the progressbar 264 | if (progressModeWidth < circleRadius) { 265 | swipeAngle = progressModeWidth / circleRadius * DEGREE_180; 266 | } 267 | 268 | //the center rect of the progressbar 269 | if (progressModeWidth < progressRect.width() - circleRadius && progressModeWidth >= circleRadius) { 270 | rectProgressRect = new RectF(progressRect.left + circleRadius, progressRect.top, progressRect.left + progressModeWidth, progressRect.bottom); 271 | } 272 | 273 | //the right half circle of the progressbar 274 | if (progressWidth >= progressRect.width() - circleRadius) { 275 | rectProgressRect = new RectF(progressRect.left + circleRadius, progressRect.top, progressRect.right - circleRadius, progressRect.bottom); 276 | mScale = (progressRect.width() - progressWidth) / circleRadius; 277 | } 278 | 279 | //the left of the right half circle 280 | if (progressWidth < progressRect.width() - circleRadius) { 281 | mRotation = (progressWidth / (progressRect.width() - circleRadius)) * FULL_GROUP_ROTATION % DEGREE_360; 282 | 283 | RectF leafRect = new RectF(progressRect.left + progressWidth, progressRect.top, progressRect.right - circleRadius, progressRect.bottom); 284 | addLeaf(progress, leafRect); 285 | } 286 | 287 | Path path = new Path(); 288 | path.addArc(arcProgressRect, DEGREE_180 - swipeAngle / 2, swipeAngle); 289 | 290 | if (rectProgressRect != null) { 291 | path.addRect(rectProgressRect, Path.Direction.CW); 292 | } 293 | 294 | return path; 295 | } 296 | 297 | @Override 298 | protected void computeRender(float renderProgress) { 299 | if (renderProgress < DECELERATE_DURATION_PERCENTAGE) { 300 | mProgress = DECELERATE_INTERPOLATOR.getInterpolation(renderProgress / DECELERATE_DURATION_PERCENTAGE) * DECELERATE_DURATION_PERCENTAGE; 301 | } else { 302 | mProgress = ACCELERATE_INTERPOLATOR.getInterpolation((renderProgress - DECELERATE_DURATION_PERCENTAGE) / ACCELERATE_DURATION_PERCENTAGE) * ACCELERATE_DURATION_PERCENTAGE + DECELERATE_DURATION_PERCENTAGE; 303 | } 304 | } 305 | 306 | @Override 307 | protected void setAlpha(int alpha) { 308 | mPaint.setAlpha(alpha); 309 | 310 | } 311 | 312 | @Override 313 | protected void setColorFilter(ColorFilter cf) { 314 | mPaint.setColorFilter(cf); 315 | 316 | } 317 | 318 | @Override 319 | protected void reset() { 320 | mScale = 1.0f; 321 | mCurrentLeafCount = 0; 322 | mNextLeafCreateThreshold = 0.0f; 323 | mLeafHolders.clear(); 324 | } 325 | 326 | protected void setInsets(int width, int height) { 327 | final float minEdge = (float) Math.min(width, height); 328 | float insetXs; 329 | if (mCenterRadius <= 0 || minEdge < 0) { 330 | insetXs = (float) Math.ceil(mCenterRadius / 2.0f); 331 | } else { 332 | insetXs = mCenterRadius; 333 | } 334 | mStrokeYInset = (float) Math.ceil(mCenterRadius / 2.0f); 335 | mStrokeXInset = insetXs; 336 | } 337 | 338 | private void addLeaf(float progress, RectF leafFlyRect) { 339 | if (progress < mNextLeafCreateThreshold) { 340 | return; 341 | } 342 | mNextLeafCreateThreshold += LEAF_CREATE_DURATION_INTERVAL; 343 | 344 | LeafHolder leafHolder = new LeafHolder(); 345 | mLeafHolders.add(leafHolder); 346 | Animator leafAnimator = getAnimator(leafHolder, leafFlyRect, progress); 347 | leafAnimator.addListener(new AnimEndListener(leafHolder)); 348 | leafAnimator.start(); 349 | } 350 | 351 | private Animator getAnimator(LeafHolder target, RectF leafFlyRect, float progress) { 352 | ValueAnimator bezierValueAnimator = getBezierValueAnimator(target, leafFlyRect, progress); 353 | 354 | AnimatorSet finalSet = new AnimatorSet(); 355 | finalSet.playSequentially(bezierValueAnimator); 356 | finalSet.setInterpolator(INTERPOLATORS[mRandom.nextInt(INTERPOLATORS.length)]); 357 | finalSet.setTarget(target); 358 | return finalSet; 359 | } 360 | 361 | private ValueAnimator getBezierValueAnimator(LeafHolder target, RectF leafFlyRect, float progress) { 362 | BezierEvaluator evaluator = new BezierEvaluator(getPoint1(leafFlyRect), getPoint2(leafFlyRect)); 363 | 364 | int leafFlyStartY = (int) (mCurrentProgressBounds.bottom - mLeafDrawable.getIntrinsicHeight()); 365 | int leafFlyRange = (int) (mCurrentProgressBounds.height() - mLeafDrawable.getIntrinsicHeight()); 366 | 367 | int startPointY = leafFlyStartY - mRandom.nextInt(leafFlyRange); 368 | int endPointY = leafFlyStartY - mRandom.nextInt(leafFlyRange); 369 | 370 | ValueAnimator animator = ValueAnimator.ofObject(evaluator, 371 | new PointF((int) (leafFlyRect.right - mLeafDrawable.getIntrinsicWidth()), startPointY), 372 | new PointF(leafFlyRect.left, endPointY)); 373 | animator.addUpdateListener(new BezierListener(target)); 374 | animator.setTarget(target); 375 | 376 | animator.setDuration((long) ((mRandom.nextInt(300) + mDuration * DEFAULT_LEAF_FLY_DURATION_FACTOR) * (1.0f - progress))); 377 | 378 | return animator; 379 | } 380 | 381 | //get the pointF which belong to the right half side 382 | private PointF getPoint1(RectF leafFlyRect) { 383 | PointF point = new PointF(); 384 | point.x = leafFlyRect.right - mRandom.nextInt((int) (leafFlyRect.width() / 2) > 0 ? (int) (leafFlyRect.width() / 2) : 1); 385 | point.y = (int) (leafFlyRect.bottom - mRandom.nextInt((int) leafFlyRect.height() > 0 ? (int) leafFlyRect.height() : 1)); 386 | return point; 387 | } 388 | 389 | //get the pointF which belong to the left half side 390 | private PointF getPoint2(RectF leafFlyRect) { 391 | PointF point = new PointF(); 392 | point.x = leafFlyRect.left + mRandom.nextInt((int) (leafFlyRect.width() / 2) > 0 ? (int) (leafFlyRect.width() / 2) : 1); 393 | point.y = (int) (leafFlyRect.bottom - mRandom.nextInt((int) leafFlyRect.height() > 0 ? (int) leafFlyRect.height() : 1)); 394 | return point; 395 | } 396 | 397 | private class BezierEvaluator implements TypeEvaluator { 398 | 399 | private PointF point1; 400 | private PointF point2; 401 | 402 | public BezierEvaluator(PointF point1, PointF point2) { 403 | this.point1 = point1; 404 | this.point2 = point2; 405 | } 406 | 407 | //Third-order Bezier curve formula: B(t) = point0 * (1-t)^3 + 3 * point1 * t * (1-t)^2 + 3 * point2 * t^2 * (1-t) + point3 * t^3 408 | @Override 409 | public PointF evaluate(float fraction, PointF point0, PointF point3) { 410 | 411 | float t = fraction; 412 | float tLeft = 1.0f - t; 413 | 414 | float x = (float) (point0.x * Math.pow(tLeft, 3) + 3 * point1.x * t * Math.pow(tLeft, 2) + 3 * point2.x * Math.pow(t, 2) * tLeft + point3.x * Math.pow(t, 3)); 415 | float y = (float) (point0.y * Math.pow(tLeft, 3) + 3 * point1.y * t * Math.pow(tLeft, 2) + 3 * point2.y * Math.pow(t, 2) * tLeft + point3.y * Math.pow(t, 3)); 416 | 417 | return new PointF(x, y); 418 | } 419 | } 420 | 421 | private class BezierListener implements ValueAnimator.AnimatorUpdateListener { 422 | 423 | private LeafHolder target; 424 | 425 | public BezierListener(LeafHolder target) { 426 | this.target = target; 427 | } 428 | 429 | @Override 430 | public void onAnimationUpdate(ValueAnimator animation) { 431 | PointF point = (PointF) animation.getAnimatedValue(); 432 | target.mLeafRect.set((int) point.x, (int) point.y, 433 | (int) (point.x + mLeafDrawable.getIntrinsicWidth()), (int) (point.y + mLeafDrawable.getIntrinsicHeight())); 434 | target.mLeafRotation = target.mMaxRotation * animation.getAnimatedFraction(); 435 | } 436 | } 437 | 438 | private class AnimEndListener extends AnimatorListenerAdapter { 439 | private LeafHolder target; 440 | 441 | public AnimEndListener(LeafHolder target) { 442 | this.target = target; 443 | } 444 | 445 | @Override 446 | public void onAnimationEnd(Animator animation) { 447 | super.onAnimationEnd(animation); 448 | mLeafHolders.remove(target); 449 | mCurrentLeafCount++; 450 | } 451 | } 452 | 453 | private class LeafHolder { 454 | public Rect mLeafRect = new Rect(); 455 | public float mLeafRotation = 0.0f; 456 | 457 | public float mMaxRotation = mRandom.nextInt(120); 458 | } 459 | 460 | public static class Builder { 461 | private Context mContext; 462 | 463 | public Builder(Context mContext) { 464 | this.mContext = mContext; 465 | } 466 | 467 | public ElectricFanLoadingRenderer build() { 468 | ElectricFanLoadingRenderer loadingRenderer = new ElectricFanLoadingRenderer(mContext); 469 | return loadingRenderer; 470 | } 471 | } 472 | } 473 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/LoadingDrawable.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.graphics.Canvas; 4 | import android.graphics.ColorFilter; 5 | import android.graphics.PixelFormat; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Animatable; 8 | import android.graphics.drawable.Drawable; 9 | 10 | public class LoadingDrawable extends Drawable implements Animatable { 11 | private final LoadingRenderer mLoadingRender; 12 | 13 | private final Callback mCallback = new Callback() { 14 | @Override 15 | public void invalidateDrawable(Drawable d) { 16 | invalidateSelf(); 17 | } 18 | 19 | @Override 20 | public void scheduleDrawable(Drawable d, Runnable what, long when) { 21 | scheduleSelf(what, when); 22 | } 23 | 24 | @Override 25 | public void unscheduleDrawable(Drawable d, Runnable what) { 26 | unscheduleSelf(what); 27 | } 28 | }; 29 | 30 | public LoadingDrawable(LoadingRenderer loadingRender) { 31 | this.mLoadingRender = loadingRender; 32 | this.mLoadingRender.setCallback(mCallback); 33 | } 34 | 35 | @Override 36 | protected void onBoundsChange(Rect bounds) { 37 | super.onBoundsChange(bounds); 38 | this.mLoadingRender.setBounds(bounds); 39 | } 40 | 41 | @Override 42 | public void draw(Canvas canvas) { 43 | if (!getBounds().isEmpty()) { 44 | this.mLoadingRender.draw(canvas); 45 | } 46 | } 47 | 48 | @Override 49 | public void setAlpha(int alpha) { 50 | this.mLoadingRender.setAlpha(alpha); 51 | } 52 | 53 | @Override 54 | public void setColorFilter(ColorFilter cf) { 55 | this.mLoadingRender.setColorFilter(cf); 56 | } 57 | 58 | @Override 59 | public int getOpacity() { 60 | return PixelFormat.TRANSLUCENT; 61 | } 62 | 63 | @Override 64 | public void start() { 65 | this.mLoadingRender.start(); 66 | } 67 | 68 | @Override 69 | public void stop() { 70 | this.mLoadingRender.stop(); 71 | } 72 | 73 | @Override 74 | public boolean isRunning() { 75 | return this.mLoadingRender.isRunning(); 76 | } 77 | 78 | @Override 79 | public int getIntrinsicHeight() { 80 | return (int) this.mLoadingRender.mHeight; 81 | } 82 | 83 | @Override 84 | public int getIntrinsicWidth() { 85 | return (int) this.mLoadingRender.mWidth; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/LoadingRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.animation.Animator; 4 | import android.animation.ValueAnimator; 5 | import android.content.Context; 6 | import android.graphics.Canvas; 7 | import android.graphics.ColorFilter; 8 | import android.graphics.Rect; 9 | import android.graphics.drawable.Drawable; 10 | import android.view.animation.Animation; 11 | import android.view.animation.LinearInterpolator; 12 | 13 | public abstract class LoadingRenderer { 14 | private static final long ANIMATION_DURATION = 1333; 15 | private static final float DEFAULT_SIZE = 56.0f; 16 | 17 | private final ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener 18 | = new ValueAnimator.AnimatorUpdateListener() { 19 | @Override 20 | public void onAnimationUpdate(ValueAnimator animation) { 21 | computeRender((float) animation.getAnimatedValue()); 22 | invalidateSelf(); 23 | } 24 | }; 25 | 26 | /** 27 | * Whenever {@link LoadingDrawable} boundary changes mBounds will be updated. 28 | * More details you can see {@link LoadingDrawable#onBoundsChange(Rect)} 29 | */ 30 | protected final Rect mBounds = new Rect(); 31 | 32 | private Drawable.Callback mCallback; 33 | private ValueAnimator mRenderAnimator; 34 | 35 | protected long mDuration; 36 | 37 | protected float mWidth; 38 | protected float mHeight; 39 | 40 | public LoadingRenderer(Context context) { 41 | initParams(context); 42 | setupAnimators(); 43 | } 44 | 45 | @Deprecated 46 | protected void draw(Canvas canvas, Rect bounds) { 47 | } 48 | 49 | protected void draw(Canvas canvas) { 50 | draw(canvas, mBounds); 51 | } 52 | 53 | protected abstract void computeRender(float renderProgress); 54 | 55 | protected abstract void setAlpha(int alpha); 56 | 57 | protected abstract void setColorFilter(ColorFilter cf); 58 | 59 | protected abstract void reset(); 60 | 61 | protected void addRenderListener(Animator.AnimatorListener animatorListener) { 62 | mRenderAnimator.addListener(animatorListener); 63 | } 64 | 65 | void start() { 66 | reset(); 67 | mRenderAnimator.addUpdateListener(mAnimatorUpdateListener); 68 | 69 | mRenderAnimator.setRepeatCount(ValueAnimator.INFINITE); 70 | mRenderAnimator.setDuration(mDuration); 71 | mRenderAnimator.start(); 72 | } 73 | 74 | void stop() { 75 | // if I just call mRenderAnimator.end(), 76 | // it will always call the method onAnimationUpdate(ValueAnimator animation) 77 | // why ? if you know why please send email to me (dinus_developer@163.com) 78 | mRenderAnimator.removeUpdateListener(mAnimatorUpdateListener); 79 | 80 | mRenderAnimator.setRepeatCount(0); 81 | mRenderAnimator.setDuration(0); 82 | mRenderAnimator.end(); 83 | // 重置为初始状态,防止retry的时候进度不对 84 | computeRender(0f); 85 | invalidateSelf(); 86 | } 87 | 88 | boolean isRunning() { 89 | return mRenderAnimator.isRunning(); 90 | } 91 | 92 | void setCallback(Drawable.Callback callback) { 93 | this.mCallback = callback; 94 | } 95 | 96 | void setBounds(Rect bounds) { 97 | mBounds.set(bounds); 98 | } 99 | 100 | private void initParams(Context context) { 101 | mWidth = DensityUtil.dip2px(context, DEFAULT_SIZE); 102 | mHeight = DensityUtil.dip2px(context, DEFAULT_SIZE); 103 | 104 | mDuration = ANIMATION_DURATION; 105 | } 106 | 107 | private void setupAnimators() { 108 | mRenderAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); 109 | mRenderAnimator.setRepeatCount(Animation.INFINITE); 110 | mRenderAnimator.setRepeatMode(Animation.RESTART); 111 | mRenderAnimator.setDuration(mDuration); 112 | //fuck you! the default interpolator is AccelerateDecelerateInterpolator 113 | mRenderAnimator.setInterpolator(new LinearInterpolator()); 114 | mRenderAnimator.addUpdateListener(mAnimatorUpdateListener); 115 | } 116 | 117 | private void invalidateSelf() { 118 | mCallback.invalidateDrawable(null); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/LoadingRendererFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.content.Context; 4 | import android.util.SparseArray; 5 | 6 | import java.lang.reflect.Constructor; 7 | 8 | 9 | public final class LoadingRendererFactory { 10 | private static final SparseArray> LOADING_RENDERERS = new SparseArray<>(); 11 | 12 | static { 13 | //circle rotate 14 | // LOADING_RENDERERS.put(0, MaterialLoadingRenderer.class); 15 | // LOADING_RENDERERS.put(1, LevelLoadingRenderer.class); 16 | // LOADING_RENDERERS.put(2, WhorlLoadingRenderer.class); 17 | // LOADING_RENDERERS.put(3, GearLoadingRenderer.class); 18 | // //circle jump 19 | // LOADING_RENDERERS.put(4, SwapLoadingRenderer.class); 20 | // LOADING_RENDERERS.put(5, GuardLoadingRenderer.class); 21 | // LOADING_RENDERERS.put(6, DanceLoadingRenderer.class); 22 | // LOADING_RENDERERS.put(7, CollisionLoadingRenderer.class); 23 | // //scenery 24 | // LOADING_RENDERERS.put(8, DayNightLoadingRenderer.class); 25 | LOADING_RENDERERS.put(9, ElectricFanLoadingRenderer.class); 26 | //animal 27 | // LOADING_RENDERERS.put(10, FishLoadingRenderer.class); 28 | // LOADING_RENDERERS.put(11, GhostsEyeLoadingRenderer.class); 29 | // //goods 30 | // LOADING_RENDERERS.put(12, BalloonLoadingRenderer.class); 31 | // LOADING_RENDERERS.put(13, WaterBottleLoadingRenderer.class); 32 | // //shape change 33 | // LOADING_RENDERERS.put(14, CircleBroodLoadingRenderer.class); 34 | // LOADING_RENDERERS.put(15, CoolWaitLoadingRenderer.class); 35 | } 36 | 37 | private LoadingRendererFactory() { 38 | } 39 | 40 | public static LoadingRenderer createLoadingRenderer(Context context, int loadingRendererId) throws Exception { 41 | Class loadingRendererClazz = LOADING_RENDERERS.get(loadingRendererId); 42 | Constructor[] constructors = loadingRendererClazz.getDeclaredConstructors(); 43 | for (Constructor constructor : constructors) { 44 | Class[] parameterTypes = constructor.getParameterTypes(); 45 | if (parameterTypes != null 46 | && parameterTypes.length == 1 47 | && parameterTypes[0].equals(Context.class)) { 48 | constructor.setAccessible(true); 49 | return (LoadingRenderer) constructor.newInstance(context); 50 | } 51 | } 52 | 53 | throw new InstantiationException(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhangyf/loadmanager/widget/loadingdrawable/LoadingView.java: -------------------------------------------------------------------------------- 1 | package com.zhangyf.loadmanager.widget.loadingdrawable; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.util.AttributeSet; 6 | import android.view.View; 7 | import android.widget.ImageView; 8 | 9 | import com.zhangyf.loadmanager.R; 10 | 11 | 12 | public class LoadingView extends ImageView { 13 | private LoadingDrawable mLoadingDrawable; 14 | 15 | public LoadingView(Context context) { 16 | super(context); 17 | } 18 | 19 | public LoadingView(Context context, AttributeSet attrs) { 20 | super(context, attrs); 21 | initAttrs(context, attrs); 22 | } 23 | 24 | private void initAttrs(Context context, AttributeSet attrs) { 25 | try { 26 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LoadingView); 27 | int loadingRendererId = ta.getInt(R.styleable.LoadingView_loading_renderer, 0); 28 | LoadingRenderer loadingRenderer = LoadingRendererFactory.createLoadingRenderer(context, loadingRendererId); 29 | setLoadingRenderer(loadingRenderer); 30 | ta.recycle(); 31 | } catch (Exception e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | 36 | public void setLoadingRenderer(LoadingRenderer loadingRenderer) { 37 | mLoadingDrawable = new LoadingDrawable(loadingRenderer); 38 | setImageDrawable(mLoadingDrawable); 39 | } 40 | 41 | @Override 42 | protected void onAttachedToWindow() { 43 | super.onAttachedToWindow(); 44 | // startAnimation(); 45 | } 46 | 47 | @Override 48 | protected void onDetachedFromWindow() { 49 | super.onDetachedFromWindow(); 50 | // stopAnimation(); 51 | } 52 | 53 | @Override 54 | protected void onVisibilityChanged(View changedView, int visibility) { 55 | super.onVisibilityChanged(changedView, visibility); 56 | // if (visibility == View.VISIBLE) { 57 | // startAnimation(); 58 | // } else { 59 | // stopAnimation(); 60 | // } 61 | } 62 | 63 | 64 | public void startAnimation() { 65 | if (mLoadingDrawable != null) { 66 | mLoadingDrawable.start(); 67 | } 68 | } 69 | 70 | public void stopAnimation() { 71 | if (mLoadingDrawable != null) { 72 | mLoadingDrawable.stop(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_anim.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |