package com.mints.library.utils;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

/**
 * 描述：GlideUtils
 * 作者：孟崔广
 * 时间：2018/1/10 17:51
 * 邮箱：mengcga@163.com
 */
public class GlideUtils {
    /**
     * Glide特点
     * 使用简单
     * 可配置度高，自适应程度高
     * 支持常见图片格式 Jpg png gif webp
     * 支持多种数据源  网络、本地、资源、Assets 等
     * 高效缓存策略    支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
     * 生命周期集成   根据Activity/Fragment生命周期自动管理请求
     * 高效处理Bitmap  使用Bitmap Pool使Bitmap复用，主动调用recycle回收需要回收的Bitmap，减小系统回收压力
     * 这里默认支持Context，Glide支持Context,Activity,Fragment，FragmentActivity
     */

    //默认加载
    public static void loadImageView(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).into(mImageView);
    }

    //设置加载中图片
    public static void loadImageViewLoding(Context mContext, String path, ImageView mImageView, int lodingImage) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(lodingImage);
        Glide.with(mContext).load(path).apply(requestOptions).into(mImageView);
    }

    //设置加载中以及加载失败图片
    public static void loadImageViewLoding(Context mContext, String path, ImageView mImageView, int lodingImage, int errorImageView) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(lodingImage);
        requestOptions.error(errorImageView);
        Glide.with(mContext).load(path).apply(requestOptions).into(mImageView);
    }

    //设置加载中以及加载失败圆形图片
    public static void loadCircleImageView(Context mContext, String path, ImageView mImageView, int lodingImage, int errorImageView) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(lodingImage);
        requestOptions.error(errorImageView);
        requestOptions.circleCrop();
        Glide.with(mContext).load(path).apply(requestOptions).into(mImageView);
    }

    //加载Gif
    public static void loadImageViewGif(Context mContext, int drawable, ImageView imageView) {
        Glide.with(mContext).load(drawable).into(imageView);
    }

    //加载Gif
    public static void loadImageViewGifUrl(Context mContext, ImageView view, String url) {
        RequestOptions options = (new RequestOptions()).diskCacheStrategy(DiskCacheStrategy.RESOURCE).timeout(60000).priority(Priority.HIGH);
        Glide.with(mContext).load(url).apply(options).into(view);
    }
}
