Android实现图片转高斯模糊以及高斯模糊布局
时间:2022-07-27
本文章向大家介绍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
以上就是本文的全部内容,希望对大家的学习有所帮助。
- 在一个.net sln中包含多个project,project引用同一个dll导致的错误
- Windows下安装MariaDB
- Quartz.net官方开发指南 第一课:使用Quartz.net
- Quartz.net官方开发指南 第二课:Jobs And Triggers
- 为你的WordPress 主题添加结构化数据/丰富文本摘要,高亮搜索结果(上)
- 复合事件处理(Complex Event Processing)介绍
- Quartz.net官方开发指南 第三课:更多关于Jobs和JobDetails
- 为你的WordPress 主题添加结构化数据/丰富文本摘要,高亮搜索结果(下)
- Quartz.net官方开发指南 第四课:关于Triggers更多内容
- 数据分析:寻找Python最优计算性能
- 事件流处理框架NEsper for .NET
- Quartz.net官方开发指南 第五课: SimpleTrigger
- SQL Server Performance Dashboard Reports
- 添加WordPress评论输入邮箱实时显示Gravatar头像功能
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- AWS 命名提示需要指定 region
- AWS CodeArtifact 如何设置用户的 TOKEN
- GORM V2 自动迁移和迁移接口的方法
- Vue Nginx反向代理配置 解决生产环境跨域
- react的事件处理为什么要bind this 改变this的指向?
- 没有用到React,为什么我需要import引入React?
- JSON对象和JavaScript对象直接量的区别--不同之处
- (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
- 关于DOM的理解
- 关于闭包函数和递归函数的详细理解
- 新手怎么学JS?JavaScript基础入门
- 纯CSS制作加<div>制作动画版哆啦A梦
- Ubuntu 18.04 安装使用 Supervisor 进程守护并设置开机自动启动
- AWS CLI version 2 在 Windows 下的安装
- Vue事件绑定原理