Android RenderScript模糊图片失败_虚化图片失败_的原因
来源:互联网 发布:斗鱼点播软件 编辑:程序博客网 时间:2024/05/18 03:26
今天用到了CollapsingToolbarLayout,给背景添加一个模糊图片,原图是从ImageView中获取的Drawable对象;网上查阅了很多文章,,能快速模糊方式,是使用谷歌提供的RenderScript.
由于我的ImageView的图片是通过网络加载,顾只能通过getDrawable()拿到图片的Drawable对象,
使用网上提供的方法将Drawable转换成Bitmap,并高斯模糊设置到背景(略麻烦..),可结果竟然是这样…
而我想要的效果则是这样子的:
使用方法:
1:将Drawable转换成Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
2.将Bitmap进行高斯模糊:
public static Bitmap blurBitmap(Bitmap bitmap){ Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); RenderScript rs = RenderScript.create(UIUtils.getContext()); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); blurScript.setRadius(25.0f); blurScript.setInput(allIn); blurScript.forEach(allOut); allOut.copyTo(outBitmap); bitmap.recycle(); rs.destroy(); return outBitmap; }
原因和解决:
方法没问题可为什么出不来效果?仔细检查了方法,额…….,原来两个方法创建bitmap对象时参数3不一致造成的这样的后果,
//从drawable转bitmap对象 Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);//使用转换的bitmap生成高斯模糊的图片 Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
这样把createBitmap()的参数3 统一改成Bitmap.Config.ARGB_8888
问题就解决了!!!
后来又找到一种方法,直接使用
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();就是将从ImageView获取的drawable强转成BitmapDrawable,在获取Bitmap,这样
真的是一失足成千古恨…一点都马虎不得/
Config.ARGB_8888具体参数的含义:
http://blog.csdn.net/wulongtiantang/article/details/8481077
参考的其他文章:
http://blog.csdn.net/hezhipin610039/article/details/7899248/
http://www.open-open.com/lib/view/open1477017390932.html
1 0
- Android RenderScript模糊图片失败_虚化图片失败_的原因
- RenderScript----Android图片模糊效果的简单实现
- Android模糊一张图片的简单方法之RenderScript
- RenderScript模糊图片
- android使用RenderScript模糊图片技术
- RenderScript-图片的高斯模糊
- CI 生成随机图片失败的原因
- RenderScript对图片模糊处理
- Android RenderScript 简单高效实现图片的高斯模糊效果
- 用 Renderscript 实现高效率的图片模糊效果
- android图片_旋转图片
- android图片_缩放图片
- android图片_刮刮乐
- 图片分享和加载失败的原因之一
- OpenCV中使用imread函数读取图片失败的原因
- Android分享图片失败解决方案
- 样式化加载失败的图片
- 样式化加载失败的图片
- Path从懵逼到精通——贝塞尔曲线
- char,unsigned char和signed char三种数据类型的区别
- C语言好难——几个宏定义__DATE__,__TIME__,__FILE__,__LINE__
- ORM “杀器”之 JOOQ
- block的使用
- Android RenderScript模糊图片失败_虚化图片失败_的原因
- 单向链表的定义及基本操作
- Gym
- hdu 5997 rausen loves cakes
- 最大生成树Kruskal
- Fire Net
- 【学习】Hadoop大数据平台架构与实践--基础篇中
- 线程池实现原理
- RTMP协议学习