Android之Glide图像处理

经过了一段时间的忙碌,终于有一点点自己的学习时间,后悔在前段时间的开发中遇到好多有趣的问题,虽然解决了,但是都没有时间记录下来,准备接下来补上。看到了Glide圆角加载后,深入了解才知道还有其他的变换,Glide不仅是加载图片和缓存图片的工具,同样是实现图像转换的强大工具。

针对Glide的V3版(文末追加V4版本的图片圆角和圆形处理)

以 Glide Transformations为例:Glide Transformations为Glide提供了图像剪裁、模糊、蒙版、色彩过滤等功能。

基本配置:

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'
}

简要说明:

//with(this) :使用Glide需要一个Context传入。
//load(url) :加载图片的地址,可以是本地图片id、文件路径、网络图片地址等。
//into(mImageView1):加载完图片后需要在哪个ImageView中显示
//bitmapTransform(new CropTransformation(this)):位图转换
Glide.with(this)
     .load(url)
     .bitmapTransform(new CropTransformation(this))
     .into(mImageView);

图片剪裁:

  • CropCircleTransformation(圆形剪裁)
// 使用构造方法 CropCircleTransformation(Context context)
Glide.with(this)
     .load(url)
     .bitmapTransform(new CropCircleTransformation(this))
     .into(mImageView);
  • CropSquareTransformation(正方形剪裁)
// 使用构造方法 CropSquareTransformation(Context context)
Glide.with(this)
     .load(url)
     .bitmapTransform(new CropSquareTransformation(this))
     .into(mImageView);
  • 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(mImageView);
  • 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(mImageView);

注:使用CropTransformation一个参数的构造方法:只填Context会使用图片原本的宽高进行剪裁,但图片大小没有改变。

颜色转换:

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);

模糊处理:

  • 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(mImageView);

注:模糊处理是做过兼容的,当API>=18时使用RenderScript,API<18时使用FastBlur。

遮罩掩饰(视图叠加处理

  • MaskTransformation
// 使用构造方法 MaskTransformation(Context context, int maskId)
  // maskId :遮罩物文件ID
  Glide.with(this)
          .load(url)
          .bitmapTransform(new MaskTransformation(this, R.mipmap.ic_launcher))
          .into(mImageView);

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(mImageView);
  • 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(mImageView);
  • 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(mImageView);
  • 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(mImageView);
  • 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(mImageView);
  • 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(mImageView);

Glide的V4版本

针对Glide的V4版本,进行圆角、圆形处理

添加依赖:在module的gradle中添加:

implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

 开放网络权限:

 <uses-permission android:name="android.permission.INTERNET"/>

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/circle"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <ImageView
        android:id="@+id/round1"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <ImageView
        android:id="@+id/round2"
        android:layout_width="120dp"
        android:layout_height="120dp" />

</LinearLayout>

 代码中使用:

public class MainActivity extends AppCompatActivity {
    ImageView circle, round1, round2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        circle = findViewById(R.id.circle);
        round1 = findViewById(R.id.round1);
        round2 = findViewById(R.id.round2);

        Glide.with(this)
                .load("http://img5.duitang.com/uploads/item/201506/07/20150607110911_kY5cP.jpeg")
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(circle);

        Glide.with(this)
                .load("http://img.jiuzheng.com/memberlogo/s/57/0a/570af0f48f1e0327178b468d.jpg")
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))//圆角半径
                .into(round1);

        Glide.with(this)
                .load("http://img.jiuzheng.com/memberlogo/s/57/0a/570af0f48f1e0327178b468d.jpg")
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))//圆角半径
                .into(round2);
    }
}

使用GlideApp

在Glide4.0之后load(url)之后就不能调用.placeholder()等方法,4.0+的文档发现需要通过GlideApp来调用一系列方法

新建一个类继承AppGlideModule,类添加GlideModule:

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {
      //可以配置Glide
}

然后,Make Module一下,即可使用。

 

Android图片框架Glide-3.7.0(最新,很强大),超好用的图片框架(包含jar和源码) Glide 是一个高效、开源、 Android设备上的媒体管理框架,它遵循BSD、MIT以及Apache 2.0协议发布。Glide具有获取、解码和展示视频剧照、图片、动画等功能,它还有灵活的API,这些API使开发者能够将Glide应用在几乎任何网络协议栈里。创建Glide的主要目的有两个,一个是实现平滑的图片列表滚动效果,另一个是支持远程图片的获取、大小调整和展示。近日,Glide 3.0发布,现已提供 jar包下载 ,同时还支持使用Gradle以及Maven进行构建。该版本包括很多值得关注的新功能,如支持Gif 动画和视频剧照解码、智能的暂停和重新开始请求、支持缩略图等,具体新增功能如下如下: GIF 动画的解码 :通过调用Glide.with(context).load(“图片路径“)方法,GIF动画图片可以自动显示为动画效果。如果想有更多的控制,还可以使用Glide.with(context).load(“图片路径“).asBitmap()方法加载静态图片,使用Glide.with(context).load(“图片路径“).asGif()方法加载动画图片 本地视频剧照的解码: 通过调用Glide.with(context).load(“图片路径“)方法,Glide能够支持Android设备中的所有视频剧照的加载和展示 缩略图的支持: 为了减少在同一个view组件里同时加载多张图片的时间,可以调用Glide.with(context).load(“图片路径“).thumbnail(“缩略比例“).into(“view组件“)方法加载一个缩略图,还可以控制thumbnail()中的参数的大小,以控制显示不同比例大小的缩略图 Activity 生命周期的集成: 当Activity暂停和重启时,Glide能够做到智能的暂停和重新开始请求,并且当Android设备的连接状态变化时,所有失败的请求能够自动重新请求 转码的支持: Glide的toBytes() 和transcode() 两个方法可以用来获取、解码和变换背景图片,并且transcode() 方法还能够改变图片的样式 动画的支持: 新增支持图片的淡入淡出动画效果(调用crossFade()方法)和查看动画的属性的功能 OkHttp 和Volley 的支持: 默认选择HttpUrlConnection作为网络协议栈,还可以选择OkHttp和Volley作为网络协议栈 其他功能: 如在图片加载过程中,使用Drawables对象作为占位符、图片请求的优化、图片的宽度和高度可重新设定、缩略图和原图的缓存等功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值