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>
0
投稿
猜你喜欢
- [LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子
- • 创建目录和文件1、通过Path类的Combine方法可以合并路径。string activeDir = @"C:\myDir&
- state:比较常用,各种状态都可以用它,但是它更着重于一种心理状态或者物理状态。Status:用在人的身上一般是其身份和地位,作“状态,情
- 动态SQL实现前端指定返回字段问题描述在使用ClickHouse时,遇到需要根据业务需求,动态返回指定字段,从而充分利用ClickHouse
- 通常我们在看一些源码时,发现全是T、?,晕乎乎的:sob:。于是,把泛型掌握好十分重要!什么是泛型Java 泛型(generics)是 JD
- 本文实例为大家分享了java读取excel文件的具体代码,供大家参考,具体内容如下方式一:借用package com.ij34.util;/
- 有个网站需要生成静态页。据以往经验,凡比较烂的空间,短时间内运行耗能大的运算,都会出现“service unavailable”,以致网页无
- 接下来想做一个图廊,所以并没有必要用立方体,只需做一些“墙壁”就行了。而在一个平面上建起另一个矩形的
- 需求:按照起始日期查询出数据库里一段连续日期的住院信息。问题:数据库里的住院信息可能不是完整的,也就是在给出的日期区间里只有若干天的数据,缺
- 在说struts2的线程安全之前,先说一下,什么是线程安全?这是一个网友讲的。如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会
- 之前我们有介绍通过Spring Boot Admin来检测服务的上下线,然后进行通知功能。https://www.jb51.net/arti
- 线程间通信我们看下面的图我们来看线程间通信的原理:线程(Thread B)和线程(Thread A)通信, 首先线程A 必须实现同步上下文对
- 做消息通信,消息会不断从网络流中取得,而后台也有线程不断消费。本来我一直是使用一些线程安全标识或方法来控制,后来在网上找到一些java新特性
- 前言本文主要给大家介绍的是关于Java通过Class类获取Class对象的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的
- 1.过滤器 (Filter)过滤器的配置比较简单,直接实现Filter 接口即可,也可以通过@WebFilter注解实现对特定URL拦截,看
- synchronizedsynchronized可以用来同步块,同步方法。同步块可以用来更精确地控制对象锁,控制锁的作用域。(锁
- 目录前沿快速开始引入依赖定义接口配置类开始调用json序列化接口层面指定header:指定Encoder跟Decoder使用 * 注解详解@
- 本文实例为大家分享了使用C#写一个时钟,供大家参考,具体内容如下时钟是这样的一共使用四个控件即可:WinFrom窗体应用程序代码:using
- 废话不多说,我们先来认识一下这个GDI+,看看它到底长什么样。GDI+:Graphics Device Interface Plus也就是图
- 我们日常的工作中都使用开发工具(IntelliJ IDEA 或 Eclipse 等)可以很方便的调试程序,或者是通过打包工具把项目打包成 j