android ios动态模糊效果,BlurView:类似iOS的Android动态底层模糊视图

BlurView

625332134c6f4d4600884b99daebf603.png

Dynamic iOS-like blur of underlying Views for Android. Includes library and small example project.

BlurView can be used as a regular FrameLayout. It blurs its underlying content and draws it as a background for its children. BlurView redraws its blurred content when changes in view hierarchy are detected (draw() called). It honors its position and size changes, including view animation and property animation.

How to use

android:id="@+id/blurView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:blurOverlayColor="@color/colorOverlay">

float radius = 20f;

View decorView = getWindow().getDecorView();

//ViewGroup you want to start blur from. Choose root as close to BlurView in hierarchy as possible.

ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content);

//Set drawable to draw in the beginning of each blurred frame (Optional).

//Can be used in case your layout has a lot of transparent space and your content

//gets kinda lost after after blur is applied.

Drawable windowBackground = decorView.getBackground();

blurView.setupWith(rootView)

.setFrameClearDrawable(windowBackground)

.setBlurAlgorithm(new RenderScriptBlur(this))

.setBlurRadius(radius)

.setHasFixedTransformationMatrix(true);

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.

You can use setHasFixedTransformationMatrix in case if you are not animating your BlurView, this might slightly improve the performance as BlurView won't have to recalculate its transformation matrix on each frame.

Supporting API < 17

If you need to support API < 17, you can include

implementation 'com.eightbitlab:supportrenderscriptblur:1.0.2'

setup BlurView with

blurAlgorithm(new SupportRenderScriptBlur(this))

and enable RenderScript support mode

defaultConfig {

renderscriptTargetApi 28 //must match target sdk and build tools

renderscriptSupportModeEnabled true

}

Important

BlurView can be used only in a hardware-accelerated window. Otherwise, blur will not be drawn. It will fallback to a regular FrameLayout drawing process.

Performance

It takes 1-4ms on Nexus 5 and Nexus 4 to draw BlurView with the setup given in example project

Gradle

implementation 'com.eightbitlab:blurview:1.5.0'

License

Copyright 2016 Dmitry Saviuk

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值