Glide 知识梳理(3) 自定义transform

1、概述

有时候,当咱们去服务器请求图片资源成功以后,但愿先对它进行一些处理,例如缩放、旋转、蒙灰等,而后再把处理后的图片展现在控件上,这时候就能够用上Glide提供的transform()方法,简单来讲,transform()的做用就是改变原始资源在客户端上最终的展示结果canvas

2、示例

2.1 采用BitmapTransformation进行变换

首先,transform(xxx)接收的参数类型有两种,一种是BitmapTransformation,另外一种是Transformtion<GifBitmapWrapper>,下面是它们的定义,在平时的使用过程当中,若是咱们的资源为静态图片,而不是Gif或者Media,那么经过继承BitmapTransformation来实现本身的变换就能够了。缓存

//接收BitmapTransformation做为参数.
    public DrawableRequestBuilder<ModelType> transform(BitmapTransformation... transformations) {
        return bitmapTransform(transformations);
    }
    //接收Transformation做为参数.
    @Override
    public DrawableRequestBuilder<ModelType> transform(Transformation<GifBitmapWrapper>... transformation) {
        super.transform(transformation);
        return this;
    }
复制代码

下面,咱们就以一下简单的例子,来看一下如何使用BitmapTransformation来改变图片的展现结果。 这里的变换用到了前面介绍过的setShader相关的知识,能够参考下面这篇文章:bash

http://www.jianshu.com/p/6ab058329ca8服务器

首先,定义本身的BitmapTransformation实现:app

private class MyBitmapTransformation extends BitmapTransformation {

        public MyBitmapTransformation(Context context) {
            super(context);
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Canvas canvas = new Canvas(toTransform);
            BitmapShader bitmapShader = new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            int min = Math.min(toTransform.getWidth(), toTransform.getHeight());
            int radius = min / 2;
            RadialGradient radialGradient = new RadialGradient(toTransform.getWidth() / 2 , toTransform.getHeight() / 2, radius, Color.TRANSPARENT, Color.WHITE, Shader.TileMode.CLAMP);
            ComposeShader composeShader = new ComposeShader(bitmapShader, radialGradient, PorterDuff.Mode.SRC_OVER);
            Paint paint = new Paint();
            paint.setShader(composeShader);
            canvas.drawRect(0, 0, toTransform.getWidth(), toTransform.getHeight(), paint);
            return toTransform;
        }

        @Override
        public String getId() {
            return "MyBitmapTransformation";
        }
    }
复制代码

获得了BitmapTransform以后,经过下面的方式来加载图片资源,并传入咱们定义的Transformationide

public void loadTransform(View view) {
        MyBitmapTransformation myBitmapTransformation = new MyBitmapTransformation(this);
        Glide.with(this)
                .load("http://i.imgur.com/DvpvklR.png")
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transform(myBitmapTransformation)
                .into(mImageView);
    }
复制代码

最终获得的结果以下图所示: ui

2.2 使用多个BitmapTransformation

从上面的定义中,能够看到transform接收的参数个数是可变,也就是说,咱们能够传入多个Transformation进行组合,它们会按顺序应用到最终的图像上。this

3、方法说明

看完上面的应用以后,咱们去源码中看一下各方法的说明。spa

3.1 Transformation<T>

BitmapTransformation是实现了Transformation<T>接口的抽象类,源码当中对于Transformation的描述是:code

/**
 * A class for performing an arbitrary transformation on a resource.
 *
 * @param <T> The type of the resource being transformed.
 */
复制代码

BitmapTransform的参数T就是Bitmap,也就是说它是用来给Bitmap进行变换,Transformation定义了两个接口:

  • Resource<T> transform(Resource<T> resource, int outWidth, int outHeight) resourceget方法会返回原始资源,它多是从服务器上面下载的图片,也多是上一个transformation变换后的资源,而返回值就是通过变化后的resource,而outWidth/outHeight就是咱们所要加载到的目标Target的宽高,在上面的例子中,就是ImageView所指定的宽高。
  • String getId() 前面在介绍基础使用的文章当中,咱们说过最终缓存的资源文件的Cache key会依赖于一些因素,这个getId方法的返回值就是其中的一个因素。

3.2 BitmapTransformation

BitmapTransformation是实现了Transformation的抽象类,它实现了transform(Resource<T> resource, int outWidth, int outHeight)这个方法,并在里面调用了下面这个方法让子类去实现,也就是咱们例子中实现的抽象方法,这主要是方便咱们经过BitmapPoolBitmap对象进行复用,同时使用者只用关心须要变换的BitmapBitmapTransformation会负责把它更新回原来的Resource对象中。

protected abstract Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight);
复制代码

对于这个方法的实现,有几个须要注意的点:

  • 对于传入的toTransform Bitmap,不要去回收它或者把它放入到缓存池。若是实现者返回了一个不一样的Bitmap实例,Glide会负责去回收或者复用toTransform这个Bitmap
  • 不要去回收做为这个方法返回值的bitmap
  • 若是在这个方法的执行过程当中产生了临时的Bitmap实例,那么最好把它放入缓存池,或者回收它。
相关文章
相关标签/搜索