Glide图片加载框架学习及常用方法封装

一直以来只知道蒙头蒙脑的码代码,自以为所向披靡独当一面的时候,发现外面早就变了天。。。

当我把Eclipse用的熟透熟透,自己封装好的网络请求框架、支付宝支付、微信支付、百度地图、高德地图、QQ分享、新浪微博分享、微信分享、第三方登录等等吧,这些代码已经千锤百炼,自以为关中之固,金城千里,擦擦擦,Android已经进入了6.0的时代了,程序员都在使用的是Android Studio,HttpClient据说逐渐弃用,当我还在感叹Android-Universal-Image-Loader很牛逼的时候,什么picasso、glide、fresco图片类加载框架如雨后春笋般出现了,为了跟上潮流,最近学习了一下Android Studio,尼玛卡的一个皮,估计是功能太强大了,好吧 不扯淡了,上正文:

Glide图片加载框架学习及常用方法封装

github地址:https://github.com/bumptech/glide

因为项目中引用了OkHttp框架,都是一家公司的项目,我这里的依赖如下:

compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
实现效果图如下:

Glide图片加载框架学习及常用方法封装_第1张图片 Glide图片加载框架学习及常用方法封装_第2张图片

GlideTestActivity.java:

package com.czhappy.kuaizhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.czhappy.kuaizhi.R;
import com.czhappy.kuaizhi.glide.GlideImgManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description:
 * User: chenzheng
 * Date: 2016/9/8 0008
 * Time: 09:01
 */
public class GlideTestActivity extends BaseActivity {

    @BindView(R.id.imageview1)
    ImageView imageview1;
    @BindView(R.id.imageview2)
    ImageView imageview2;
    @BindView(R.id.imageview3)
    ImageView imageview3;
    @BindView(R.id.imageview4)
    ImageView imageview4;
    @BindView(R.id.imageview5)
    ImageView imageview5;
    @BindView(R.id.textview1)
    Button textview1;
    @BindView(R.id.imageview6)
    ImageView imageview6;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    private String imageArr[] = {"http://pic14.nipic.com/20110607/6776092_111031284000_2.jpg",
            "http://cdn.duitang.com/uploads/item/201507/13/20150713235634_UKdLB.jpeg",
            "http://att2.citysbs.com/taizhou/2011/08/27/101937_kuumaaio_40ee6a85b8df443965c4ca5e6f5d80fa.jpg"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_glide_test);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        //加载网络图片(普通)
        GlideImgManager.loadImage(this, imageArr[0], imageview1);

        //加载网络图片(圆角)
        GlideImgManager.loadRoundCornerImage(this, imageArr[1], imageview2);

        //加载网络图片(圆形)
        GlideImgManager.loadCircleImage(this, imageArr[2], imageview3);

        //加载项目中的图片
        GlideImgManager.loadImage(this, R.mipmap.ic_launcher, imageview4);

        //加载网络图片(GIF)
        String gifUrl = "http://ww4.sinaimg.cn/mw690/bcc93f49gw1f6r1nce77jg207x07sx6q.gif";
        GlideImgManager.loadGifImage(this, gifUrl, imageview5);
    }

    //加载进度监听
    private void loadListener() {
        Glide.with(this)
                .load(imageArr[0])
                .into(new GlideDrawableImageViewTarget(imageview6) {
                    @Override
                    public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
                        super.onResourceReady(drawable, anim);
                        progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        progressBar.setVisibility(View.VISIBLE);
                    }
                });
    }

    @OnClick(R.id.textview1)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textview1:
                loadListener();
                break;
        }
    }
}


activity_glide_test.xml:




    

        

        

        

        

        

        

下面就是代码中所用到的Glide加载图片时所用到的工具类:

GlideImgManager.java:

package com.czhappy.kuaizhi.glide;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.czhappy.kuaizhi.R;

import java.io.File;

/**
 * Description:
 * User: chenzheng
 * Date: 2016/8/31 0031
 * Time: 15:43
 */
public class GlideImgManager {

    public static void loadImage(Context context, String url, int erroImg, int emptyImg, ImageView iv) {
        //原生 API
        Glide.with(context).load(url).placeholder(emptyImg).error(erroImg).into(iv);
    }

    public static void loadImage(Context context, String url, ImageView iv) {
        //原生 API
        Glide.with(context).load(url).crossFade().placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).into(iv);
    }

    public static void loadGifImage(Context context, String url, ImageView iv) {
        Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).into(iv);
    }


    public static void loadCircleImage(Context context, String url, ImageView iv) {
        Glide.with(context).load(url).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).transform(new GlideCircleTransform(context)).into(iv);
    }

    public static void loadRoundCornerImage(Context context, String url, ImageView iv) {
        Glide.with(context).load(url).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).transform(new GlideRoundTransform(context,10)).into(iv);
    }


    public static void loadImage(Context context, final File file, final ImageView imageView) {
        Glide.with(context)
                .load(file)
                .into(imageView);


    }

    public static void loadImage(Context context, final int resourceId, final ImageView imageView) {
        Glide.with(context)
                .load(resourceId)
                .into(imageView);
    }
}

GlideCircleTransform.java:

package com.czhappy.kuaizhi.glide;

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;

/**
 * Description:
 * User: chenzheng
 * Date: 2016/8/31 0031
 * Time: 15:42
 */
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();
    }
}

GlideRoundTransform.java:

package com.czhappy.kuaizhi.glide;

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;

/**
 * Description:
 * User: chenzheng
 * Date: 2016/8/31 0031
 * Time: 15:42
 */
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);
    }
}

当前工具类支持网络图片加载、圆角图片、圆形图片、手机本地图片、项目图片、gif图片等等,简单实用




你可能感兴趣的:(Android进阶)