Glide 图片形状裁剪 ,滤镜等
Glide 、 Picasso 、 Fresco 已逐渐成为Android主流的图片加载工具(个人见解,使用Volley、ImageLoader、xUtils的大佬们请勿喷~),在多数Android程序员的印象中,它们只是加载图片和缓存图片的工具,其实它们还有很多强大的功能没有被发掘...
今天,小编向各位介绍一下这些工具的新功能: 图像转换
下面是小编配合 Glide ,以 Glide Transformations 为例,写的一个图像转化的Demo :
Glide Transformations为Glide提供了图像剪裁、模糊、蒙版、色彩过滤等功能。
接下来,小编用另一个简单的事例说明Glide Transformations相关方法的使用~
详解开始(小司机开车了~)
1.创建一个Android工程。
2.导入 [Glide Transformations] 库。
dependencies {
......
// Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
// Glide图形转换工具
compile 'jp.wasabeef:glide-transformations:2.0.1'
// GPUImage
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
}
3.在activity_main.xml添加两个ImageView,一个显示原图片,一个显示转换后的图片。
4.在Activity中使用Glide为两个ImageView加载同一张图片,但第2个ImageView会添加额外的位图转换方法。(举例:加载方法如下)
Glide.with(this)
.load(url)
.into(mImageView1);
Glide.with(this)
.load(url)
.bitmapTransform(new CropTransformation(this))
.into(mImageView2);
对于没有使用过Glide的同学,小编做下简要说明:
- Glide.with(this) :使用Glide需要一个Context传入。
- Glide.load(url) :加载图片的地址,可以是本地图片id、文件路径、网络图片地址(别忘记联网权限)等等。
- Glide.into(mImageView1):加载完图片后需要在哪个ImageView中显示。
- Glide.bitmapTransform(new CropTransformation(this)):位图转换,也是小编接下来要使用的方法。
运行下程序,界面大概是这个样子:
现在,看起来两张图片是一样的,这是因为我们的转换方法执行后和原图片的显示效果是一样的。
接下来,开始进入正题,我们开始根据类别介绍Glide Transformations提供的图片转换方法:
1.图片剪裁
CropCircleTransformation (圆形剪裁显示)
// 原图片加载省略
......
// 使用构造方法 CropCircleTransformation(Context context)
Glide.with(this)
.load(url)
.bitmapTransform(new CropCircleTransformation(this))
.into(mImageView2);
- CropSquareTransformation (正方形剪裁)
// 原图片加载省略
......
// 使用构造方法 CropSquareTransformation(Context context)
Glide.with(this)
.load(url)
.bitmapTransform(new CropSquareTransformation(this))
.into(mImageView2);
- RoundedCornersTransformation (圆角剪裁)
// 使用构造方法 RoundedCornersTransformation(Context context, int radius, int margin, CornerType cornerType)
// radius :圆角半径
// margin :填充边界
// cornerType :边角类型(可以指定4个角中的哪几个角是圆角,哪几个不是)
Glide.with(this)
.load(url)
.bitmapTransform(new RoundedCornersTransformation(this, 100, 0, RoundedCornersTransformation.CornerType.ALL))
.into(mImageView2);CropTransformation (自定义矩形剪裁)
// 使用构造方法 CropTransformation(Context context, int width, int height, CropType cropType)
// width : 剪裁宽度
// height : 剪裁高度
// cropType : 剪裁类型(指定剪裁位置,可以选择上、中、下其中一种)
Glide.with(this)
.load(url)
.bitmapTransform(new CropTransformation(this, 600, 200, CropTransformation.CropType.CENTER))
.into(mImageView2);PS:如果使用CropTransformation一个参数的构造方法:只填入一个Context,后续会使用图片原本的宽高进行剪裁,这实际上和没有剪裁是一样的。
2.颜色转换
ColorFilterTransformation (颜色滤镜)
// 使用构造方法 ColorFilterTransformation(Context context, int color)
// Color :蒙层颜色值
Glide.with(this)
.load(url)
.bitmapTransform(new ColorFilterTransformation(this, 0x7900CCCC))
.into(mImageView2);
- GrayscaleTransformation(灰度级转换)
// 使用构造方法 GrayscaleTransformation(Context context)
Glide.with(this)
.load(url)
.bitmapTransform(new GrayscaleTransformation(this))
.into(mImageView2);
3.模糊处理
BlurTransformation
// 使用构造方法 BlurTransformation(Context context, int radius, int sampling)
// radius : 离散半径/模糊度(单参构造器 - 默认25)
// sampling : 取样(单参构造器 - 默认1) 如果取2,横向、纵向都会每两个像素点取一个像素点(即:图片宽高变为原来一半)
Glide.with(this)
.load(url)
.bitmapTransform(new BlurTransformation(this, 100, 2))
.into(mImageView2);
PS: 模糊处理是做过兼容的,当API>=18时使用RenderScript,API<18时使用FastBlur。
4.遮罩掩饰(视图叠加处理)
MaskTransformation
// 使用构造方法 MaskTransformation(Context context, int maskId)
// maskId :遮罩物文件ID
Glide.with(this)
.load(url)
.bitmapTransform(new MaskTransformation(this, R.mipmap.ic_launcher))
.into(mImageView2);

5.GPU过滤(需要依赖GPUImage库)
ToonFilterTransformation (卡通滤波器)
// 使用构造方法 ToonFilterTransformation(Context context, float threshold, float quantizationLevels)
// threshold :阀值(单参构造器 - 默认0.2F)影响色块边界的描边效果
// quantizationLevels :量化等级(单参构造器 - 默认10.0F)影响色块色彩
Glide.with(this)
.load(url)
.bitmapTransform(new ToonFilterTransformation(this, 0.2F, 10F))
.into(mImageView2);

SepiaFilterTransformation (乌墨色滤波器)
// 使用构造方法 SepiaFilterTransformation(Context context, float intensity)
// intensity 渲染强度(单参构造器 - 默认1.0F)
Glide.with(this)
.load(url)
.bitmapTransform(new SepiaFilterTransformation(this, 1.0F))
.into(mImageView2);
- ContrastFilterTransformation (对比度滤波器)
// 使用构造方法 ContrastFilterTransformation(Context context, float contrast)
// contrast 对比度 (单参构造器 - 默认1.0F)
Glide.with(this)
.load(url)
.bitmapTransform(new ContrastFilterTransformation(this, 3F))
.into(mImageView2);
- InvertFilterTransformation (反转滤波器)
// 使用构造方法 InvertFilterTransformation(Context context)
Glide.with(this)
.load(url)
.bitmapTransform(new InvertFilterTransformation(this))
.into(mImageView2);
- PixelationFilterTransformation (像素化滤波器)
// 使用构造方法 PixelationFilterTransformation(Context context, float pixel)
// pixel 像素值(单参构造器 - 默认10F)数值越大,绘制出的像素点越大,图像越失真
Glide.with(this)
.load(url)
.bitmapTransform(new PixelationFilterTransformation(this, 20F))
.into(mImageView2);
- SketchFilterTransformation (素描滤波器)
// 使用构造方法 SketchFilterTransformation(Context context)
Glide.with(this)
.load(url)
.bitmapTransform(new SketchFilterTransformation(this))
.into(mImageView2);
- SwirlFilterTransformation (旋转滤波器)
// 使用构造方法 SwirlFilterTransformation(Context context, float radius, float angle, PointF center)
// radius 旋转半径[0.0F,1.0F] (单参构造器 - 默认0.5F)
// angle 角度[0.0F,无穷大)(单参构造器 - 默认1.0F)视图表现为旋转圈数
// center 旋转中心点 (单参构造器 - 默认new PointF(0.5F,0.5F))
Glide.with(this)
.load(url)
.bitmapTransform(new SwirlFilterTransformation(this, 1.0F, 0.4F, new PointF(0.5F, 0.5F)))
.into(mImageView2);
- BrightnessFilterTransformation (亮度滤波器)
// 使用构造方法 BrightnessFilterTransformation(Context context, float brightness)
// brightness 光亮强度[-1F,1F](单参构造器 - 默认0.0F)小于-1F纯黑色,大于1F纯白色
Glide.with(this)
.load(url)
.bitmapTransform(new BrightnessFilterTransformation(this, 0.5F))
.into(mImageView2);
- KuwaharaFilterTransformation (Kuwahara滤波器)
// 使用构造方法 KuwaharaFilterTransformation(Context context, int radius)
// radius 半径 (单参构造器 - 默认25)
Glide.with(this)
.load(url)
.bitmapTransform(new KuwaharaFilterTransformation(this, 10))
.into(mImageView2);
- VignetteFilterTransformation (装饰图滤波器)
// 使用构造方法 VignetteFilterTransformation(Context context, PointF center, float[] color, float start, float end)
// center 装饰中心 (单参构造器 - 默认new PointF(0.5F, 0.5F))
// color 颜色组合 (单参构造器 - 默认new float[0.0F,0.0F,0.0F]) 3个颜色值分别对应RGB3种颜色,取值范围都为[0.0F,1.0F]
// start 起始点 (单参构造器 - 默认0.0F)
// end 终止点 (单参构造器 - 默认0.75F)
Glide.with(this)
.load(url)
.bitmapTransform(new VignetteFilterTransformation(this, new PointF(0.5F, 0.5F), new float[]{0.0F, 0.0F, 0.0F}, 0.0F, 0.5F))
.into(mImageView2);
Glide 图片形状裁剪 ,滤镜等的更多相关文章
- 深入探索Glide图片加载框架:做了哪些优化?如何管理生命周期?怎么做大图加载?
前言 Glide可以说是最常用的图片加载框架了,Glide链式调用使用方便,性能上也可以满足大多数场景的使用,Glide源码与原理也是面试中的常客. 但是Glide的源码内容比较多,想要学习它的源码往 ...
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide图片缓存库的联系与区别
Universal-Image-Loader,android-Volley,Picasso.Fresco和Glide五大Android开源组件加载网络图片比较 在Android中的加载网络图片是一件十 ...
- DDGScreenShot —图片加各种滤镜高逼格操作
写在前面 图片加各种滤镜操作,当然苹果给开发者提供了相关的api和封装, 大部分开发者感觉这是这是晦涩难懂的,接下来就让我们来了解一下, 其实也没有那么深不可测. 代码如下(每一步已经解释的很详细) ...
- android Glide图片加载框架的初探
一.Glide图片加载框架的简介 谷歌2014年开发者论坛会上介绍的图片加载框架,它让我们在处理不管是网路下载的图片还是本地的图片,减轻了很多工作量, 二.开发步骤: 1.添加链接库 compile ...
- C# 图片的裁剪,两个图片合成一个图片
图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary> /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_ne ...
- android 照相或从相册获取图片并裁剪
照相或从相册获取图片并裁剪 在android应用中很多时候都要获取图片(例如获取用户的头像)就需要从用户手机上获取图片.可以直接照,也可以从用户SD卡上获取图片,但获取到的图片未必能达到要求.所以要对 ...
- 详细解释如何通过Android自带的方式来实现图片的裁剪——原理分析+解决方案
我们很多时候需要进行图片的裁剪,其实这个功能在android系统中已经有一套解决方案了,虽然界面和效果并不是很优秀但功能毫无疑问是完美实现了.至于,不用自带的方案怎么做自定义,这个就是后话了.本篇主要 ...
- android开发——从相冊中选择图片不裁剪
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46864777 问题: 在郭神的第一行代码中,第8章的从相冊中选择图片这块,从相冊选 ...
- 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放
如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...
随机推荐
- Python 字符编码-文件处理
.read #读取所有内容,光标移动到文件末尾.readable #判断文件是否可读.readline #读取一行内容,光标移动到第二行首部.readlines #读取每一行内容,存放于列表中.wri ...
- MyBatis 实现分页功能
MySQL 的分页功能是基于内存的分页(即查出来所有记录,再按起始位置和页面容量取出结果). 案例:①根据用户名(支持模糊查询).用户角色 id 查询用户列表(即根据用户名称或根据用户角色 id 又或 ...
- linux core dump 生成和调试
core dump 某些信号的产生会导致产生core dump,包含了进程终止时的内存镜像.在某些时候这个core文件就非常的有用处,配合gdb或者lldb调试起来非常方便. 更详细的文档参考 Lin ...
- Django中配置自定义日志系统
将
- 设计方案--移动端延迟300ms的原因以及解决方案
一.前言 移动端浏览器提供一个特殊的功能:双击(double tap)缩放. 二.移动端延迟300ms的原因 为什么要用触摸事件?触摸事件是移动端浏览器特有的html5事件. 因为移动端的clic ...
- xtu Shortest Path
Acceteped : 23 Submit : 61 Time Limit : 5000 MS Memory Limit : 65536 KB Description 题目描述 N(3≤N≤1 ...
- Linux环境下使用VSCode编译makefile文件的注意事项
Linux环境下使用VSCode编译makefile文件的注意事项 首先安装C/C++的两个依赖 在debug,launch会自动的生成下方的launch.json launch.json { // ...
- Codeforces Round #352 (Div. 2),A题与B题题解代码,水过~~
->点击<- A. Summer Camp time limit per test 1 second memory limit per test 256 megabytes input s ...
- [luoguP1494] 岳麓山上打水 && [luoguP2744] [USACO5.3]量取牛奶Milk Measuring
传送门 传送门 dfs选取集合,dp背包判断 虽然我觉的会TLE.. 但是的确是AC了 #include <cstdio> #include <cstring> #includ ...
- 删除右键open foler as pycharm project(WIN10)
1.打开注册表(WIN+R 输入regedit) 2.找到 HKEY_CLASSES_ROOT\Directory\Background 路径 下找到Parcharm文件夹,删除,右键的open fo ...