Android实现图片转高斯模糊以及高斯模糊布局
作者:KevinSpaces 发布时间:2023-10-20 16:10:23
标签:Android,高斯模糊
第一个为大家介绍图片如何转高斯模拟:
1.方法的实现:
public static void updateBgToBlur(Activity a, Bitmap bmpToBlur, View view, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
opt.inSampleSize = 8;
opt.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), resId, opt);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(null);
} else {
view.setBackgroundDrawable(null);
}
if (bmpToBlur != null && !bmpToBlur.isRecycled()) {
bmpToBlur.recycle();
}
bmpToBlur = blurBitmap(a, bmp);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(new BitmapDrawable(a.getResources(), bmpToBlur));
} else {
view.setBackgroundDrawable(new BitmapDrawable(a.getResources(), bmpToBlur));
}
}
public static Bitmap blurBitmap(Context c, Bitmap bitmap) {
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(c.getApplicationContext());
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
2 调用:
Bitmap bitmap=null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
ImageUtil.updateBgToBlur(getActivity(), bitmap, slidingUpPanelLayout, R.drawable.bg_tageditor);
} else {
slidingUpPanelLayout.setBackgroundResource(R.drawable.bg_tageditor);
}
二、高斯模糊布局:
项目需求: 现有一个紫色背景图片, 相册图片覆盖在背景图片 , 一个Framlayout 覆盖在这个含有相册图片的背景图中 ,实现模糊盖在上面的高斯模拟效果:
1 引用BlurView:
compile 'com.eightbitlab:supportrenderscriptblur:1.0.0'
compile 'com.eightbitlab:blurview:1.3.3'
defaultConfig {
renderscriptTargetApi 25 //must match target sdk and build tools, 23+
renderscriptSupportModeEnabled true
}
2 .调用:
final float radius = 20;
final View decorView = getActivity().getWindow().getDecorView();
//Activity's root View. Can also be root View of your layout (preferably)
final ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content);
//set background, if your root layout doesn't have one
final Drawable windowBackground = decorView.getBackground();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mBlurView.setupWith(rootView)
.windowBackground(windowBackground)
.blurAlgorithm(new RenderScriptBlur(getActivity()))
.blurRadius(radius);
}else {
mBlurView.setupWith(rootView)
.windowBackground(windowBackground)
.blurAlgorithm(new SupportRenderScriptBlur(getActivity()))
.blurRadius(radius);
}
3 xml
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:blurOverlayColor="@color/colorOverlay">
<!--Any child View here, TabLayout for example-->
</eightbitlab.com.blurview.BlurView>


猜你喜欢
- 一、问题来源项目中遇到 json 模型映射成 RadialGradient 组件的需求,其他参数正常传递即可;唯独 radius 参数效果有
- 本文设计一个简单的班级管理系统,满足如下要求:1、设计学生类Student,包含学号(String型)、姓名(String型)、
- 前言最近接到个需求,不使用第三方SDK的情况下实现IM通讯,文字聊天已经通过MQTT实现,而语音功能目前想到的较好解决方案就是进行录音文件的
- 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程:首先温馨提示, 这一小节高度耦合第三章的
- 前言最近接手了一个老项目,“愉悦的心情”自然无以言表,做开发的朋友都懂,这里就不多说了,都是泪...
- 只需要在控件TextBox的keypress事件中写入如下代码即可满足要求:代码如下:if (e.KeyChar == '.'
- 前言:现在一般的Android软件都是需要不断更新的,当你打开某个app的时候,如果有新的版本,它会提示你有新版本需要更新。该项目实现的就是
- 需要读取如图所示注册表【HKEY_LOCAL_MACHINE\SOFTWARE\EasyDrv7】节点下的【DateTime】的值直接上代码
- 本文实例为大家分享了android实现简单拼图游戏的具体代码,供大家参考,具体内容如下1.2.//使用回调接口,首先初始化pintuview
- 本文实例讲述了C#文件和字节流的转换方法。分享给大家供大家参考。具体实现方法如下:1、读取文件,并转换为字节流FileStream fs =
- Android中子线程和UI线程之间通信的详细解释 1.在多线程编程这块,我们经常要使用Handler,Thread和Runnable这三个
- 摘要每天一个UI小技巧,提高开发效率 UI开发中繁琐的drawable xml开发,不同的view背景样式一致,却因为部分设计区别,就要重新
- 这篇文章主要介绍了Java List分页功能实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢?在
- 本文将是JVM 性能优化系列的第二篇文章(第一篇:传送门),Java 编译器将是本文讨论的核心内容。本文中,作者(Eva Andreasso
- 一、TimerTimer是Android直接启动定时器的类,TimerTask是一个子线程,方便处理一些比较复杂耗时的功能逻辑,经常与han
- 自定义View分为继承自View和ViewGroup,继承ViewGroup相比继承View在事件分发上ViewGroup多dispatch
- 一、ToolBar1、在build.gradle中添加依赖,例如:compile 'com.android.support:appc
- 本文实例讲述了C#利用System.Uri转URL为绝对地址的方法。分享给大家供大家参考。具体分析如下:在使用ASPOSE.Word生成Wo
- 前言打包桌面应用程序实在是一个不常使用的东西,偶尔使用起来经常会忘东忘西的耽误时间,因此,这篇文章多以图片记录过程,也是用于备忘。下载打包工