工具类三、Fresco

package com.oldeleven.guaguacartoonversionone.helper;

import android.content.Context;
import android.net.Uri;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by My on 2016/8/8.
 * 封装的FrescoHelper工具类
 * 第一步:导包 compile 'com.facebook.fresco:fresco:0.12.0'
 * 第二步:自定义Application的子类用于初始化框架
 *
 * 单例FrescoHelper:
 * DCL实现的单例虽然一定程度上解决了资源消耗,多余的同步、线程安全问题,但还是在某些情况下回出现失效的问题。
 *
 */
public class FrescoHelper {
    private static Context mContext = null;
    private static FrescoHelper sInstance = null;
    /**
     * 构造器私有化
     * @param context
     */
    private FrescoHelper(Context context) {
        mContext = context;
    }

    /**
     * 获得单例的对象
     * @param context 上下文环境对象
     * @return
     */
    public static FrescoHelper getInstance(Context context){
        if (sInstance == null){
            synchronized (FrescoHelper.class){
                if (sInstance == null){
                    sInstance = new FrescoHelper(context);
                }
            }
        }
        return sInstance;
    }

    /**
     * 不带样式的图片加载
     * @param view
     */
    public static void SimpleImageLoader(SimpleDraweeView view,String imageUrl){
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
        //设置图片的比例 4:3
        view.setAspectRatio(1.33f);
    }

    ///////////////////////////////////////////////////////////////////////////
    // 加载图片的样式设置
    ///////////////////////////////////////////////////////////////////////////


    /**
     * 设置Fresco框架的进度条加载图片
     *
     * @return
     */
    //fresco的特性
    public static GenericDraweeHierarchy getHierarchy() {
        GenericDraweeHierarchy genericDraweeHierarchy = new GenericDraweeHierarchyBuilder(mContext.getResources())
                .setProgressBarImage(new ProgressBarDrawable())
                .build();

        return genericDraweeHierarchy;

    }


    /**
     * @param view   SimpleDraweeView 的对象
     * @param imageUrl  要加载的网络图片的地址
     * @return  返回PipelineDraweeController控制器的对象
     */
    public static PipelineDraweeController getController(SimpleDraweeView view, String imageUrl) {
        //渐进式加载
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl))
                .setProgressiveRenderingEnabled(true).build();
        //初始化控制器
        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(view.getController())
                .setTapToRetryEnabled(true).build();

        return pipelineDraweeController;

    }

}

你可能感兴趣的:(工具类三、Fresco)