Glide的簡單封裝

版權聲明:本文爲延成原創文章,轉載請標明出處

public final class ImageLoader {

    private ImageLoader() {
        throw new RuntimeException("ImageLoader cannot be initialized!");
    }
    
    public static void loadFit(Context context, String url, ImageView view, int defaultResId) {
        Glide.with(context).load(url).fitCenter().dontAnimate().placeholder(defaultResId).into(view);
    }

    public static void loadCenterCrop(Context context, String url, ImageView view, int defaultResId) {
        Glide.with(context).load(url).centerCrop().dontAnimate().placeholder(defaultResId).into(view);
    }

    public static void loadFitCenter(Context context, String url, ImageView view, int defaultResId) {
        Glide.with(context).load(url).fitCenter().dontAnimate().placeholder(defaultResId).into(view);
    }

    public static void loadCircleCrop(Context context, String url, ImageView view, int defaultResId) {
        Glide.with(context).load(url).circleCrop().dontAnimate().placeholder(defaultResId).into(view);
    }

    //設置跳過內存緩存
    public static void loadImageViewCache(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).skipMemoryCache(true).into(mImageView);
    }

    //設置縮略圖支持
    public static void loadImageViewThumbnail(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).thumbnail(0.1f).into(mImageView);
    }


    /**
     * 帶監聽處理
     *
     * @param context
     * @param url
     * @param view
     * @param listener
     */
    public static void loadFitCenter(Context context, String url, ImageView view, RequestListener listener) {
        Glide.with(context).load(url).fitCenter().dontAnimate().listener(listener).into(view);
    }

    public static void loadCenterCrop(Context context, String url, ImageView view, RequestListener listener) {
        Glide.with(context).load(url).centerCrop().dontAnimate().listener(listener).into(view);
    }

    /**
     * 設置圖片大小處理
     *
     * @param context
     * @param url
     * @param view
     * @param defaultResId
     * @param width
     * @param height
     */
    public static void loadFitOverride(Context context, String url, ImageView view, int defaultResId, int width, int height) {
        Glide.with(context).load(url).fitCenter().dontAnimate().override(width, height).placeholder(defaultResId).into(view);
    }

    /**
     * 計算圖片分辨率
     *
     * @param context
     * @param url
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static String calePhotoSize(Context context, String url) throws ExecutionException, InterruptedException {
        File file = Glide.with(context).load(url)
                .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        return options.outWidth + "*" + options.outHeight;
    }

    //清理磁盤緩存
    public static void GuideClearDiskCache(Context mContext) {
        //理磁盤緩存 需要在子線程中執行
        Glide.get(mContext).clearDiskCache();
    }

    //清理內存緩存
    public static void GuideClearMemory(Context mContext) {
        //清理內存緩存  可以在UI主線程中進行
        Glide.get(mContext).clearMemory();
    }

}
發佈了151 篇原創文章 · 獲贊 23 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章