Glide框架------加載圖片部分

Glide是一個快速高效的Android媒體管理框架,將媒體解碼、內存和硬盤緩存、資源池包裝成簡單和易於使用的接口。Glide支持獲取、解碼和顯示視頻文件、圖片和動畫GIF文件。包含了一個靈活的API,藉助以API開發者能夠將Glide應用到大多數的網絡協議棧中。默認情況下,Glide使用了基於stack的定製HttpUrlConnection,但是還包含了使用工具類將Glide應用到Google's Volley項目或Square's OkHttp類庫。

Glide的首要目標是儘可能的使任意類型的圖片列表快速流暢的滾動,另一個目標是高效的獲取、顯示遠程圖片以及調整遠程圖片的大小



1、加載普通圖片

代碼:

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

wKiom1gr-uXTYHx7AAGNk0eJjWw230.png-wh_50

2、填充式加載圖片

代碼:

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

wKiom1gr-13gIhjIAAHceM_UfRs315.png-wh_50

3、加載圓形圖片

代碼:

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

wKioL1gr-7jgUGQ_AANYiKO-yT8273.png-wh_50

GlideCircleTransform類:

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



4、加載圓角圖片

代碼:

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

wKioL1gr--vigHfxAAR5GxaB9p8848.png-wh_50

GlideRoundTransform 類:

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


5、加載本地圖片

代碼:

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

6、加載SD卡圖片

代碼:

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


轉自:http://blog.csdn.net/easkshark/article/details/51374049

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章