Glide

加載圓角圖片(上下文,圖片,控件)GlideImage.setGlideImageView(context,company.getLogo(),viewHolder1.iv_img);
加載圓圈輪廓GlideImage.setGlideImageView(mContext,company.getLogo(),logo);

public class GlideCircleTransform extends BitmapTransformation {
    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }

    public GlideCircleTransform(Context 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;
        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;
    }
}
public class GlideRoundNewTransform extends BitmapTransformation {

    private static float radius = 0f;

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

    public GlideRoundNewTransform(Context context, int dp) {
         super();
        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 bitmap = changeBitmapSize(source);
        Bitmap result = pool.get(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
        if (result == null) {

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            result = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }
    public static Bitmap changeBitmapSize(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //設(shè)置想要的大小
        int newWidth=72;
        int newHeight=72;
        //計(jì)算壓縮的比率
        float scaleWidth=((float)newWidth)/width;
        float scaleHeight=((float)newHeight)/height;
        //獲取想要縮放的matrix
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);
        //獲取新的bitmap
        bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
        bitmap.getWidth();
        bitmap.getHeight();
        Log.e("newWidth","newWidth"+bitmap.getWidth());
        Log.e("newHeight","newHeight"+bitmap.getHeight());
        return bitmap;
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}
public class GlideRoundTransform extends BitmapTransformation {
    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }

    private static float radius = 0f;

    /**
     * 構(gòu)造函數(shù) 默認(rèn)圓角半徑 4dp
     * @param context Context
     */
    public GlideRoundTransform(Context context) {
        this(context, 6);
        radius = Resources.getSystem().getDisplayMetrics().density * 6;
    }
    /**
     * 構(gòu)造函數(shù)
     * @param context Context
     * @param dp 圓角半徑
     */
    public GlideRoundTransform(Context context, int dp) {
        super();
        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;
    }
}
public class GlideImage {
//        普通加載圖片
    public static void setImg(Context context, String url, ImageView imageView){
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(R.mipmap.main_hot_summary_default_img)
                .error(R.mipmap.main_hot_summary_default_img);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }
//    加載方角圖片
    public static void setRoundImg(Context context, String url, ImageView imageView,int num){
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .dontAnimate()
                .transforms( new CenterCrop(),new GlideRoundTransform(context,num));
        //Glide 加載圖片簡(jiǎn)單用法
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    //    圖片
    public static void setGlideImageView(Context context, String url, ImageView imageView){
        int width = (int) ScreenUtils.dp2px(context, 60);
        int height = (int) ScreenUtils.dp2px(context, 50);
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .override(width,height)
                .placeholder(R.mipmap.company_default)
                .error(R.mipmap.company_default)
                .centerInside()
                .dontAnimate();
//                .transforms(new CenterCrop(), new GlideCircleTransform(context));
        //Glide 加載圖片簡(jiǎn)單用法
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }


//    加載圓角圖片
    public static void setCircleImg(Context context, String url, ImageView imageView){
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.main_hot_summary_default_img)
                    .error(R.mipmap.main_hot_summary_default_img)
                    .transform(new CenterCrop(), new GlideCircleTransform(context));
            //Glide 加載圖片簡(jiǎn)單用法
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(imageView);
    }
    //    加載方角圖片
    public static void setRoundBitmap(Context context, String url, ImageView imageView, int num){
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.main_hot_summary_default_img)
                .error(R.mipmap.main_hot_summary_default_img)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .dontAnimate()
                .transform( new CenterCrop(),new GlideRoundTransform(context,num));
        //Glide 加載圖片簡(jiǎn)單用法
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末脊岳,一起剝皮案震驚了整個(gè)濱河市宗侦,隨后出現(xiàn)的幾起案子颊糜,更是在濱河造成了極大的恐慌,老刑警劉巖猿规,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異宙橱,居然都是意外死亡姨俩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門师郑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)环葵,“玉大人,你說(shuō)我怎么就攤上這事宝冕≌旁猓” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵地梨,是天一觀的道長(zhǎng)菊卷。 經(jīng)常有香客問(wèn)我,道長(zhǎng)湿刽,這世上最難降的妖魔是什么的烁? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮诈闺,結(jié)果婚禮上渴庆,老公的妹妹穿的比我還像新娘。我一直安慰自己雅镊,他們只是感情好襟雷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著仁烹,像睡著了一般耸弄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上卓缰,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天计呈,我揣著相機(jī)與錄音砰诵,去河邊找鬼。 笑死捌显,一個(gè)胖子當(dāng)著我的面吹牛茁彭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播扶歪,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼理肺,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了善镰?” 一聲冷哼從身側(cè)響起妹萨,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎炫欺,沒(méi)想到半個(gè)月后乎完,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡竣稽,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年囱怕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片毫别。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡娃弓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出岛宦,到底是詐尸還是另有隱情台丛,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布砾肺,位于F島的核電站挽霉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏变汪。R本人自食惡果不足惜侠坎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望裙盾。 院中可真熱鬧实胸,春花似錦、人聲如沸番官。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)徘熔。三九已至门躯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間酷师,已是汗流浹背讶凉。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工染乌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缀遍。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓慕匠,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親域醇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容