Glide的一些对图片的简单处理

还是借鉴的其他博主的,自己截取了一段
推荐使用独立的图片处理库:wasabeef/glide-transformations,使用也很简单:
也就是添加依赖

 compile 'jp.wasabeef:glide-transformations:2.0.0'

之后我们就可以使用GenericRequestBuilder或其子类的transform()或bitmapTransform()方法设置图片转换了:

 //圆形裁剪
    Glide.with(this)
        .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
        .bitmapTransform(new CropCircleTransformation(this))
        .into(iv_0);
    //圆角处理
    Glide.with(this)
        .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
        .bitmapTransform(new RoundedCornersTransformation(this,30,0, RoundedCornersTransformation.CornerType.ALL))
        .into(iv_0);
    //灰度处理
    Glide.with(this)
        .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
        .bitmapTransform(new GrayscaleTransformation(this))
        .into(iv_0);
    //其它变换...

还可以自己写Transformation:
最简单的方式就是继承BitmapTransformation:

private static class MyTransformation extends BitmapTransformation {

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

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, 
            int outWidth, int outHeight) {
       Bitmap myTransformedBitmap = ... //对Bitmap进行各种变换处理。
       return myTransformedBitmap;
    }

    @Override
    public String getId() {
        // 返回代表该变换的唯一Id,会作为cache key的一部分。
        // 注意:最好不要用getClass().getName(),因为容易受混淆影响。如果变换过程不影响缓存数据,可以返回空字符串。
        return "com.example.myapp.MyTransformation";
    }
}

使用也是很简单的

Glide.with(yourFragment)
    .load(yourUrl)
    .asBitmap()
    .transform(new MyTransformation(context))
    .into(yourView);

自定义图片处理时Glide会自动计算View/Target大小,我们不需要传View的宽高,当然你可以使用override(int, int)去改变这种行为。

你可能感兴趣的:(Glide的一些对图片的简单处理)