关于Glide图片加载框架的使用详解

首先是普通的加载

效果图
关于Glide图片加载框架的使用详解_第1张图片
代码实现

   Glide.with(this).load(url).into(iv_demo);

填充式的加载图片

效果图
关于Glide图片加载框架的使用详解_第2张图片
代码实现:

 Glide.with(this).load(url).centerCrop().into(iv_demo);

加载圆形图片

效果图
关于Glide图片加载框架的使用详解_第3张图片
代码实现(需要自定义实现GlideCircleTransform这个类)

 Glide.with(this).load(url).transform(new GlideCircleTransform(this)).into(iv_demo);

加载带边角的图片

效果图
关于Glide图片加载框架的使用详解_第4张图片
代码实现(需要自定义实现GlideRoundTransform这个类)

  Glide.with(this).load(url).transform(new GlideRoundTransform(this)).into(iv_demo);

加载本地图片

代码实现

Glide.with(this).load(R.drawable.ic_launceher).transform(new GlideRoundTransform(this)).into(iv_demo);

加载sd卡图片

代码实现

Glide.with(this).load(path).transform(new GlideRoundTransform(this)).into(iv_demo);

怎么样是不是超简单用起来.圆角或者圆形图片自定义的两个类

package com.example.yukuo.glidedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/** * Created by yukuo on 2016/3/21. * 这是一个将网络图片转换为圆形的方法 */
public class GlideCircleTransform extends BitmapTransformation {
    public GlideCircleTransform(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private Bitmap circleCrop(BitmapPool pool, Bitmap toTransform) {
        if (toTransform == null) return null;
        int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
        int x = (toTransform.getWidth() - size) / 2;
        int y = (toTransform.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(toTransform, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName();
    }
}
package com.example.yukuo.glidedemo;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/** * Created by yukuo on 2016/3/21. * 这是一个加载网络图片设置圆角的类 */
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}

ok到此结束,有什么不懂的请留言给我

你可能感兴趣的:(框架,图片,Glide)