├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ └── issue-template.md ├── .gitignore ├── BlurScreenshot.png ├── LICENSE.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── eightbitlab │ │ └── blurview_sample │ │ ├── BaseFragment.java │ │ ├── ExampleListAdapter.java │ │ ├── ImageFragment.java │ │ ├── ListFragment.java │ │ ├── MainActivity.java │ │ ├── ScrollFragment.java │ │ └── SeekBarListenerAdapter.java │ ├── list_item_thumb-web.png │ └── res │ ├── drawable │ ├── face.jpg │ ├── fire.jpg │ ├── lines.jpg │ ├── marine.jpg │ ├── rounded.xml │ └── trees.jpg │ ├── layout │ ├── activity_main.xml │ ├── fragment_image.xml │ ├── fragment_list.xml │ ├── fragment_scroll.xml │ └── list_item.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── list_item_thumb.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── list_item_thumb.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── list_item_thumb.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── list_item_thumb.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── list_item_thumb.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── eightbitlab │ │ │ └── com │ │ │ └── blurview │ │ │ ├── BlurAlgorithm.java │ │ │ ├── BlurController.java │ │ │ ├── BlurView.java │ │ │ ├── BlurViewCanvas.java │ │ │ ├── BlurViewFacade.java │ │ │ ├── NoOpController.java │ │ │ ├── PreDrawBlurController.java │ │ │ ├── RenderEffectBlur.java │ │ │ ├── RenderScriptBlur.java │ │ │ └── SizeScaler.java │ └── res │ │ └── values │ │ └── styles.xml │ └── test │ └── java │ └── eightbitlab │ └── com │ └── blurview │ └── SizeScalerTest.java └── settings.gradle /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ["https://paypal.me/dimezis"] 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/issue-template.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Issue template 3 | about: Bug report 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | Please include: 11 | 1) Library version 12 | 2) Device and OS version 13 | 3) Detailed steps to reproduce the issue 14 | 4) XML layout and code for BlurView setup 15 | 5) Stacktrace in case of a crash 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea 3 | .gradle 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | -------------------------------------------------------------------------------- /BlurScreenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/BlurScreenshot.png -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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 | [![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine) 2 | 3 | # BlurView 4 | 5 | 6 | 7 | Dynamic iOS-like blur for Android Views. Includes library and small example project. 8 | 9 | BlurView can be used as a regular FrameLayout. It blurs its underlying content and draws it as a 10 | background for its children. The children of the BlurView are not blurred. BlurView redraws its 11 | blurred content when changes in view hierarchy are detected (draw() called). It honors its position 12 | and size changes, including view animation and property animation. 13 | 14 | ## How to use 15 | ```XML 16 | 21 | 22 | 23 | 24 | 25 | ``` 26 | 27 | ```Java 28 | float radius = 20f; 29 | 30 | View decorView = getWindow().getDecorView(); 31 | // ViewGroup you want to start blur from. Choose root as close to BlurView in hierarchy as possible. 32 | ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content); 33 | 34 | // Optional: 35 | // Set drawable to draw in the beginning of each blurred frame. 36 | // Can be used in case your layout has a lot of transparent space and your content 37 | // gets a too low alpha value after blur is applied. 38 | Drawable windowBackground = decorView.getBackground(); 39 | 40 | blurView.setupWith(rootView) // Optionally pass RenderEffectBlur or RenderScriptBlur as the second parameter 41 | .setFrameClearDrawable(windowBackground) // Optional. Useful when your root has a lot of transparent background, which results in semi-transparent blurred content. This will make the background opaque 42 | .setBlurRadius(radius) 43 | ``` 44 | 45 | Always try to choose the closest possible root layout to BlurView. This will greatly reduce the amount of work needed for creating View hierarchy snapshot. 46 | 47 | ## SurfaceView, TextureView, VideoView, MapFragment, GLSurfaceView, etc 48 | BlurView currently doesn't support blurring of these targets, because they work only with hardware-accelerated Canvas, and BlurView relies on a software Canvas to make a snapshot of Views to blur. 49 | 50 | ## Gradle 51 | 52 | Since JCenter is closing, please use https://jitpack.io/ and release tags as the source of stable 53 | artifacts. 54 | ```Groovy 55 | implementation 'com.github.Dimezis:BlurView:version-2.0.6' 56 | ``` 57 | 58 | ## Rounded corners 59 | It's possible to set rounded corners without any custom API, the algorithm is the same as with other regular View: 60 | 61 | Create a rounded drawable, and set it as a background. 62 | 63 | Then set up the clipping, so the BlurView doesn't draw outside the corners 64 | ```Java 65 | blurView.setOutlineProvider(ViewOutlineProvider.BACKGROUND); 66 | blurView.setClipToOutline(true); 67 | ``` 68 | Related thread - https://github.com/Dimezis/BlurView/issues/37 69 | 70 | ## Why blurring on the main thread? 71 | Because blurring on other threads would introduce 1-2 frames of latency. 72 | For what it's worth, `RenderEffectBlur` performs blur on the Render Thread. 73 | 74 | ## Compared to other blurring libs 75 | - The main advantage of BlurView over almost any other library is that it doesn't trigger redundant redraw. 76 | - The BlurView never invalidates itself or other Views in the hierarchy and updates only when needed relying on just a Bitmap mutation, which is recorded on a hardware-accelerated canvas. 77 | - It supports multiple BlurViews on the screen without triggering a draw loop. 78 | - It uses optimized RenderScript Allocations on devices that require certain Allocation sizes, which greatly increases blur performance. 79 | - It allows choosing a custom root view to take a snapshot from, which reduces the amount of drawing traversals and allows greater flexibility. 80 | - Supports blurring of Dialogs (and Dialog's background) 81 | 82 | Other libs: 83 | - 🛑 [BlurKit](https://github.com/CameraKit/blurkit-android) - constantly invalidates itself 84 | - 🛑 [RealtimeBlurView](https://github.com/mmin18/RealtimeBlurView) - constantly invalidates itself 85 | 86 | License 87 | ------- 88 | 89 | Copyright 2024 Dmytro Saviuk 90 | 91 | Licensed under the Apache License, Version 2.0 (the "License"); 92 | you may not use this file except in compliance with the License. 93 | You may obtain a copy of the License at 94 | 95 | http://www.apache.org/licenses/LICENSE-2.0 96 | 97 | Unless required by applicable law or agreed to in writing, software 98 | distributed under the License is distributed on an "AS IS" BASIS, 99 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 100 | See the License for the specific language governing permissions and 101 | limitations under the License. 102 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 31 5 | 6 | defaultConfig { 7 | applicationId "com.eightbitlab.blurview_sample" 8 | minSdkVersion 19 9 | targetSdkVersion 31 10 | versionCode 3 11 | versionName "1.1" 12 | } 13 | 14 | compileOptions { 15 | sourceCompatibility JavaVersion.VERSION_1_8 16 | targetCompatibility JavaVersion.VERSION_1_8 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | ext { 28 | supportLib = "28.0.0" 29 | } 30 | 31 | dependencies { 32 | implementation 'androidx.appcompat:appcompat:1.4.1' 33 | implementation 'androidx.cardview:cardview:1.0.0' 34 | implementation 'com.google.android.material:material:1.5.0' 35 | implementation 'androidx.recyclerview:recyclerview:1.2.1' 36 | 37 | implementation project(':library') 38 | } 39 | -------------------------------------------------------------------------------- /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 F:\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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.LayoutRes; 5 | import androidx.annotation.NonNull; 6 | import androidx.annotation.Nullable; 7 | import androidx.fragment.app.Fragment; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | public abstract class BaseFragment extends Fragment { 13 | 14 | @LayoutRes 15 | abstract int getLayoutId(); 16 | 17 | @Nullable 18 | @Override 19 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 20 | return inflater.inflate(getLayoutId(), container, false); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/ExampleListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | import android.content.Context; 4 | import androidx.annotation.NonNull; 5 | import androidx.recyclerview.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | public class ExampleListAdapter extends RecyclerView.Adapter { 11 | 12 | private static final int ITEMS_COUNT = 64; 13 | private LayoutInflater inflater; 14 | 15 | ExampleListAdapter(Context context) { 16 | inflater = LayoutInflater.from(context); 17 | } 18 | 19 | @Override 20 | @NonNull 21 | public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 22 | return new Holder(inflater.inflate(R.layout.list_item, parent, false)); 23 | } 24 | 25 | @Override 26 | public void onBindViewHolder(@NonNull Holder holder, int position) { 27 | } 28 | 29 | @Override 30 | public int getItemCount() { 31 | return ITEMS_COUNT; 32 | } 33 | 34 | static class Holder extends RecyclerView.ViewHolder { 35 | Holder(View itemView) { 36 | super(itemView); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/ImageFragment.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | public class ImageFragment extends BaseFragment { 4 | @Override 5 | int getLayoutId() { 6 | return R.layout.fragment_image; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/ListFragment.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.Nullable; 5 | import androidx.recyclerview.widget.LinearLayoutManager; 6 | import androidx.recyclerview.widget.RecyclerView; 7 | import android.view.View; 8 | 9 | public class ListFragment extends BaseFragment { 10 | 11 | @Override 12 | int getLayoutId() { 13 | return R.layout.fragment_list; 14 | } 15 | 16 | @Override 17 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 18 | super.onViewCreated(view, savedInstanceState); 19 | init(); 20 | } 21 | 22 | private void init() { 23 | RecyclerView recyclerView = getView().findViewById(R.id.recyclerView); 24 | recyclerView.setAdapter(new ExampleListAdapter(getContext())); 25 | recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | import android.graphics.Outline; 4 | import android.graphics.drawable.Drawable; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.view.ViewOutlineProvider; 10 | import android.widget.SeekBar; 11 | 12 | import androidx.annotation.NonNull; 13 | import androidx.appcompat.app.AppCompatActivity; 14 | import androidx.fragment.app.Fragment; 15 | import androidx.fragment.app.FragmentManager; 16 | import androidx.fragment.app.FragmentPagerAdapter; 17 | import androidx.viewpager.widget.ViewPager; 18 | 19 | import com.google.android.material.tabs.TabLayout; 20 | 21 | import eightbitlab.com.blurview.BlurAlgorithm; 22 | import eightbitlab.com.blurview.BlurView; 23 | import eightbitlab.com.blurview.RenderEffectBlur; 24 | import eightbitlab.com.blurview.RenderScriptBlur; 25 | 26 | public class MainActivity extends AppCompatActivity { 27 | 28 | private ViewPager viewPager; 29 | private TabLayout tabLayout; 30 | private BlurView bottomBlurView; 31 | private BlurView topBlurView; 32 | private SeekBar radiusSeekBar; 33 | private ViewGroup root; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_main); 39 | initView(); 40 | setupBlurView(); 41 | setupViewPager(); 42 | } 43 | 44 | private void initView() { 45 | viewPager = findViewById(R.id.viewPager); 46 | tabLayout = findViewById(R.id.tabLayout); 47 | bottomBlurView = findViewById(R.id.bottomBlurView); 48 | topBlurView = findViewById(R.id.topBlurView); 49 | // Rounded corners + casting elevation shadow with transparent background 50 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 51 | topBlurView.setClipToOutline(true); 52 | topBlurView.setOutlineProvider(new ViewOutlineProvider() { 53 | @Override 54 | public void getOutline(View view, Outline outline) { 55 | topBlurView.getBackground().getOutline(outline); 56 | outline.setAlpha(1f); 57 | } 58 | }); 59 | } 60 | radiusSeekBar = findViewById(R.id.radiusSeekBar); 61 | root = findViewById(R.id.root); 62 | } 63 | 64 | private void setupViewPager() { 65 | viewPager.setOffscreenPageLimit(2); 66 | viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 67 | tabLayout.setupWithViewPager(viewPager); 68 | } 69 | 70 | private void setupBlurView() { 71 | final float radius = 25f; 72 | final float minBlurRadius = 4f; 73 | final float step = 4f; 74 | 75 | //set background, if your root layout doesn't have one 76 | final Drawable windowBackground = getWindow().getDecorView().getBackground(); 77 | BlurAlgorithm algorithm = getBlurAlgorithm(); 78 | 79 | topBlurView.setupWith(root, algorithm) 80 | .setFrameClearDrawable(windowBackground) 81 | .setBlurRadius(radius); 82 | 83 | bottomBlurView.setupWith(root, new RenderScriptBlur(this)) 84 | .setFrameClearDrawable(windowBackground) 85 | .setBlurRadius(radius); 86 | 87 | int initialProgress = (int) (radius * step); 88 | radiusSeekBar.setProgress(initialProgress); 89 | 90 | radiusSeekBar.setOnSeekBarChangeListener(new SeekBarListenerAdapter() { 91 | @Override 92 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 93 | float blurRadius = progress / step; 94 | blurRadius = Math.max(blurRadius, minBlurRadius); 95 | topBlurView.setBlurRadius(blurRadius); 96 | bottomBlurView.setBlurRadius(blurRadius); 97 | } 98 | }); 99 | } 100 | 101 | @NonNull 102 | private BlurAlgorithm getBlurAlgorithm() { 103 | BlurAlgorithm algorithm; 104 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { 105 | algorithm = new RenderEffectBlur(); 106 | } else { 107 | algorithm = new RenderScriptBlur(this); 108 | } 109 | return algorithm; 110 | } 111 | 112 | static class ViewPagerAdapter extends FragmentPagerAdapter { 113 | 114 | ViewPagerAdapter(FragmentManager fragmentManager) { 115 | super(fragmentManager); 116 | } 117 | 118 | @Override 119 | public Fragment getItem(int position) { 120 | return Page.values()[position].getFragment(); 121 | } 122 | 123 | @Override 124 | public CharSequence getPageTitle(int position) { 125 | return Page.values()[position].getTitle(); 126 | } 127 | 128 | @Override 129 | public int getCount() { 130 | return Page.values().length; 131 | } 132 | } 133 | 134 | enum Page { 135 | FIRST("ScrollView") { 136 | @Override 137 | Fragment getFragment() { 138 | return new ScrollFragment(); 139 | } 140 | }, 141 | SECOND("RecyclerView") { 142 | @Override 143 | Fragment getFragment() { 144 | return new ListFragment(); 145 | } 146 | }, 147 | THIRD("Static") { 148 | @Override 149 | Fragment getFragment() { 150 | return new ImageFragment(); 151 | } 152 | }; 153 | 154 | private String title; 155 | 156 | Page(String title) { 157 | this.title = title; 158 | } 159 | 160 | String getTitle() { 161 | return title; 162 | } 163 | 164 | abstract Fragment getFragment(); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/ScrollFragment.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | public class ScrollFragment extends BaseFragment { 4 | @Override 5 | int getLayoutId() { 6 | return R.layout.fragment_scroll; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/eightbitlab/blurview_sample/SeekBarListenerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.eightbitlab.blurview_sample; 2 | 3 | import android.widget.SeekBar; 4 | 5 | public abstract class SeekBarListenerAdapter implements SeekBar.OnSeekBarChangeListener { 6 | @Override 7 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 8 | } 9 | 10 | @Override 11 | public void onStartTrackingTouch(SeekBar seekBar) { 12 | } 13 | 14 | @Override 15 | public void onStopTrackingTouch(SeekBar seekBar) { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/list_item_thumb-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/list_item_thumb-web.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/face.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/drawable/face.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/fire.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/drawable/fire.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/lines.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/drawable/lines.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/marine.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/drawable/marine.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/rounded.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/trees.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/drawable/trees.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 24 | 25 | 30 | 31 | 36 | 37 | 42 | 43 | 44 | 45 | 46 | 53 | 54 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_image.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_scroll.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 28 | 29 | 35 | 36 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/res/layout/list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 18 | 19 | 26 | 27 | 33 | 34 | 42 | 43 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/list_item_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-hdpi/list_item_thumb.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/list_item_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-mdpi/list_item_thumb.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/list_item_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xhdpi/list_item_thumb.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/list_item_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xxhdpi/list_item_thumb.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/list_item_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/app/src/main/res/mipmap-xxxhdpi/list_item_thumb.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #00BCD4 4 | #0097A7 5 | #009688 6 | #78ffffff 7 | #1400bcd4 8 | #14000000 9 | #6400bcd4 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 80dp 7 | 8dp 8 | 4dp 9 | 64dp 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | BlurView 3 | Blur me please! 4 | Some Long Description Text\nSome Long Description Text 5 | Strong slogan here 6 | Radius 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | google() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:7.1.2' 8 | classpath("de.mannodermaus.gradle.plugins:android-junit5:1.8.2.0") 9 | } 10 | } 11 | 12 | allprojects { 13 | repositories { 14 | jcenter() 15 | google() 16 | } 17 | } 18 | 19 | task clean(type: Delete) { 20 | delete rootProject.buildDir 21 | } 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | android.useAndroidX=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dimezis/BlurView/da23c46bc8b97265c218f7383cbba133642cef15/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Aug 08 12:56:13 CEST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'de.mannodermaus.android-junit5' 3 | 4 | android { 5 | compileSdkVersion 31 6 | 7 | compileOptions { 8 | sourceCompatibility JavaVersion.VERSION_1_8 9 | targetCompatibility JavaVersion.VERSION_1_8 10 | } 11 | 12 | defaultConfig { 13 | minSdkVersion 18 14 | targetSdkVersion 31 15 | } 16 | } 17 | 18 | configurations { 19 | javadocDeps 20 | } 21 | 22 | dependencies { 23 | implementation 'androidx.annotation:annotation:1.3.0' 24 | javadocDeps 'androidx.annotation:annotation:1.3.0' 25 | 26 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' 27 | testImplementation("org.junit.jupiter:junit-jupiter-params:5.8.2") 28 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' 29 | } 30 | 31 | task androidJavadocs(type: Javadoc) { 32 | failOnError = false 33 | source = android.sourceSets.main.java.srcDirs 34 | ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar" 35 | classpath += files(ext.androidJar) 36 | } 37 | 38 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 39 | classifier = 'javadoc' 40 | from androidJavadocs.destinationDir 41 | } 42 | 43 | task androidSourcesJar(type: Jar) { 44 | classifier = 'sources' 45 | from android.sourceSets.main.java.srcDirs 46 | } 47 | -------------------------------------------------------------------------------- /library/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 C:\Java\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 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/BlurAlgorithm.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | 6 | import androidx.annotation.NonNull; 7 | 8 | public interface BlurAlgorithm { 9 | /** 10 | * @param bitmap bitmap to be blurred 11 | * @param blurRadius blur radius 12 | * @return blurred bitmap 13 | */ 14 | Bitmap blur(@NonNull Bitmap bitmap, @NonNull float blurRadius); 15 | 16 | /** 17 | * Frees allocated resources 18 | */ 19 | void destroy(); 20 | 21 | /** 22 | * @return true if this algorithm returns the same instance of bitmap as it accepted 23 | * false if it creates a new instance. 24 | *

25 | * If you return false from this method, you'll be responsible to swap bitmaps in your 26 | * {@link BlurAlgorithm#blur(Bitmap, float)} implementation 27 | * (assign input bitmap to your field and return the instance algorithm just blurred). 28 | */ 29 | boolean canModifyBitmap(); 30 | 31 | /** 32 | * Retrieve the {@link android.graphics.Bitmap.Config} on which the {@link BlurAlgorithm} 33 | * can actually work. 34 | * 35 | * @return bitmap config supported by the given blur algorithm. 36 | */ 37 | @NonNull 38 | Bitmap.Config getSupportedBitmapConfig(); 39 | 40 | float scaleFactor(); 41 | 42 | void render(@NonNull Canvas canvas, @NonNull Bitmap bitmap); 43 | } 44 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/BlurController.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.Canvas; 4 | 5 | public interface BlurController extends BlurViewFacade { 6 | 7 | float DEFAULT_SCALE_FACTOR = 6f; 8 | float DEFAULT_BLUR_RADIUS = 16f; 9 | 10 | /** 11 | * Draws blurred content on given canvas 12 | * 13 | * @return true if BlurView should proceed with drawing itself and its children 14 | */ 15 | boolean draw(Canvas canvas); 16 | 17 | /** 18 | * Must be used to notify Controller when BlurView's size has changed 19 | */ 20 | void updateBlurViewSize(); 21 | 22 | /** 23 | * Frees allocated resources 24 | */ 25 | void destroy(); 26 | } 27 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/BlurView.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import static eightbitlab.com.blurview.PreDrawBlurController.TRANSPARENT; 4 | 5 | import android.content.Context; 6 | import android.content.res.TypedArray; 7 | import android.graphics.Canvas; 8 | import android.os.Build; 9 | import android.util.AttributeSet; 10 | import android.util.Log; 11 | import android.view.ViewGroup; 12 | import android.widget.FrameLayout; 13 | 14 | import androidx.annotation.ColorInt; 15 | import androidx.annotation.NonNull; 16 | import androidx.annotation.RequiresApi; 17 | 18 | /** 19 | * FrameLayout that blurs its underlying content. 20 | * Can have children and draw them over blurred background. 21 | */ 22 | public class BlurView extends FrameLayout { 23 | 24 | private static final String TAG = BlurView.class.getSimpleName(); 25 | 26 | BlurController blurController = new NoOpController(); 27 | 28 | @ColorInt 29 | private int overlayColor; 30 | private boolean blurAutoUpdate = true; 31 | 32 | public BlurView(Context context) { 33 | super(context); 34 | init(null, 0); 35 | } 36 | 37 | public BlurView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | init(attrs, 0); 40 | } 41 | 42 | public BlurView(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | init(attrs, defStyleAttr); 45 | } 46 | 47 | private void init(AttributeSet attrs, int defStyleAttr) { 48 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BlurView, defStyleAttr, 0); 49 | overlayColor = a.getColor(R.styleable.BlurView_blurOverlayColor, TRANSPARENT); 50 | a.recycle(); 51 | } 52 | 53 | @Override 54 | public void draw(Canvas canvas) { 55 | boolean shouldDraw = blurController.draw(canvas); 56 | if (shouldDraw) { 57 | super.draw(canvas); 58 | } 59 | } 60 | 61 | @Override 62 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 63 | super.onSizeChanged(w, h, oldw, oldh); 64 | blurController.updateBlurViewSize(); 65 | } 66 | 67 | @Override 68 | protected void onDetachedFromWindow() { 69 | super.onDetachedFromWindow(); 70 | blurController.setBlurAutoUpdate(false); 71 | } 72 | 73 | @Override 74 | protected void onAttachedToWindow() { 75 | super.onAttachedToWindow(); 76 | if (!isHardwareAccelerated()) { 77 | Log.e(TAG, "BlurView can't be used in not hardware-accelerated window!"); 78 | } else { 79 | blurController.setBlurAutoUpdate(this.blurAutoUpdate); 80 | } 81 | } 82 | 83 | /** 84 | * @param rootView root to start blur from. 85 | * Can be Activity's root content layout (android.R.id.content) 86 | * or (preferably) some of your layouts. The lower amount of Views are in the root, the better for performance. 87 | * @param algorithm sets the blur algorithm 88 | * @return {@link BlurView} to setup needed params. 89 | */ 90 | public BlurViewFacade setupWith(@NonNull ViewGroup rootView, BlurAlgorithm algorithm) { 91 | this.blurController.destroy(); 92 | BlurController blurController = new PreDrawBlurController(this, rootView, overlayColor, algorithm); 93 | this.blurController = blurController; 94 | 95 | return blurController; 96 | } 97 | 98 | /** 99 | * @param rootView root to start blur from. 100 | * Can be Activity's root content layout (android.R.id.content) 101 | * or (preferably) some of your layouts. The lower amount of Views are in the root, the better for performance. 102 | *

103 | * BlurAlgorithm is automatically picked based on the API version. 104 | * It uses RenderEffectBlur on API 31+, and RenderScriptBlur on older versions. 105 | * @return {@link BlurView} to setup needed params. 106 | */ 107 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 108 | public BlurViewFacade setupWith(@NonNull ViewGroup rootView) { 109 | return setupWith(rootView, getBlurAlgorithm()); 110 | } 111 | 112 | // Setters duplicated to be able to conveniently change these settings outside of setupWith chain 113 | 114 | /** 115 | * @see BlurViewFacade#setBlurRadius(float) 116 | */ 117 | public BlurViewFacade setBlurRadius(float radius) { 118 | return blurController.setBlurRadius(radius); 119 | } 120 | 121 | /** 122 | * @see BlurViewFacade#setOverlayColor(int) 123 | */ 124 | public BlurViewFacade setOverlayColor(@ColorInt int overlayColor) { 125 | this.overlayColor = overlayColor; 126 | return blurController.setOverlayColor(overlayColor); 127 | } 128 | 129 | /** 130 | * @see BlurViewFacade#setBlurAutoUpdate(boolean) 131 | */ 132 | public BlurViewFacade setBlurAutoUpdate(boolean enabled) { 133 | this.blurAutoUpdate = enabled; 134 | return blurController.setBlurAutoUpdate(enabled); 135 | } 136 | 137 | /** 138 | * @see BlurViewFacade#setBlurEnabled(boolean) 139 | */ 140 | public BlurViewFacade setBlurEnabled(boolean enabled) { 141 | return blurController.setBlurEnabled(enabled); 142 | } 143 | 144 | @NonNull 145 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 146 | private BlurAlgorithm getBlurAlgorithm() { 147 | BlurAlgorithm algorithm; 148 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { 149 | algorithm = new RenderEffectBlur(); 150 | } else { 151 | algorithm = new RenderScriptBlur(getContext()); 152 | } 153 | return algorithm; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/BlurViewCanvas.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | 6 | import androidx.annotation.NonNull; 7 | 8 | // Serves purely as a marker of a Canvas used in BlurView 9 | // to skip drawing itself and other BlurViews on the View hierarchy snapshot 10 | public class BlurViewCanvas extends Canvas { 11 | public BlurViewCanvas(@NonNull Bitmap bitmap) { 12 | super(bitmap); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/BlurViewFacade.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.drawable.Drawable; 4 | 5 | import androidx.annotation.ColorInt; 6 | import androidx.annotation.Nullable; 7 | 8 | public interface BlurViewFacade { 9 | 10 | /** 11 | * Enables/disables the blur. Enabled by default 12 | * 13 | * @param enabled true to enable, false otherwise 14 | * @return {@link BlurViewFacade} 15 | */ 16 | BlurViewFacade setBlurEnabled(boolean enabled); 17 | 18 | /** 19 | * Can be used to stop blur auto update or resume if it was stopped before. 20 | * Enabled by default. 21 | * 22 | * @return {@link BlurViewFacade} 23 | */ 24 | BlurViewFacade setBlurAutoUpdate(boolean enabled); 25 | 26 | /** 27 | * @param frameClearDrawable sets the drawable to draw before view hierarchy. 28 | * Can be used to draw Activity's window background if your root layout doesn't provide any background 29 | * Optional, by default frame is cleared with a transparent color. 30 | * @return {@link BlurViewFacade} 31 | */ 32 | BlurViewFacade setFrameClearDrawable(@Nullable Drawable frameClearDrawable); 33 | 34 | /** 35 | * @param radius sets the blur radius 36 | * Default value is {@link BlurController#DEFAULT_BLUR_RADIUS} 37 | * @return {@link BlurViewFacade} 38 | */ 39 | BlurViewFacade setBlurRadius(float radius); 40 | 41 | /** 42 | * Sets the color overlay to be drawn on top of blurred content 43 | * 44 | * @param overlayColor int color 45 | * @return {@link BlurViewFacade} 46 | */ 47 | BlurViewFacade setOverlayColor(@ColorInt int overlayColor); 48 | } 49 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/NoOpController.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.Canvas; 4 | import android.graphics.drawable.Drawable; 5 | 6 | import androidx.annotation.Nullable; 7 | 8 | // Used in edit mode and in case if no BlurController was set 9 | public class NoOpController implements BlurController { 10 | @Override 11 | public boolean draw(Canvas canvas) { 12 | return true; 13 | } 14 | 15 | @Override 16 | public void updateBlurViewSize() { 17 | } 18 | 19 | @Override 20 | public void destroy() { 21 | } 22 | 23 | @Override 24 | public BlurViewFacade setBlurRadius(float radius) { 25 | return this; 26 | } 27 | 28 | @Override 29 | public BlurViewFacade setOverlayColor(int overlayColor) { 30 | return this; 31 | } 32 | 33 | @Override 34 | public BlurViewFacade setFrameClearDrawable(@Nullable Drawable windowBackground) { 35 | return this; 36 | } 37 | 38 | @Override 39 | public BlurViewFacade setBlurEnabled(boolean enabled) { 40 | return this; 41 | } 42 | 43 | @Override 44 | public BlurViewFacade setBlurAutoUpdate(boolean enabled) { 45 | return this; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/PreDrawBlurController.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.Drawable; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.view.ViewTreeObserver; 10 | 11 | import androidx.annotation.ColorInt; 12 | import androidx.annotation.NonNull; 13 | import androidx.annotation.Nullable; 14 | 15 | /** 16 | * Blur Controller that handles all blur logic for the attached View. 17 | * It honors View size changes, View animation and Visibility changes. 18 | *

19 | * The basic idea is to draw the view hierarchy on a bitmap, excluding the attached View, 20 | * then blur and draw it on the system Canvas. 21 | *

22 | * It uses {@link ViewTreeObserver.OnPreDrawListener} to detect when 23 | * blur should be updated. 24 | *

25 | */ 26 | public final class PreDrawBlurController implements BlurController { 27 | 28 | @ColorInt 29 | public static final int TRANSPARENT = 0; 30 | 31 | private float blurRadius = DEFAULT_BLUR_RADIUS; 32 | 33 | private final BlurAlgorithm blurAlgorithm; 34 | private BlurViewCanvas internalCanvas; 35 | private Bitmap internalBitmap; 36 | 37 | @SuppressWarnings("WeakerAccess") 38 | final View blurView; 39 | private int overlayColor; 40 | private final ViewGroup rootView; 41 | private final int[] rootLocation = new int[2]; 42 | private final int[] blurViewLocation = new int[2]; 43 | 44 | private final ViewTreeObserver.OnPreDrawListener drawListener = new ViewTreeObserver.OnPreDrawListener() { 45 | @Override 46 | public boolean onPreDraw() { 47 | // Not invalidating a View here, just updating the Bitmap. 48 | // This relies on the HW accelerated bitmap drawing behavior in Android 49 | // If the bitmap was drawn on HW accelerated canvas, it holds a reference to it and on next 50 | // drawing pass the updated content of the bitmap will be rendered on the screen 51 | updateBlur(); 52 | return true; 53 | } 54 | }; 55 | 56 | private boolean blurEnabled = true; 57 | private boolean initialized; 58 | 59 | @Nullable 60 | private Drawable frameClearDrawable; 61 | 62 | /** 63 | * @param blurView View which will draw it's blurred underlying content 64 | * @param rootView Root View where blurView's underlying content starts drawing. 65 | * Can be Activity's root content layout (android.R.id.content) 66 | * @param algorithm sets the blur algorithm 67 | */ 68 | public PreDrawBlurController(@NonNull View blurView, @NonNull ViewGroup rootView, @ColorInt int overlayColor, BlurAlgorithm algorithm) { 69 | this.rootView = rootView; 70 | this.blurView = blurView; 71 | this.overlayColor = overlayColor; 72 | this.blurAlgorithm = algorithm; 73 | if (algorithm instanceof RenderEffectBlur) { 74 | // noinspection NewApi 75 | ((RenderEffectBlur) algorithm).setContext(blurView.getContext()); 76 | } 77 | 78 | int measuredWidth = blurView.getMeasuredWidth(); 79 | int measuredHeight = blurView.getMeasuredHeight(); 80 | 81 | init(measuredWidth, measuredHeight); 82 | } 83 | 84 | @SuppressWarnings("WeakerAccess") 85 | void init(int measuredWidth, int measuredHeight) { 86 | setBlurAutoUpdate(true); 87 | SizeScaler sizeScaler = new SizeScaler(blurAlgorithm.scaleFactor()); 88 | if (sizeScaler.isZeroSized(measuredWidth, measuredHeight)) { 89 | // Will be initialized later when the View reports a size change 90 | blurView.setWillNotDraw(true); 91 | return; 92 | } 93 | 94 | blurView.setWillNotDraw(false); 95 | SizeScaler.Size bitmapSize = sizeScaler.scale(measuredWidth, measuredHeight); 96 | internalBitmap = Bitmap.createBitmap(bitmapSize.width, bitmapSize.height, blurAlgorithm.getSupportedBitmapConfig()); 97 | internalCanvas = new BlurViewCanvas(internalBitmap); 98 | initialized = true; 99 | // Usually it's not needed, because `onPreDraw` updates the blur anyway. 100 | // But it handles cases when the PreDraw listener is attached to a different Window, for example 101 | // when the BlurView is in a Dialog window, but the root is in the Activity. 102 | // Previously it was done in `draw`, but it was causing potential side effects and Jetpack Compose crashes 103 | updateBlur(); 104 | } 105 | 106 | @SuppressWarnings("WeakerAccess") 107 | void updateBlur() { 108 | if (!blurEnabled || !initialized) { 109 | return; 110 | } 111 | 112 | if (frameClearDrawable == null) { 113 | internalBitmap.eraseColor(Color.TRANSPARENT); 114 | } else { 115 | frameClearDrawable.draw(internalCanvas); 116 | } 117 | 118 | internalCanvas.save(); 119 | setupInternalCanvasMatrix(); 120 | rootView.draw(internalCanvas); 121 | internalCanvas.restore(); 122 | 123 | blurAndSave(); 124 | } 125 | 126 | /** 127 | * Set up matrix to draw starting from blurView's position 128 | */ 129 | private void setupInternalCanvasMatrix() { 130 | rootView.getLocationOnScreen(rootLocation); 131 | blurView.getLocationOnScreen(blurViewLocation); 132 | 133 | int left = blurViewLocation[0] - rootLocation[0]; 134 | int top = blurViewLocation[1] - rootLocation[1]; 135 | 136 | // https://github.com/Dimezis/BlurView/issues/128 137 | float scaleFactorH = (float) blurView.getHeight() / internalBitmap.getHeight(); 138 | float scaleFactorW = (float) blurView.getWidth() / internalBitmap.getWidth(); 139 | 140 | float scaledLeftPosition = -left / scaleFactorW; 141 | float scaledTopPosition = -top / scaleFactorH; 142 | 143 | internalCanvas.translate(scaledLeftPosition, scaledTopPosition); 144 | internalCanvas.scale(1 / scaleFactorW, 1 / scaleFactorH); 145 | } 146 | 147 | @Override 148 | public boolean draw(Canvas canvas) { 149 | if (!blurEnabled || !initialized) { 150 | return true; 151 | } 152 | // Not blurring itself or other BlurViews to not cause recursive draw calls 153 | // Related: https://github.com/Dimezis/BlurView/issues/110 154 | if (canvas instanceof BlurViewCanvas) { 155 | return false; 156 | } 157 | 158 | // https://github.com/Dimezis/BlurView/issues/128 159 | float scaleFactorH = (float) blurView.getHeight() / internalBitmap.getHeight(); 160 | float scaleFactorW = (float) blurView.getWidth() / internalBitmap.getWidth(); 161 | 162 | canvas.save(); 163 | canvas.scale(scaleFactorW, scaleFactorH); 164 | blurAlgorithm.render(canvas, internalBitmap); 165 | canvas.restore(); 166 | if (overlayColor != TRANSPARENT) { 167 | canvas.drawColor(overlayColor); 168 | } 169 | return true; 170 | } 171 | 172 | private void blurAndSave() { 173 | internalBitmap = blurAlgorithm.blur(internalBitmap, blurRadius); 174 | if (!blurAlgorithm.canModifyBitmap()) { 175 | internalCanvas.setBitmap(internalBitmap); 176 | } 177 | } 178 | 179 | @Override 180 | public void updateBlurViewSize() { 181 | int measuredWidth = blurView.getMeasuredWidth(); 182 | int measuredHeight = blurView.getMeasuredHeight(); 183 | 184 | init(measuredWidth, measuredHeight); 185 | } 186 | 187 | @Override 188 | public void destroy() { 189 | setBlurAutoUpdate(false); 190 | blurAlgorithm.destroy(); 191 | initialized = false; 192 | } 193 | 194 | @Override 195 | public BlurViewFacade setBlurRadius(float radius) { 196 | this.blurRadius = radius; 197 | return this; 198 | } 199 | 200 | @Override 201 | public BlurViewFacade setFrameClearDrawable(@Nullable Drawable frameClearDrawable) { 202 | this.frameClearDrawable = frameClearDrawable; 203 | return this; 204 | } 205 | 206 | @Override 207 | public BlurViewFacade setBlurEnabled(boolean enabled) { 208 | this.blurEnabled = enabled; 209 | setBlurAutoUpdate(enabled); 210 | blurView.invalidate(); 211 | return this; 212 | } 213 | 214 | public BlurViewFacade setBlurAutoUpdate(final boolean enabled) { 215 | rootView.getViewTreeObserver().removeOnPreDrawListener(drawListener); 216 | blurView.getViewTreeObserver().removeOnPreDrawListener(drawListener); 217 | if (enabled) { 218 | rootView.getViewTreeObserver().addOnPreDrawListener(drawListener); 219 | // Track changes in the blurView window too, for example if it's in a bottom sheet dialog 220 | if (rootView.getWindowId() != blurView.getWindowId()) { 221 | blurView.getViewTreeObserver().addOnPreDrawListener(drawListener); 222 | } 223 | } 224 | return this; 225 | } 226 | 227 | @Override 228 | public BlurViewFacade setOverlayColor(int overlayColor) { 229 | if (this.overlayColor != overlayColor) { 230 | this.overlayColor = overlayColor; 231 | blurView.invalidate(); 232 | } 233 | return this; 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/RenderEffectBlur.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.RenderEffect; 7 | import android.graphics.RenderNode; 8 | import android.graphics.Shader; 9 | import android.os.Build; 10 | 11 | import androidx.annotation.NonNull; 12 | import androidx.annotation.Nullable; 13 | import androidx.annotation.RequiresApi; 14 | 15 | /** 16 | * Leverages the new RenderEffect.createBlurEffect API to perform blur. 17 | * Hardware accelerated. 18 | * Blur is performed on a separate thread - native RenderThread. 19 | * It doesn't block the Main thread, however it can still cause an FPS drop, 20 | * because it's just in a different part of the rendering pipeline. 21 | */ 22 | @RequiresApi(Build.VERSION_CODES.S) 23 | public class RenderEffectBlur implements BlurAlgorithm { 24 | 25 | private final RenderNode node = new RenderNode("BlurViewNode"); 26 | 27 | private int height, width; 28 | private float lastBlurRadius = 1f; 29 | 30 | @Nullable 31 | public BlurAlgorithm fallbackAlgorithm; 32 | private Context context; 33 | 34 | public RenderEffectBlur() { 35 | } 36 | 37 | @Override 38 | public Bitmap blur(@NonNull Bitmap bitmap, float blurRadius) { 39 | lastBlurRadius = blurRadius; 40 | 41 | if (bitmap.getHeight() != height || bitmap.getWidth() != width) { 42 | height = bitmap.getHeight(); 43 | width = bitmap.getWidth(); 44 | node.setPosition(0, 0, width, height); 45 | } 46 | Canvas canvas = node.beginRecording(); 47 | canvas.drawBitmap(bitmap, 0, 0, null); 48 | node.endRecording(); 49 | node.setRenderEffect(RenderEffect.createBlurEffect(blurRadius, blurRadius, Shader.TileMode.MIRROR)); 50 | // returning not blurred bitmap, because the rendering relies on the RenderNode 51 | return bitmap; 52 | } 53 | 54 | @Override 55 | public void destroy() { 56 | node.discardDisplayList(); 57 | if (fallbackAlgorithm != null) { 58 | fallbackAlgorithm.destroy(); 59 | } 60 | } 61 | 62 | @Override 63 | public boolean canModifyBitmap() { 64 | return true; 65 | } 66 | 67 | @NonNull 68 | @Override 69 | public Bitmap.Config getSupportedBitmapConfig() { 70 | return Bitmap.Config.ARGB_8888; 71 | } 72 | 73 | @Override 74 | public float scaleFactor() { 75 | return BlurController.DEFAULT_SCALE_FACTOR; 76 | } 77 | 78 | @Override 79 | public void render(@NonNull Canvas canvas, @NonNull Bitmap bitmap) { 80 | if (canvas.isHardwareAccelerated()) { 81 | canvas.drawRenderNode(node); 82 | } else { 83 | if (fallbackAlgorithm == null) { 84 | fallbackAlgorithm = new RenderScriptBlur(context); 85 | } 86 | fallbackAlgorithm.blur(bitmap, lastBlurRadius); 87 | fallbackAlgorithm.render(canvas, bitmap); 88 | } 89 | } 90 | 91 | void setContext(@NonNull Context context) { 92 | this.context = context; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/RenderScriptBlur.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import static eightbitlab.com.blurview.BlurController.DEFAULT_SCALE_FACTOR; 4 | 5 | import android.content.Context; 6 | import android.graphics.Bitmap; 7 | import android.graphics.Canvas; 8 | import android.graphics.Paint; 9 | import android.os.Build; 10 | import android.renderscript.Allocation; 11 | import android.renderscript.Element; 12 | import android.renderscript.RenderScript; 13 | import android.renderscript.ScriptIntrinsicBlur; 14 | 15 | import androidx.annotation.NonNull; 16 | import androidx.annotation.RequiresApi; 17 | 18 | /** 19 | * Blur using RenderScript, processed on GPU when device drivers support it. 20 | * Requires API 17+ 21 | * 22 | * @deprecated because RenderScript is deprecated and its hardware acceleration is not guaranteed. 23 | * RenderEffectBlur is the best alternative at the moment. 24 | */ 25 | @Deprecated 26 | public class RenderScriptBlur implements BlurAlgorithm { 27 | private final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); 28 | private final RenderScript renderScript; 29 | private final ScriptIntrinsicBlur blurScript; 30 | private Allocation outAllocation; 31 | 32 | private int lastBitmapWidth = -1; 33 | private int lastBitmapHeight = -1; 34 | 35 | /** 36 | * @param context Context to create the {@link RenderScript} 37 | */ 38 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 39 | public RenderScriptBlur(@NonNull Context context) { 40 | renderScript = RenderScript.create(context); 41 | blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); 42 | } 43 | 44 | private boolean canReuseAllocation(@NonNull Bitmap bitmap) { 45 | return bitmap.getHeight() == lastBitmapHeight && bitmap.getWidth() == lastBitmapWidth; 46 | } 47 | 48 | /** 49 | * @param bitmap bitmap to blur 50 | * @param blurRadius blur radius (1..25) 51 | * @return blurred bitmap 52 | */ 53 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 54 | @Override 55 | public Bitmap blur(@NonNull Bitmap bitmap, float blurRadius) { 56 | //Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag 57 | Allocation inAllocation = Allocation.createFromBitmap(renderScript, bitmap); 58 | 59 | if (!canReuseAllocation(bitmap)) { 60 | if (outAllocation != null) { 61 | outAllocation.destroy(); 62 | } 63 | outAllocation = Allocation.createTyped(renderScript, inAllocation.getType()); 64 | lastBitmapWidth = bitmap.getWidth(); 65 | lastBitmapHeight = bitmap.getHeight(); 66 | } 67 | 68 | blurScript.setRadius(blurRadius); 69 | blurScript.setInput(inAllocation); 70 | //do not use inAllocation in forEach. it will cause visual artifacts on blurred Bitmap 71 | blurScript.forEach(outAllocation); 72 | outAllocation.copyTo(bitmap); 73 | 74 | inAllocation.destroy(); 75 | return bitmap; 76 | } 77 | 78 | @Override 79 | public final void destroy() { 80 | blurScript.destroy(); 81 | renderScript.destroy(); 82 | if (outAllocation != null) { 83 | outAllocation.destroy(); 84 | } 85 | } 86 | 87 | @Override 88 | public boolean canModifyBitmap() { 89 | return true; 90 | } 91 | 92 | @NonNull 93 | @Override 94 | public Bitmap.Config getSupportedBitmapConfig() { 95 | return Bitmap.Config.ARGB_8888; 96 | } 97 | 98 | @Override 99 | public float scaleFactor() { 100 | return DEFAULT_SCALE_FACTOR; 101 | } 102 | 103 | @Override 104 | public void render(@NonNull Canvas canvas, @NonNull Bitmap bitmap) { 105 | canvas.drawBitmap(bitmap, 0f, 0f, paint); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /library/src/main/java/eightbitlab/com/blurview/SizeScaler.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | /** 4 | * Scales width and height by [scaleFactor], 5 | * and then rounds the size proportionally so the width is divisible by [ROUNDING_VALUE] 6 | */ 7 | public class SizeScaler { 8 | 9 | // Bitmap size should be divisible by ROUNDING_VALUE to meet stride requirement. 10 | // This will help avoiding an extra bitmap allocation when passing the bitmap to RenderScript for blur. 11 | // Usually it's 16, but on Samsung devices it's 64 for some reason. 12 | private static final int ROUNDING_VALUE = 64; 13 | private final float scaleFactor; 14 | 15 | public SizeScaler(float scaleFactor) { 16 | this.scaleFactor = scaleFactor; 17 | } 18 | 19 | Size scale(int width, int height) { 20 | int nonRoundedScaledWidth = downscaleSize(width); 21 | int scaledWidth = roundSize(nonRoundedScaledWidth); 22 | //Only width has to be aligned to ROUNDING_VALUE 23 | float roundingScaleFactor = (float) width / scaledWidth; 24 | //Ceiling because rounding or flooring might leave empty space on the View's bottom 25 | int scaledHeight = (int) Math.ceil(height / roundingScaleFactor); 26 | 27 | return new Size(scaledWidth, scaledHeight, roundingScaleFactor); 28 | } 29 | 30 | boolean isZeroSized(int measuredWidth, int measuredHeight) { 31 | return downscaleSize(measuredHeight) == 0 || downscaleSize(measuredWidth) == 0; 32 | } 33 | 34 | /** 35 | * Rounds a value to the nearest divisible by {@link #ROUNDING_VALUE} to meet stride requirement 36 | */ 37 | private int roundSize(int value) { 38 | if (value % ROUNDING_VALUE == 0) { 39 | return value; 40 | } 41 | return value - (value % ROUNDING_VALUE) + ROUNDING_VALUE; 42 | } 43 | 44 | private int downscaleSize(float value) { 45 | return (int) Math.ceil(value / scaleFactor); 46 | } 47 | 48 | static class Size { 49 | 50 | final int width; 51 | final int height; 52 | // TODO this is probably not needed anymore 53 | final float scaleFactor; 54 | 55 | Size(int width, int height, float scaleFactor) { 56 | this.width = width; 57 | this.height = height; 58 | this.scaleFactor = scaleFactor; 59 | } 60 | 61 | @Override 62 | public boolean equals(Object o) { 63 | if (this == o) return true; 64 | if (o == null || getClass() != o.getClass()) return false; 65 | 66 | Size size = (Size) o; 67 | 68 | if (width != size.width) return false; 69 | if (height != size.height) return false; 70 | return Float.compare(size.scaleFactor, scaleFactor) == 0; 71 | } 72 | 73 | @Override 74 | public int hashCode() { 75 | int result = width; 76 | result = 31 * result + height; 77 | result = 31 * result + (scaleFactor != +0.0f ? Float.floatToIntBits(scaleFactor) : 0); 78 | return result; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | return "Size{" + 84 | "width=" + width + 85 | ", height=" + height + 86 | ", scaleFactor=" + scaleFactor + 87 | '}'; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /library/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/test/java/eightbitlab/com/blurview/SizeScalerTest.java: -------------------------------------------------------------------------------- 1 | package eightbitlab.com.blurview; 2 | 3 | import androidx.annotation.NonNull; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | import org.junit.jupiter.params.provider.Arguments; 7 | import org.junit.jupiter.params.provider.CsvSource; 8 | import org.junit.jupiter.params.provider.MethodSource; 9 | 10 | import java.util.stream.Stream; 11 | 12 | import eightbitlab.com.blurview.SizeScaler.Size; 13 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; 15 | import static org.junit.jupiter.params.provider.Arguments.of; 16 | 17 | class SizeScalerTest { 18 | private static final float scalingFactor = 8f; 19 | 20 | private final SizeScaler scaler = new SizeScaler(scalingFactor); 21 | 22 | @ParameterizedTest 23 | @MethodSource("scalingResults") 24 | void scales_and_returns_proper_size_and_scale_factor(int x, int y, Size expected) { 25 | Size result = scaler.scale(x, y); 26 | assertEquals(expected, result); 27 | } 28 | 29 | // In case if rounding mode for downscaleSize() will be changed 30 | @ParameterizedTest 31 | @CsvSource({"0,0,true", "1,1,false", "8,8,false", "0,100,true"}) 32 | void isZeroSized(int x, int y, boolean isZeroSized) { 33 | assertEquals(isZeroSized, scaler.isZeroSized(x, y)); 34 | } 35 | 36 | @SuppressWarnings("unused") 37 | private static Stream scalingResults() { 38 | return Stream.of( 39 | of(64, 64, size(64, 64, 1f)), 40 | // min size is 64 41 | of(7, 7, size(64, 64, 0.109375f)), 42 | of(128, 128, size(64, 64, 2f)), 43 | of(1024, 1024, size(128, 128, 8f)), 44 | // if Y is not divisible by 64 but X is, don't align Y 45 | of(1024, 256, size(128, 32, 8)), 46 | of(1000, 256, size(128, 33, 7.8125f)), 47 | // scale Y by the same factor as X 48 | of(900, 256, size(128, 37, 7.03125f)), 49 | of(900, 200, size(128, 29, 7.03125f)), 50 | of(907, 203, size(128, 29, 7.0859375f)), 51 | of(1080, 104, size(192, 19, 5.625f)), 52 | of(1080, 192, size(192, 35, 5.625f)), 53 | of(1080, 1149, size(192, 205, 5.625f)) 54 | ); 55 | } 56 | 57 | @NonNull 58 | private static Size size(int x, int y, float scaleFactor) { 59 | return new Size(x, y, scaleFactor); 60 | } 61 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------