图片实现高斯玻璃效果
来源:互联网 发布:淘宝网购物压缩袋 编辑:程序博客网 时间:2024/05/17 02:45
最近做项目需要一个背景图,可是能找到的背景图都会影响美观,因此决定使用高斯玻璃效果。
上我使用的代码:
public class ImageUtil { public static Bitmap blurBitmap(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_8888); //Instantiate a new Renderscript RenderScript rs = RenderScript.create(MyApplication.getContext()); //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; }}然后使用glide框架加载。可是glide没办法加载bitmap类,所以使用了其他方法将bitmap转换成byte[]。
下面我我将我使用的一个工具类献上,这也是我从一位网友那里获得的。如果这位朋友觉得侵权了,请跟我联系,我会立即删除。
public class ImageDispose { /** * @param 将图片内容解析成字节数组 * @param inStream * @return byte[] * @throws Exception */ public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } /** * @param 将字节数组转换为ImageView可调用的Bitmap对象 * @param bytes * @param opts * @return Bitmap */ public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { if (bytes != null) if (opts != null) return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts); else return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); return null; } /** * @param 图片缩放 * @param bitmap 对象 * @param w 要缩放的宽度 * @param h 要缩放的高度 * @return newBmp 新 Bitmap对象 */ public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) w / width); float scaleHeight = ((float) h / height); matrix.postScale(scaleWidth, scaleHeight); Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return newBmp; } /** * 把Bitmap转Byte */ public static byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * 把字节数组保存为一个文件 */ public static File getFileFromBytes(byte[] b, String outputFile) { BufferedOutputStream stream = null; File file = null; try { file = new File(outputFile); FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return file; }}
通过上面的步骤就可以很完美的做到这一点。给大家上一下效果图。
阅读全文
0 0
- 图片实现高斯玻璃效果
- 玻璃效果的实现
- unity实现玻璃效果
- iOS实现图片高斯模糊效果
- 图片高斯模糊效果的实现
- 玻璃效果
- 玻璃效果
- 玻璃效果
- 关闭窗口是实现渐变玻璃效果
- 网上找的例子实现玻璃效果.
- Android完美实现高斯模糊图片效果
- iOS 毛玻璃效果和高斯图片模糊实现
- 图片高斯模糊效果
- Javascript代码实现图片玻璃特效
- VC实现去掉vista窗口的玻璃效果
- iOS图片高斯模糊 毛玻璃效果
- 图片高斯模糊效果简单优化
- 给图片增加高斯模糊效果
- 1次演讲13次提到技术,马云在阿里巴巴年会上都说了什么?
- Java开发Maven插件
- 前端架构之路:Windows下安装Nodejs步骤
- GitHub的使用
- mvn testng idea
- 图片实现高斯玻璃效果
- python django-admin 创建project的问题
- 属性-字体(2)
- 图的前向星表示
- 机器学习第十五课(朴素贝叶斯,拉普拉斯修正,贝叶斯网络)
- Input.GetTouch()方法
- Apache Commons工具集简介
- 资讯精选 | 对话阿里云异构计算掌舵人:人工智能爆发三要素
- python 函数式编程