Android 控件模糊案例

blurkit-android

项目地址: wonderkiln/blurkit-android
简介:The missing Android blurring library. Fast blur-behind layout that parallels iOS.

BlurKit Header

BlurKit is an extraordinarily easy to use utility to get live blurring just like on iOS. Built by Dylan McIntyre.

BlurKit Demo

BlurKit is faster than other blurring libraries due to a number of bitmap retrieval and drawing optimizations. We've been logging benchmarks for the basic high-intensity tasks for a 300dp x 100dp BlurView:

TaskBlurKit timeAvg. time without BlurKit
Retrieve source bitmap1-2 ms8-25 ms
Blur and draw to BlurView1-2 ms10-50ms

This results in an average work/frame time of 2-4ms, which will be a seamless experience for most users and apps.

Setup

Add BlurKit to your dependencies block:

compile 'com.wonderkiln:blurkit:1.0.0'

You also need to add RenderScript to your app module. Add these lines to the defaultConfig block of yourbuild.gradle.

renderscriptTargetApi 24
renderscriptSupportModeEnabled true

Usage

BlurLayout

Add a BlurLayout to your layout just like any other view.

<com.wonderkiln.blurkit.BlurLayout
    android:id="@+id/blurLayout"
    android:layout_width="150dp"
    android:layout_height="150dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="BlurKit!"
        android:textColor="@android:color/white" />

</com.wonderkiln.blurkit.BlurLayout>

The layout background will continuously blur the content behind it. If you know your background content will be somewhat static, you can set the layout fps to 0. At any time you can re-blur the background content by callinginvalidate() on the BlurLayout.

<com.wonderkiln.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
    android:id="@+id/blurLayout"
    android:layout_width="150dp"
    android:layout_height="150dp"
    blurkit:blk_fps="0" />

Other attributes you can configure are the blur radius and the downscale factor. Getting these to work together well can take some experimentation. The downscale factor is a performance optimization; the bitmap for the background content will be downsized by this factor before being drawn and blurred.

<com.wonderkiln.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
    android:id="@+id/blurLayout"
    android:layout_width="150dp"
    android:layout_height="150dp"
    blurkit:blk_blurRadius="12"
    blurkit:blk_downscaleFactor="0.12"
    blurkit:blk_fps="60" />

Other

You can use the BlurKit class which has a few useful blurring utilities. Before using this class outside of aBlurLayout, you need to initialize BlurKit.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        BlurKit.init(this);
    }
}

You can blur a View, or a Bitmap directly.

// View
BlurKit.getInstance().blur(View src, int radius);

// Bitmap
BlurKit.getInstance().blur(Bitmap src, int radius);

You can also fastBlur a View. This optimizes the view blurring process by allocating a downsized bitmap and using a Matrix with the bitmaps Canvas to prescale the drawing of the view to the bitmap.

BlurKit.getInstance().fastBlur(View src, int radius, float downscaleFactor);

Proguard

If you use Proguard, add the following to your proguard-rules.pro:

-keep class com.wonderkiln.blurkit.** { *; }

-dontwarn android.support.v8.renderscript.*
-keepclassmembers class android.support.v8.renderscript.RenderScript {
  native *** rsn*(...);
  native *** n*(...);
}

To Do (incoming!)

  • [ ] SurfaceView support
  • [ ] Support for use outside of an Activity (dialogs, etc.)
  • [ ] Enhance retrieval of background content to only include views drawn behind the BlurLayout.

Credits

Dylan McIntyre

License

BlurKit-Android is MIT licensed.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 控件集合是指在Android开发中,用于构建用户界面的一组预定义控件的集合。Android提供了丰富的控件可供开发者使用,包括文本框、按钮、图像视图、列表视图、下拉框等。这些控件可以通过XML布局文件或者Java代码动态创建和配置。 Android 控件集合具有以下特点: 1. 多样性:Android控件集合中包含了丰富的控件种类,满足了不同应用场景下的需求。开发者可以根据具体的界面设计要求选择合适的控件进行使用。 2. 可定制性:Android控件集合提供了丰富的配置属性,开发者可以通过设置这些属性来满足自己的设计需求。同时还可以通过自定义控件继承或覆盖Android原生控件,实现更加个性化的效果。 3. 事件监听:Android控件集合支持事件监听机制,开发者可以为控件添加相应的事件处理器,对用户的交互行为做出响应。比如,可以为按钮控件添加点击事件监听器,实现点击按钮后执行相应的操作。 4. 布局管理:Android控件集合中的控件可以通过各种布局管理器进行灵活的排列和组合。比如,线性布局、相对布局、帧布局等。开发者可以根据需求选择合适的布局管理器,实现想要的页面布局效果。 综上所述,Android控件集合提供了丰富多样的控件供开发者使用,具备较高的可定制性和灵活性,方便开发者构建出各种各样的用户界面。通过合理运用和配置控件集合,可以实现强大的Android应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值