DevWiki

Fresco加载图片封装
使用第三方库的原则:最好添加一层自己的封装,不要直接使用。防止替换库带来的麻烦!以下是使用Fresco加载图片时的...
扫描右侧二维码阅读全文
15
2016/10

Fresco加载图片封装

使用第三方库的原则:最好添加一层自己的封装,不要直接使用。防止替换库带来的麻烦!

以下是使用Fresco加载图片时的简单封装,以备不时之需。

/**
 * 图片加载的封装
 * Created by DevWiki on 2016/8/6.
 */
public class ImageLoader {

    private static RoundingParams circleParams;
    static {
        circleParams = new RoundingParams();
        circleParams.setRoundAsCircle(true);
    }

    public static RoundingParams getCircleParams() {
        return circleParams;
    }

    private ImageLoader(){}

    public static void loadImageRes(Context context, @DrawableRes int resId, SimpleDraweeView view) {
        Uri uri = Uri.parse("res://" + context.getPackageName() + "/" + resId);
        loadImage(uri, view);
    }

    public static void loadImageResAsCircle(Context context, @DrawableRes int resId, SimpleDraweeView view) {
        Uri uri = Uri.parse("res://" + context.getPackageName() + "/" + resId);
        loadImageAsCircle(uri, view);
    }

    public static void loadImageFile(String filePath, SimpleDraweeView view) {
        Uri uri = Uri.parse("file://" + filePath);
        loadImage(uri, view);
    }

    public static void loadImageFileAsCircle(String filePath, SimpleDraweeView view) {
        Uri uri = Uri.parse("file://" + filePath);
        loadImageAsCircle(uri, view);
    }

    public static void loadNetImage(String url, SimpleDraweeView view) {
        Uri uri = Uri.parse(url);
        loadImage(uri, view);
    }

    public static void loadNetImageAsCircle(String url, SimpleDraweeView view) {
        Uri uri = Uri.parse(url);
        loadImageAsCircle(uri, view);
    }

    public static void loadImage(Uri uri, SimpleDraweeView view) {
        GenericDraweeHierarchy hierarchy =
                new GenericDraweeHierarchyBuilder(FrescoApp.getContext().getResources())
                        .setFadeDuration(300)
                        .setPlaceholderImage(R.drawable.bg_loading_gif)
                        .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                        .build();
        view.setHierarchy(hierarchy);
        view.setImageURI(uri);
    }

    /**
     * 加载图片为圆形图片
     * @param uri 图片的uri
     * @param view 要加载的视图
     */
    public static void loadImageAsCircle(Uri uri, SimpleDraweeView view) {
        GenericDraweeHierarchy hierarchy =
                new GenericDraweeHierarchyBuilder(FrescoApp.getContext().getResources())
                        .setFadeDuration(300)
                        .setRoundingParams(circleParams)
                        .setPlaceholderImage(R.drawable.bg_loading_gif)
                        .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                        .build();
        view.setHierarchy(hierarchy);
        view.setImageURI(uri);
    }

    /**
     * 加载Gif资源
     * @param resId Gif的资源Id
     * @param view 要填充的View
     */
    public static void loadGifRes(Context context, @DrawableRes int resId, SimpleDraweeView view) {
        Uri uri = Uri.parse("res://" + context.getPackageName() + "/" + resId);
        loadGif(uri, view);
    }

    /**
     * 加载Gif文件
     * @param filePath Gif的路径
     * @param view 要填充的View
     */
    public static void loadGifFile(String filePath, SimpleDraweeView view) {
        Uri uri = Uri.parse("file://" + filePath);
        loadGif(uri, view);
    }

    /**
     * 加载网络Gif
     * @param url Gif的URI
     * @param view 要填充的View
     */
    public static void loadNetGif(String url, SimpleDraweeView view) {
        Uri uri = Uri.parse(url);
        loadGif(uri, view);
    }

    /**
     * 加载Gif
     * @param uri Gif的URI
     * @param view 要填充的View
     */
    public static void loadGif(Uri uri, SimpleDraweeView view) {
        GenericDraweeHierarchy hierarchy =
                new GenericDraweeHierarchyBuilder(FrescoApp.getContext().getResources())
                        .setFadeDuration(300)
                        .setPlaceholderImage(R.drawable.bg_loading_gif)
                        .setPlaceholderImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                        .setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                        .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setRetainImageOnFailure(true)
                .setAutoPlayAnimations(true)
                .build();
        view.setHierarchy(hierarchy);
        view.setController(controller);
    }

    /**
     * 暂停加载图片,一般在列表滚动时调用此方法。<br><br/>
     * 注意:暂停后请在适当时机恢复加载!!!
     */
    public static void pauseLoader() {
        if (!Fresco.getImagePipeline().isPaused()) {
            Fresco.getImagePipeline().pause();
        }
    }

    /**
     * 恢复图片加载,一般在列表停止滚动时调用此方法。
     */
    public static void resumeLoader() {
        if (Fresco.getImagePipeline().isPaused()) {
            Fresco.getImagePipeline().resume();
        }
    }

    /**
     * 清除缓存
     * @param uri 图片的URI
     */
    public static void clearDiskCache(Uri uri) {
        Fresco.getImagePipeline().evictFromDiskCache(uri);
    }

    public static void clearMemoryCache(Uri uri) {
        Fresco.getImagePipeline().evictFromMemoryCache(uri);
    }

    public static void clearCache(Uri uri) {
        clearDiskCache(uri);
        clearMemoryCache(uri);
    }

    /**
     * 清除文件的磁盘缓存
     * @param path 文件路径
     */
    public static void clearFileDiskCache(String path) {
        Uri uri = Uri.parse("file://" + path);
        clearDiskCache(uri);
    }

    /**
     * 清除文件的内存缓存
     * @param path 文件路径
     */
    public static void clearFileMemoryCache(String path) {
        Uri uri = Uri.parse("file://" + path);
        clearMemoryCache(uri);
    }

    /**
     * 清除文件的内存和磁盘缓存
     * @param path 文件路径
     */
    public static void clearFileCache(String path) {
        Uri uri = Uri.parse("file://" + path);
        clearCache(uri);
    }

    /**
     * 清除网络图片的磁盘缓存
     * @param url 链接
     */
    public static void clearUrlDiskCache(String url) {
        Uri uri = Uri.parse(url);
        clearDiskCache(uri);
    }

    /**
     * 清除网络图片的内存缓存
     * @param url 图片链接
     */
    public static void clearUrlMemoryCache(String url) {
        Uri uri = Uri.parse(url);
        clearMemoryCache(uri);
    }

    /**
     * 清除网络图片的内存和磁盘缓存
     * @param url 图片链接
     */
    public static void clearUrlCache(String url) {
        Uri uri = Uri.parse(url);
        clearCache(uri);
    }
}
Last modification:May 29th, 2017 at 03:33 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment