Android開發:使用Glide動態載入圓形圖片和圓角圖片,

來源:互聯網
上載者:User

Android開發:使用Glide動態載入圓形圖片和圓角圖片,

最新訊息,鼎鼎大名的Yelp應用也轉投Glide的陣營了,而且Glide在跟Listview的配合起來非常的順暢,Glide除了配置簡單,還可以本機快取圖片,也可以實現Listview圖片的提前預先載入,使得listview的更加的順滑,具體可以查看Yelp的那篇博文。

但是如果碰到要把載入下來的圖片轉成圓角或者圓形的圖片,怎麼處理呢,Glide原生沒有這個方法,於是我拓展了BitmapTransformation來實現這個功能。

請先看:

Glide是預設本機存放區的,但重複載入的時候,是不需要重複訪問網路。

下載的圖片轉圓形的方法

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 static Bitmap circleCrop(BitmapPool pool, Bitmap source) {        if (source == null) return null;        int size = Math.min(source.getWidth(), source.getHeight());        int x = (source.getWidth() - size) / 2;        int y = (source.getHeight() - size) / 2;        // TODO this could be acquired from the pool too        Bitmap squared = Bitmap.createBitmap(source, 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();    }}

自訂一個extend BitmapTransformation的方法,把獲得的bitmap轉化成圓形圖片,下面是使用方法

private RequestManager glideRequest;glideRequest = Glide.with(this);glideRequest.load("https://www.baidu.com/img/bdlogo.png").transform(new GlideCircleTransform(context)).into(imageView);

這裡不得不強調下Glide的一個強大的功能,當你在With後面的傳Activity或者Fragment,Glide就可以根據當前Activity或者Fragment的生命週期維護圖片的生命週期,比如但activity銷毀的時候,就會自動取消需要載入的圖片

下載圖片轉換層圓角圖片的方法

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

在這個方法裡面,你可以自訂圓角的大小,使用方式也非常簡單

glideRequest.load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context)).into(imageView);glideRequest.load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context, 10)).into(imageView);
Glide的詳細的使用方法可以參考官方的文檔,這裡是作為載入圖片效果的一個補充,一人覺得目前Glide已經很成熟了,可以考慮應用到實際的項目中去,目前還有一個很強大的圖片載入架構,叫做Fresco,Facebook開發的,star的數量是超過Glide的,不過本人還是傾向於使用Glide,基本上Glide已經可以滿足我所有的功能,再加片的圓角處理這裡也實現了,至於Fresco個人感覺偏複雜了點,還是Glide使用起來比較輕便,故推薦Glide。

這個文章的源碼我也放出來了,可以點擊這個連結查看

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.