├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── hokoblurdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── hokoblurdemo │ │ │ ├── activity │ │ │ ├── DynamicBlurActivity.java │ │ │ ├── EasyBlurActivity.java │ │ │ ├── MainActivity.java │ │ │ └── MultiBlurActivity.java │ │ │ └── view │ │ │ └── DragBlurringView.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── cat.jpg │ │ ├── dog.jpg │ │ ├── sample1.jpg │ │ └── sample2.jpeg │ │ ├── layout │ │ ├── activity_dynamic_blur.xml │ │ ├── activity_easy_blur.xml │ │ ├── activity_main.xml │ │ └── activity_multi_blur.xml │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── hokoblurdemo │ └── ExampleUnitTest.java ├── build.gradle ├── dependencies-plugin ├── .gitignore ├── build.gradle ├── settings.gradle └── src │ └── main │ └── java │ └── com │ └── hoko │ └── blur │ └── plugin │ └── Deps.java ├── doc ├── README_CN.md └── graphic │ ├── animation_blur_progress.gif │ └── dynamic_blur.gif ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── hoko-blur ├── .gitignore ├── build.gradle ├── maven-center.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hoko │ │ └── blur │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hoko │ │ │ └── blur │ │ │ ├── HokoBlur.java │ │ │ ├── anno │ │ │ ├── Direction.java │ │ │ ├── Mode.java │ │ │ ├── NotThreadSafe.java │ │ │ └── Scheme.java │ │ │ ├── api │ │ │ ├── IBlurBuild.java │ │ │ ├── IBlurProcessor.java │ │ │ └── IBlurResultDispatcher.java │ │ │ ├── filter │ │ │ ├── BoxBlurFilter.java │ │ │ ├── GaussianBlurFilter.java │ │ │ ├── NativeBlurFilter.java │ │ │ ├── OriginBlurFilter.java │ │ │ └── StackBlurFilter.java │ │ │ ├── opengl │ │ │ ├── BitmapTexture.java │ │ │ ├── CachePool.java │ │ │ ├── EglBuffer.java │ │ │ ├── FrameBuffer.java │ │ │ ├── FrameBufferCache.java │ │ │ ├── IRenderer.java │ │ │ ├── OffScreenBlurRenderer.java │ │ │ ├── Program.java │ │ │ ├── SimpleTexture.java │ │ │ └── Texture.java │ │ │ ├── processor │ │ │ ├── BlurProcessor.java │ │ │ ├── BlurProcessorFactory.java │ │ │ ├── HokoBlurBuild.java │ │ │ ├── NativeBlurProcessor.java │ │ │ ├── OpenGLBlurProcessor.java │ │ │ ├── OriginBlurProcessor.java │ │ │ └── RenderScriptBlurProcessor.java │ │ │ ├── task │ │ │ ├── AndroidBlurResultDispatcher.java │ │ │ ├── AsyncBlurTask.java │ │ │ ├── BitmapAsyncBlurTask.java │ │ │ ├── BlurResult.java │ │ │ ├── BlurResultRunnable.java │ │ │ ├── BlurSubTask.java │ │ │ ├── BlurTaskManager.java │ │ │ └── ViewAsyncBlurTask.java │ │ │ └── util │ │ │ ├── BitmapUtil.java │ │ │ ├── MathUtil.java │ │ │ ├── Preconditions.java │ │ │ ├── ShaderUtil.java │ │ │ └── SingleMainHandler.java │ ├── jni │ │ ├── BlurUtil.cpp │ │ ├── BoxBlurFilter.cpp │ │ ├── CMakeLists.txt │ │ ├── GaussianBlurFilter.cpp │ │ ├── StackBlurFilter.cpp │ │ └── include │ │ │ ├── BlurUtil.h │ │ │ ├── BoxBlurFilter.h │ │ │ ├── GaussianBlurFilter.h │ │ │ └── StackBlurFilter.h │ ├── res │ │ └── values │ │ │ └── strings.xml │ └── rs │ │ ├── BoxBlur.rs │ │ └── StackBlur.rs │ └── test │ └── java │ └── com │ └── hoko │ └── blur │ └── ExampleUnitTest.java ├── release.sh ├── settings.gradle └── stripe.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | #IntelliJ IDEA 36 | .idea 37 | *.iml 38 | *.ipr 39 | *.iws 40 | out 41 | 42 | # Keystore files 43 | # Uncomment the following line if you do not want to check your keystore files in. 44 | #*.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | .cxx 49 | 50 | # Google Services (e.g. APIs or Firebase) 51 | google-services.json 52 | 53 | # Freeline 54 | freeline.py 55 | freeline/ 56 | freeline_project_description.json 57 | 58 | # fastlane 59 | fastlane/report.xml 60 | fastlane/Preview.html 61 | fastlane/screenshots 62 | fastlane/test_output 63 | fastlane/readme.md -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## HokoBlur 2 | 3 | (中文版本请参看[这里](doc/README_CN.md)) 4 | 5 | HokoBlur is an Android component which provides dynamic blur effect. 6 | 7 | See the Kotlin implement [HokoBlur-Kotlin](https://github.com/HokoFly/HokoBlur-Kotlin) 8 | 9 | 10 | ### 1. Introductions 11 | 12 | - Functions: 13 | 14 | - Add blur to the image; 15 | - **Dynamic blur, real-time blurring of the background**。 16 | 17 | - Features: 18 | - Multiple schemes: RenderScript、OpenGL、Native and Java; 19 | - Multiple algorithms: Box、Stack and Gaussian algorithms. Provide different blur effect; 20 | - Multi-core and multi-threading, accelerate blurring,asynchronous interface; 21 | 22 | ### 2. Getting started 23 | 24 | 25 | #### Download 26 | 27 | ```groovy 28 | implementation 'io.github.hokofly:hoko-blur:1.5.3' 29 | ``` 30 | 31 | #### Static Blur 32 | 33 | synchronous api 34 | 35 | ```java 36 | HokoBlur.with(context) 37 | .scheme(Blur.SCHEME_NATIVE) //different implementation, RenderScript、OpenGL、Native(default) and Java 38 | .mode(Blur.MODE_STACK) //blur algorithms,Gaussian、Stack(default) and Box 39 | .radius(10) //blur radius,max=25,default=5 40 | .sampleFactor(2.0f) //scale factor,if factor=2,the width and height of a bitmap will be scale to 1/2 sizes,default=5 41 | .forceCopy(false) //If scale factor=1.0f,the origin bitmap will be modified. You could set forceCopy=true to avoid it. default=false 42 | .translateX(150)//add x axis offset when blurring 43 | .translateY(150)//add y axis offset when blurring 44 | .processor() //build a blur processor 45 | .blur(bitmap); //blur a bitmap, synchronous method 46 | 47 | ``` 48 | 49 | Daily development does not need such complicated settings. If you want a blur effect, just use as follow: 50 | 51 | ```java 52 | Bitmap outBitmap = Blur.with(context).blur(bitmap); 53 | 54 | ``` 55 | 56 | When it comes to a large size bitmap, it is recommended to use an asynchronous method. The blur job could be cancelled. 57 | 58 | 59 | ```java 60 | Future f = HokoBlur.with(this) 61 | .scheme(Blur.SCHEME_NATIVE) 62 | .mode(Blur.MODE_STACK) 63 | .radius(10) 64 | .sampleFactor(2.0f) 65 | .forceCopy(false) 66 | .asyncBlur(bitmap, new AsyncBlurTask.CallBack() { 67 | @Override 68 | public void onBlurSuccess(Bitmap outBitmap) { 69 | // do something... 70 | } 71 | 72 | @Override 73 | public void onBlurFailed() { 74 | 75 | } 76 | }); 77 | f.cancel(false); 78 | 79 | ``` 80 | 81 | ### 3. Sample 82 | 83 | #### Animation 84 | 85 | ![](https://github.com/HokoFly/HokoBlur/blob/master/doc/graphic/animation_blur_progress.gif?raw=true) 86 | 87 | 88 | #### Arbitrary Locaton Blur 89 | 90 | ![](https://github.com/HokoFly/HokoBlur/blob/master/doc/graphic/dynamic_blur.gif?raw=true) 91 | 92 | 93 | 94 | ### 4. Dynamic background blur 95 | 96 | Dynamic Blur provides real-time background blurring of View and ViewGroup, not based on Bitmap implementations. The component will blur the area where the View is located. See the repository [HokoBlurDrawable](https://github.com/HokoFly/HokoBlurDrawable). 97 | 98 | 99 | 100 | ### 5. Tips 101 | 102 | 103 | 1. When the Bitmap is not scaled (```sampleFactor(1.0f)```), the incoming Bitmap will be directly modified by the subsequent operations. So when the function returns a bitmap, it can be used immediately. 104 | 105 | 2. **It is strongly recommended to use the downScale operation before the blur operation to reduce the size of the blurred image, which will greatly improve the blur efficiency and effect.** 106 | 107 | 3. Please limit the blur radius to 25. Increasing the radius leads to much less blur effect increase than by increasing the scale factor, and if the radius increase, blur efficiency will also decrease; 108 | 109 | 4. The RenderScript solution has to be verified for compatibility. If there are scenarios that require more computation and more complex blurring, the RenderScript scheme may be better. 110 | 111 | 5. Algorithm selection 112 | - If you have low effect requirements for blurring and want to blur the image faster, please choose Box algorithm.; 113 | - If you have a higher effect requirement for blurring and can tolerate slower blurring of the image, please choose the Gaussian algorithm; 114 | - The Stack algorithm has a blur effect that is very close to the Gaussian algorithm, and it improves the efficiency. Generally, the Stack algorithm is recommended; 115 | 116 | 6. BlurDrawable is implemented by OpenGL, so if the hardware acceleration is not enabled, the background blur will be invalid. 117 | 118 | 7. Sample and usage. Please see the sample project. 119 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | import com.hoko.blur.plugin.Deps 2 | 3 | plugins { 4 | alias libs.plugins.android.application 5 | alias libs.plugins.dependencies.plugin 6 | } 7 | apply from: project.file("../stripe.gradle") 8 | 9 | android { 10 | compileSdk(Deps.compileSdkVersion) 11 | 12 | defaultConfig { 13 | applicationId "com.example.hokoblurdemo" 14 | minSdkVersion Deps.minSdkVersion 15 | targetSdkVersion Deps.targetSdkVersion 16 | buildToolsVersion = Deps.buildToolsVersion 17 | versionCode 1 18 | versionName "1.0" 19 | 20 | renderscriptTargetApi Deps.renderscriptTargetApi 21 | renderscriptSupportModeEnabled Deps.renderscriptSupportModeEnabled 22 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 23 | } 24 | 25 | compileOptions { 26 | sourceCompatibility Deps.javaVersion 27 | targetCompatibility Deps.javaVersion 28 | } 29 | 30 | buildTypes { 31 | release { 32 | minifyEnabled true 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | signingConfig signingConfigs.debug 35 | } 36 | 37 | debug { 38 | jniDebuggable = true 39 | } 40 | } 41 | lint { 42 | abortOnError false 43 | } 44 | 45 | sourceSets { 46 | main { 47 | jniLibs.srcDir "src/main/libs" 48 | } 49 | } 50 | namespace 'com.example.hokoblurdemo' 51 | } 52 | 53 | dependencies { 54 | implementation fileTree(include: ['*.jar'], dir: 'libs') 55 | testImplementation Deps.junit 56 | androidTestImplementation Deps.androidTestJunit 57 | androidTestImplementation Deps.espressoCore 58 | implementation Deps.appcompat 59 | implementation project(':hoko-blur') 60 | // implementation "io.github.hokofly:hoko-blur:${Deps.hokoBlurReleaseVersion}" 61 | } 62 | -------------------------------------------------------------------------------- /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 /Users/yuxfzju/Library/Android/sdk/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/example/hokoblurdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import android.content.Context; 6 | 7 | import androidx.test.platform.app.InstrumentationRegistry; 8 | import androidx.test.ext.junit.runners.AndroidJUnit4; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * See [testing documentation](http://d.android.com/tools/testing). 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("com.example.hokoblurdemo", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/hokoblurdemo/activity/DynamicBlurActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo.activity; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | 6 | import androidx.appcompat.app.AppCompatActivity; 7 | 8 | import com.example.hokoblurdemo.R; 9 | import com.example.hokoblurdemo.view.DragBlurringView; 10 | 11 | public class DynamicBlurActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_dynamic_blur); 17 | View blurredView = findViewById(R.id.container); 18 | DragBlurringView dragBlurringView = findViewById(R.id.blurring); 19 | dragBlurringView.setBlurredView(blurredView); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/hokoblurdemo/activity/EasyBlurActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo.activity; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapFactory; 5 | import android.os.Bundle; 6 | import android.widget.ImageView; 7 | 8 | import androidx.appcompat.app.AppCompatActivity; 9 | 10 | import com.example.hokoblurdemo.R; 11 | import com.hoko.blur.HokoBlur; 12 | import com.hoko.blur.task.AsyncBlurTask; 13 | 14 | public class EasyBlurActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_easy_blur); 20 | 21 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cat); 22 | 23 | final ImageView imageView = findViewById(R.id.image); 24 | final ImageView imageView1 = findViewById(R.id.image1); 25 | final ImageView imageView2 = findViewById(R.id.image2); 26 | final ImageView imageView3 = findViewById(R.id.image3); 27 | 28 | imageView.setImageBitmap(bitmap); 29 | imageView1.setImageBitmap(HokoBlur.with(this) 30 | .forceCopy(true) 31 | .scheme(HokoBlur.SCHEME_RENDER_SCRIPT) 32 | .sampleFactor(3.0f) 33 | .radius(20) 34 | .blur(bitmap)); 35 | HokoBlur.with(this) 36 | .scheme(HokoBlur.SCHEME_OPENGL) 37 | .translateX(150) 38 | .translateY(150) 39 | .forceCopy(false) 40 | .sampleFactor(5.0f) 41 | .asyncBlur(bitmap, new AsyncBlurTask.Callback() { 42 | @Override 43 | public void onBlurSuccess(Bitmap bitmap) { 44 | imageView2.setImageBitmap(bitmap); 45 | } 46 | 47 | @Override 48 | public void onBlurFailed(Throwable e) { 49 | e.printStackTrace(); 50 | } 51 | }); 52 | 53 | imageView1.post(() -> HokoBlur.with(EasyBlurActivity.this) 54 | .scheme(HokoBlur.SCHEME_NATIVE) 55 | .translateX(100) 56 | .translateY(100) 57 | .asyncBlur(imageView1, new AsyncBlurTask.Callback() { 58 | @Override 59 | public void onBlurSuccess(Bitmap bitmap1) { 60 | imageView3.setImageBitmap(bitmap1); 61 | } 62 | 63 | @Override 64 | public void onBlurFailed(Throwable e) { 65 | e.printStackTrace(); 66 | } 67 | })); 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/hokoblurdemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo.activity; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import androidx.appcompat.app.AppCompatActivity; 10 | 11 | import com.example.hokoblurdemo.R; 12 | 13 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | initView(); 20 | } 21 | 22 | private void initView() { 23 | Button multiBlurBtn = findViewById(R.id.multi_blur); 24 | Button dynamicBtn = findViewById(R.id.dynamic_blur); 25 | Button easyBlurBtn = findViewById(R.id.easy_blur); 26 | multiBlurBtn.setOnClickListener(this); 27 | dynamicBtn.setOnClickListener(this); 28 | easyBlurBtn.setOnClickListener(this); 29 | } 30 | 31 | @Override 32 | public void onClick(View view) { 33 | final Intent intent = new Intent(); 34 | switch (view.getId()) { 35 | case R.id.multi_blur: 36 | intent.setClass(MainActivity.this, MultiBlurActivity.class); 37 | break; 38 | case R.id.dynamic_blur: 39 | intent.setClass(MainActivity.this, DynamicBlurActivity.class); 40 | break; 41 | case R.id.easy_blur: 42 | intent.setClass(MainActivity.this, EasyBlurActivity.class); 43 | break; 44 | } 45 | ComponentName componentName = intent.resolveActivity(getPackageManager()); 46 | if (componentName != null) { 47 | startActivity(intent); 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/hokoblurdemo/activity/MultiBlurActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo.activity; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.view.animation.LinearInterpolator; 9 | import android.widget.AdapterView; 10 | import android.widget.ArrayAdapter; 11 | import android.widget.Button; 12 | import android.widget.ImageView; 13 | import android.widget.SeekBar; 14 | import android.widget.Spinner; 15 | import android.widget.SpinnerAdapter; 16 | import android.widget.TextView; 17 | 18 | import androidx.annotation.ArrayRes; 19 | import androidx.annotation.DrawableRes; 20 | import androidx.appcompat.app.AppCompatActivity; 21 | 22 | import com.example.hokoblurdemo.R; 23 | import com.hoko.blur.HokoBlur; 24 | import com.hoko.blur.api.IBlurBuild; 25 | import com.hoko.blur.api.IBlurProcessor; 26 | 27 | import java.util.concurrent.ExecutorService; 28 | import java.util.concurrent.Executors; 29 | import java.util.concurrent.Future; 30 | 31 | public class MultiBlurActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener, SeekBar.OnSeekBarChangeListener { 32 | 33 | private static final float SAMPLE_FACTOR = 8.0f; 34 | private static final int INIT_RADIUS = 5; 35 | 36 | private static final int[] TEST_IMAGE_RES = {R.drawable.sample1, R.drawable.sample2}; 37 | 38 | private int mCurrentImageRes = TEST_IMAGE_RES[0]; 39 | private int index = 0; 40 | private SeekBar mSeekBar; 41 | private TextView mRadiusText; 42 | private ImageView mImageView; 43 | 44 | private IBlurBuild mBlurBuilder; 45 | private IBlurProcessor mProcessor; 46 | private Bitmap mInBitmap; 47 | private int mRadius = INIT_RADIUS; 48 | private ValueAnimator mAnimator; 49 | private ValueAnimator mRoundAnimator; 50 | private volatile Future mFuture; 51 | private ExecutorService mDispatcher = Executors.newSingleThreadExecutor(); 52 | 53 | @Override 54 | protected void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | setContentView(R.layout.activity_multi_blur); 57 | if (getSupportActionBar() != null) { 58 | getSupportActionBar().hide(); 59 | } 60 | 61 | mImageView = findViewById(R.id.photo); 62 | mImageView.setOnClickListener(this); 63 | mSeekBar = findViewById(R.id.radius_seekbar); 64 | mSeekBar.setOnSeekBarChangeListener(this); 65 | mRadiusText = findViewById(R.id.blur_radius); 66 | 67 | Spinner schemeSpinner = findViewById(R.id.scheme_spinner); 68 | schemeSpinner.setAdapter(makeSpinnerAdapter(R.array.blur_schemes)); 69 | schemeSpinner.setOnItemSelectedListener(this); 70 | 71 | Spinner modeSpinner = findViewById(R.id.mode_spinner); 72 | modeSpinner.setAdapter(makeSpinnerAdapter(R.array.blur_modes)); 73 | modeSpinner.setOnItemSelectedListener(this); 74 | 75 | Button resetBtn = findViewById(R.id.reset_btn); 76 | resetBtn.setOnClickListener(this); 77 | Button animBtn = findViewById(R.id.anim_btn); 78 | animBtn.setOnClickListener(this); 79 | 80 | setImage(mCurrentImageRes); 81 | mBlurBuilder = HokoBlur.with(this).sampleFactor(SAMPLE_FACTOR); 82 | 83 | } 84 | 85 | private SpinnerAdapter makeSpinnerAdapter(@ArrayRes int arrayRes) { 86 | ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, 87 | arrayRes, android.R.layout.simple_spinner_item); 88 | spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 89 | return spinnerAdapter; 90 | } 91 | 92 | 93 | private void setImage(@DrawableRes final int id) { 94 | mImageView.setImageResource(id); 95 | mDispatcher.submit(() -> { 96 | mInBitmap = BitmapFactory.decodeResource(getResources(), id); 97 | 98 | runOnUiThread(() -> { 99 | endAnimators(); 100 | mAnimator = ValueAnimator.ofFloat(0, mRadius / 25f); 101 | mAnimator.setInterpolator(new LinearInterpolator()); 102 | mAnimator.addUpdateListener(animation -> { 103 | mSeekBar.setProgress((int) ((float) animation.getAnimatedValue() * 100)); 104 | updateImage((int) ((float) animation.getAnimatedValue() * 25f)); 105 | }); 106 | 107 | mAnimator.setDuration(300); 108 | mAnimator.start(); 109 | }); 110 | }); 111 | } 112 | 113 | @Override 114 | public void onItemSelected(AdapterView parent, View view, int position, long id) { 115 | final int spinnerId = parent.getId(); 116 | if (spinnerId == R.id.scheme_spinner) { 117 | switch (position) { 118 | case 0: 119 | mBlurBuilder.scheme(HokoBlur.SCHEME_RENDER_SCRIPT); 120 | break; 121 | case 1: 122 | mBlurBuilder.scheme(HokoBlur.SCHEME_OPENGL); 123 | break; 124 | case 2: 125 | mBlurBuilder.scheme(HokoBlur.SCHEME_NATIVE); 126 | break; 127 | case 3: 128 | mBlurBuilder.scheme(HokoBlur.SCHEME_JAVA); 129 | break; 130 | } 131 | 132 | } else if (spinnerId == R.id.mode_spinner) { 133 | switch (position) { 134 | case 0: 135 | mBlurBuilder.mode(HokoBlur.MODE_GAUSSIAN); 136 | break; 137 | case 1: 138 | mBlurBuilder.mode(HokoBlur.MODE_STACK); 139 | break; 140 | case 2: 141 | mBlurBuilder.mode(HokoBlur.MODE_BOX); 142 | break; 143 | } 144 | 145 | } 146 | endAnimators(); 147 | mProcessor = mBlurBuilder.processor(); 148 | mProcessor.radius(mRadius); 149 | updateImage(mRadius); 150 | 151 | } 152 | 153 | @Override 154 | public void onNothingSelected(AdapterView parent) { 155 | 156 | } 157 | 158 | @Override 159 | public void onClick(View v) { 160 | final int id = v.getId(); 161 | switch (id) { 162 | case R.id.reset_btn: 163 | endAnimators(); 164 | mImageView.setImageResource(mCurrentImageRes); 165 | mSeekBar.setProgress(0); 166 | break; 167 | case R.id.anim_btn: 168 | endAnimators(); 169 | mRoundAnimator = ValueAnimator.ofInt(mSeekBar.getProgress(), 100, mSeekBar.getProgress()); 170 | mRoundAnimator.setInterpolator(new LinearInterpolator()); 171 | mRoundAnimator.addUpdateListener(animation -> { 172 | mSeekBar.setProgress((int) animation.getAnimatedValue()); 173 | final int radius = (int) ((int) animation.getAnimatedValue() / 100f * 25); 174 | updateImage(radius); 175 | }); 176 | mRoundAnimator.setDuration(2000); 177 | mRoundAnimator.start(); 178 | break; 179 | case R.id.photo: 180 | mCurrentImageRes = TEST_IMAGE_RES[++index % TEST_IMAGE_RES.length]; 181 | setImage(mCurrentImageRes); 182 | break; 183 | } 184 | 185 | 186 | } 187 | 188 | @Override 189 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 190 | final int radius = (int) (progress / 100f * 25); 191 | mRadiusText.setText("Blur Radius: " + radius); 192 | updateImage(radius); 193 | } 194 | 195 | @Override 196 | public void onStartTrackingTouch(SeekBar seekBar) { 197 | 198 | } 199 | 200 | @Override 201 | public void onStopTrackingTouch(SeekBar seekBar) { 202 | 203 | } 204 | 205 | private void updateImage(int radius) { 206 | mRadius = radius; 207 | cancelPreTask(); 208 | mFuture = mDispatcher.submit(new BlurTask(mInBitmap, mProcessor, radius) { 209 | @Override 210 | void onBlurSuccess(final Bitmap bitmap) { 211 | if (!isFinishing() && bitmap != null) { 212 | runOnUiThread(() -> mImageView.setImageBitmap(bitmap)); 213 | } 214 | } 215 | }); 216 | } 217 | 218 | private void cancelPreTask() { 219 | if (mFuture != null && !mFuture.isCancelled() && !mFuture.isDone()) { 220 | mFuture.cancel(false); 221 | mFuture = null; 222 | } 223 | } 224 | 225 | private void endAnimators() { 226 | if (mAnimator != null && mAnimator.isStarted()) { 227 | mAnimator.end(); 228 | } 229 | if (mRoundAnimator != null && mRoundAnimator.isStarted()) { 230 | mRoundAnimator.end(); 231 | } 232 | } 233 | 234 | @Override 235 | protected void onDestroy() { 236 | super.onDestroy(); 237 | cancelPreTask(); 238 | } 239 | 240 | private abstract static class BlurTask implements Runnable { 241 | private Bitmap bitmap; 242 | private IBlurProcessor blurProcessor; 243 | private int radius; 244 | 245 | BlurTask(Bitmap bitmap, IBlurProcessor blurProcessor, int radius) { 246 | this.bitmap = bitmap; 247 | this.blurProcessor = blurProcessor; 248 | this.radius = radius; 249 | } 250 | 251 | @Override 252 | public void run() { 253 | if (bitmap != null && !bitmap.isRecycled() && blurProcessor != null) { 254 | blurProcessor.radius(radius); 255 | onBlurSuccess(blurProcessor.blur(bitmap)); 256 | } 257 | } 258 | 259 | abstract void onBlurSuccess(Bitmap bitmap); 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/hokoblurdemo/view/DragBlurringView.java: -------------------------------------------------------------------------------- 1 | package com.example.hokoblurdemo.view; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.drawable.ColorDrawable; 8 | import android.util.AttributeSet; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | 12 | import com.hoko.blur.HokoBlur; 13 | import com.hoko.blur.api.IBlurProcessor; 14 | 15 | 16 | /** 17 | * Created by yuxfzju on 16/8/20. 18 | */ 19 | public class DragBlurringView extends View { 20 | 21 | private static final int DOWNSAMPLE_FACTOR = 5; 22 | 23 | private float mOldX; 24 | private float mOldY; 25 | 26 | private View mBlurredView; 27 | 28 | private Bitmap mToBlurBitmap; 29 | private Canvas mBlurringCanvas; 30 | private IBlurProcessor mProcessor; 31 | 32 | public DragBlurringView(Context context) { 33 | super(context); 34 | init(); 35 | } 36 | 37 | public DragBlurringView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | init(); 40 | } 41 | 42 | 43 | private void init() { 44 | mProcessor = HokoBlur.with(getContext()) 45 | .scheme(HokoBlur.SCHEME_NATIVE) 46 | .mode(HokoBlur.MODE_GAUSSIAN) 47 | .radius(5) 48 | .sampleFactor(1.0f) 49 | .processor(); 50 | } 51 | 52 | @Override 53 | protected void onDraw(Canvas canvas) { 54 | super.onDraw(canvas); 55 | 56 | if (mBlurredView != null) { 57 | if (prepare()) { 58 | if (mBlurredView.getBackground() != null && mBlurredView.getBackground() instanceof ColorDrawable) { 59 | mToBlurBitmap.eraseColor(((ColorDrawable) mBlurredView.getBackground()).getColor()); 60 | } else { 61 | mToBlurBitmap.eraseColor(Color.TRANSPARENT); 62 | } 63 | mBlurredView.draw(mBlurringCanvas); 64 | Bitmap blurredBitmap = mProcessor.blur(mToBlurBitmap); 65 | canvas.save(); 66 | canvas.translate(mBlurredView.getX() - getX(), mBlurredView.getY() - getY()); 67 | canvas.scale(DOWNSAMPLE_FACTOR, DOWNSAMPLE_FACTOR); 68 | canvas.drawBitmap(blurredBitmap, 0, 0, null); 69 | canvas.restore(); 70 | } 71 | 72 | } 73 | 74 | } 75 | 76 | private boolean prepare() { 77 | final int width = mBlurredView.getWidth(); 78 | final int height = mBlurredView.getHeight(); 79 | if (mBlurringCanvas == null) { 80 | int scaledWidth = width / DOWNSAMPLE_FACTOR; 81 | int scaleHeight = height / DOWNSAMPLE_FACTOR; 82 | if (mToBlurBitmap == null) { 83 | mToBlurBitmap = Bitmap.createBitmap(scaledWidth, scaleHeight, Bitmap.Config.ARGB_8888); 84 | } 85 | if (mToBlurBitmap == null) { 86 | return false; 87 | } 88 | mBlurringCanvas = new Canvas(mToBlurBitmap); 89 | mBlurringCanvas.scale(1.0f / DOWNSAMPLE_FACTOR, 1.0f / DOWNSAMPLE_FACTOR); 90 | } 91 | return true; 92 | } 93 | 94 | 95 | public void setBlurredView(View blurredView) { 96 | mBlurredView = blurredView; 97 | } 98 | 99 | @Override 100 | public boolean onTouchEvent(MotionEvent event) { 101 | switch (event.getActionMasked()) { 102 | case MotionEvent.ACTION_DOWN: 103 | mOldX = event.getRawX(); 104 | mOldY = event.getRawY(); 105 | return true; 106 | case MotionEvent.ACTION_MOVE: 107 | float dx = event.getRawX() - mOldX; 108 | float dy = event.getRawY() - mOldY; 109 | offsetLeftAndRight((int) dx); 110 | offsetTopAndBottom((int) dy); 111 | mOldX = event.getRawX(); 112 | mOldY = event.getRawY(); 113 | invalidate(); 114 | break; 115 | case MotionEvent.ACTION_CANCEL: 116 | case MotionEvent.ACTION_UP: 117 | break; 118 | } 119 | return super.onTouchEvent(event); 120 | } 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HokoFly/HokoBlur/5cdd7890f3aa3e2230b4c1b96419762a0e878601/app/src/main/res/drawable-xxhdpi/cat.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HokoFly/HokoBlur/5cdd7890f3aa3e2230b4c1b96419762a0e878601/app/src/main/res/drawable-xxhdpi/dog.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/sample1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HokoFly/HokoBlur/5cdd7890f3aa3e2230b4c1b96419762a0e878601/app/src/main/res/drawable-xxhdpi/sample1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/sample2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HokoFly/HokoBlur/5cdd7890f3aa3e2230b4c1b96419762a0e878601/app/src/main/res/drawable-xxhdpi/sample2.jpeg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_dynamic_blur.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | 21 | 22 | 29 | 30 | 31 | 32 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_easy_blur.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 17 | 18 | 22 | 23 | 27 | 28 | 32 | 33 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 |