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
原创粉丝点击