分享自己写的一个高斯模糊的工具类,可以根据Bitmap,Imageviw,Drawable或者资源文件设置

public class BlurImageView {

/** 水平方向模糊度 */

public static float HRADIUS = 5;

/** 竖直方向模糊度 */

public static float VRADIUS = 5;

/** 模糊迭代度 */

public static int ITERATIONS = 5;

/**

* 根据bitmap设置高斯模糊

* @param bmp:bitmap参数

* @return

*/

public static Drawable BoxBlurFilter(Bitmap bmp) {

int width = bmp.getWidth();

int height = bmp.getHeight();

int[] inPixels = new int[width * height];

int[] outPixels = new int[width * height];

Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);

bmp.getPixels(inPixels, 0, width, 0, 0, width, height);

for (int i = 0; i < ITERATIONS; i++) {

blur(inPixels, outPixels, width, height, HRADIUS);

blur(outPixels, inPixels, height, width, VRADIUS);

}

blurFractional(inPixels, outPixels, width, height, HRADIUS);

blurFractional(outPixels, inPixels, height, width, VRADIUS);

bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);

Drawable drawable = new BitmapDrawable(bitmap);

return drawable;

}

/**

* 根据ImageView设置高斯模糊

* @param img:ImageView

* @return

*/

public static void BoxBlurFilter(ImageView img) {

Bitmap bmp =((BitmapDrawable)img.getDrawable()).getBitmap();

int width = bmp.getWidth();

int height = bmp.getHeight();

int[] inPixels = new int[width * height];

int[] outPixels = new int[width * height];

Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);

bmp.getPixels(inPixels, 0, width, 0, 0, width, height);

for (int i = 0; i < ITERATIONS; i++) {

blur(inPixels, outPixels, width, height, HRADIUS);

blur(outPixels, inPixels, height, width, VRADIUS);

}

blurFractional(inPixels, outPixels, width, height, HRADIUS);

blurFractional(outPixels, inPixels, height, width, VRADIUS);

bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);

Drawable drawable = new BitmapDrawable(bitmap);

img.setImageDrawable(drawable);

}

/**

* 根据项目资源文件图片返回高斯模糊drawable

* @param context:上下文

* @param res:资源id

*/

public static Drawable BoxBlurFilter(Context context, int res) {

Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), res);

return BoxBlurFilter(bmp);

}

/**

* 根据drawable返回高斯模糊

* @param drawable

* @return

*/

public static Drawable BoxBlurFilter(Drawable drawable) {

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

return BoxBlurFilter(bitmap);

}

/**

* 核心代码

* @param in

* @param out

* @param width

* @param height

* @param radius

*/

public static void blur(int[] in, int[] out, int width, int height,

float radius) {

int widthMinus1 = width - 1;

int r = (int) radius;

int tableSize = 2 * r + 1;

int divide[] = new int[256 * tableSize];

for (int i = 0; i < 256 * tableSize; i++)

divide[i] = i / tableSize;

int inIndex = 0;

for (int y = 0; y < height; y++) {

int outIndex = y;

int ta = 0, tr = 0, tg = 0, tb = 0;

for (int i = -r; i <= r; i++) {

int rgb = in[inIndex + clamp(i, 0, width - 1)];

ta += (rgb >> 24) & 0xff;

tr += (rgb >> 16) & 0xff;

tg += (rgb >> 8) & 0xff;

tb += rgb & 0xff;

}

for (int x = 0; x < width; x++) {

out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16)

| (divide[tg] << 8) | divide[tb];

int i1 = x + r + 1;

if (i1 > widthMinus1)

i1 = widthMinus1;

int i2 = x - r;

if (i2 < 0)

i2 = 0;

int rgb1 = in[inIndex + i1];

int rgb2 = in[inIndex + i2];

ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);

tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;

tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;

tb += (rgb1 & 0xff) - (rgb2 & 0xff);

outIndex += height;

}

inIndex += width;

}

}

public static void blurFractional(int[] in, int[] out, int width,

int height, float radius) {

radius -= (int) radius;

float f = 1.0f / (1 + 2 * radius);

int inIndex = 0;

for (int y = 0; y < height; y++) {

int outIndex = y;

out[outIndex] = in[0];

outIndex += height;

for (int x = 1; x < width - 1; x++) {

int i = inIndex + x;

int rgb1 = in[i - 1];

int rgb2 = in[i];

int rgb3 = in[i + 1];

int a1 = (rgb1 >> 24) & 0xff;

int r1 = (rgb1 >> 16) & 0xff;

int g1 = (rgb1 >> 8) & 0xff;

int b1 = rgb1 & 0xff;

int a2 = (rgb2 >> 24) & 0xff;

int r2 = (rgb2 >> 16) & 0xff;

int g2 = (rgb2 >> 8) & 0xff;

int b2 = rgb2 & 0xff;

int a3 = (rgb3 >> 24) & 0xff;

int r3 = (rgb3 >> 16) & 0xff;

int g3 = (rgb3 >> 8) & 0xff;

int b3 = rgb3 & 0xff;

a1 = a2 + (int) ((a1 + a3) * radius);

r1 = r2 + (int) ((r1 + r3) * radius);

g1 = g2 + (int) ((g1 + g3) * radius);

b1 = b2 + (int) ((b1 + b3) * radius);

a1 *= f;

r1 *= f;

g1 *= f;

b1 *= f;

out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;

outIndex += height;

}

out[outIndex] = in[width - 1];

inIndex += width;

}

}

public static int clamp(int x, int a, int b) {

return (x < a) ? a : (x > b) ? b : x;

}

}

最后放上两张对比图

android设置模糊度,Android设置高斯模糊相关推荐

  1. Android中为网络图片设置高斯模糊效果

    参考了这篇文章:               Android:简单靠谱的动态高斯模糊效果 写一个方法,用来对Bitmap进行高斯模糊: public static Bitmap blurBitmap( ...

  2. Android环境变量的设置(详细图解版)

    Android环境变量的设置(详细图解版) 转载于:https://www.cnblogs.com/zhujiabin/p/4875182.html

  3. android textview 设置字体,Android TextView设置字体风格

    在开发应用过程中经常会遇到显示一些不同的字体风格的信息犹如默认的LockScreen上面的时间和充电信息.对于类似的情况,可能第一反应就是用不同的多个TextView来实现,对于每个TextView设 ...

  4. android selector下的设置背景属性值

    在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 state_pressed,state_checked,state_pressed,state_selected, ...

  5. android setting模块,android O版本 设置(Settings)模块总结--设置的一级界面的加载

    O版本的设置界面相对有N有了一些变化,O上面增加了顶级类别的菜单,而之前一些在一级菜单的则移动到了二级界面里面, 如"WIFI","移动网络"等之前是在一级界面 ...

  6. 【Android学习笔记】设置App启动页

    先将启动页放到项目资源中,图片一般是1080*1920的jpg. 新建一个activity,如图: 创建成功之后,打开刚刚创建的activity,来进行代码的编写: public class BZLa ...

  7. 【错误记录】未安装该应用 ( 在 Android 12 之后 组件设置 android:exported=“false“ 属性 )

    文章目录 一.报错信息 二.解决方案 一.报错信息 报错信息 : 点击应用图标后 , 应用并未启动 , 并弹出 " 未安装该应用 " 提示信息 ; 二.解决方案 排查了一下相关地方 ...

  8. 【错误记录】Android 文件分享 FileProvider 设置错误

    文章目录 一.报错信息 二.解决方案 一.报错信息 分享内部存储到文件到其它应用 ; 2021-05-18 16:10:23.480 31881-31881/kim.hsl.file E/DEBUG: ...

  9. 【Android RTMP】Android Camera 视频数据采集预览 ( 图像传感器方向设置 | Camera 使用流程 | 动态权限申请 )

    文章目录 安卓直播推流专栏博客总结 一. Camera 传感器方向简介 二. Camera 图像传感器横向显示数据 三. Camera 图像传感器纵向显示数据 四. 设置 Camera 预览数据方向 ...

最新文章

  1. 资源 | 深度学习图像标注工具汇总
  2. mirna富集分析_2020年的3+分ceRNA分析长啥样?
  3. tf.keras.layers.Embedding 嵌入层 示例
  4. 教你搭建基于typescript的vue项目
  5. 分享十佳Web开发资源
  6. 用户访问网站的基本流程
  7. JAVA——System.in作为控制台输入时结束输入(输入EOF)解决方案
  8. [luoguP4705]玩游戏
  9. java+调用jacoco_java操作jacoco
  10. NavMeshAgent 动态加载障碍物
  11. ZMQ中线程之间发送命令
  12. Qt:During startup program exited with code 0xc0000135
  13. Win8仿Win10无边框效果的实现
  14. SATA,SAS,SSD 读写性能测试结果
  15. ElasticSearch 23 种映射参数详解
  16. Jetson TX2 安装 D435i ROS驱动
  17. Ps笔刷:雨水掉落效果
  18. 网页设计与网站规划 作业21 圣诞节壁纸制作
  19. DSm安装mysql_群晖Synology DSM系统安装教程
  20. RabbitMQ-topic模式

热门文章

  1. ⑬云上场景:蜻蜓fm,基于ECS构建的转码集群
  2. 【复习笔记】电分-第五章-电力系统无功功率和电压调整
  3. XML与各种格式的数据进行转换的工具类
  4. python 阻止锁屏_Python我用干什么之:锁屏单词壁纸
  5. 3dmax to UE4 模型制作流程与规范
  6. 淘淘商城——展示后台管理页面
  7. tcp服务器制作,用telnet来测试你自己写的TCP服务器
  8. 短信猫二次开发(java版)
  9. 常用postgresql命令
  10. MVC中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别