fresco加载图片

frecso基本用法

http://blog.youkuaiyun.com/hss01248/article/details/51757989

fresco加载出现oom

http://blog.youkuaiyun.com/honjane/article/details/65629799

初始化配置fresco

   /**
     * 初始化fresco
     */
    private void initFresco(){
        DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(getApplicationContext())
                .setMaxCacheSize(50 * 1024 * 1024)
                .setBaseDirectoryName("ceshiliugl")
                .setBaseDirectoryPathSupplier(new Supplier<File>() {
                    @Override
                    public File get() {
                        return getApplicationContext().getCacheDir();
                    }
                })
                .build();
        MyImageCacheStatsTracker imageCacheStatsTracker = new MyImageCacheStatsTracker();

        OkHttpClient okHttpClient= getNormalClient();

//        OkHttpClient okHttpClient = CustomerOkHttpClient.getFrescoOkHttpClient();

        ImagePipelineConfig config = OkHttpImagePipelineConfigFactory.newBuilder(getApplicationContext(), okHttpClient)
                //ImagePipelineConfig config = ImagePipelineConfig.newBuilder(context)
                .setMainDiskCacheConfig(diskCacheConfig)
                .setImageCacheStatsTracker(imageCacheStatsTracker)
                .setDownsampleEnabled(true)//Downsampling,它处理图片的速度比常规的裁剪更快,
                // 并且同时支持PNG,JPG以及WEP格式的图片,非常强大,与ResizeOptions配合使用
                .setBitmapsConfig(Bitmap.Config.RGB_565)
                //让fresco即时清理内存:http://blog.youkuaiyun.com/honjane/article/details/65629799
                .setBitmapMemoryCacheParamsSupplier(new MyBitmapMemoryCacheParamsSupplier((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)))
                .build();
        Fresco.initialize(getApplicationContext(), config);
    }


    private static OkHttpClient getNormalClient(){
        OkHttpClient client = new OkHttpClient.Builder()
                //.sslSocketFactory(sslContext.getSocketFactory())
                //.hostnameVerifier(DO_NOT_VERIFY)
                .readTimeout(0, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS).writeTimeout(0, TimeUnit.SECONDS) //设置超时
                .build();
        return client;
    }


package com.team.zhuoke.application;

import com.facebook.imagepipeline.cache.CountingMemoryCache;
import com.facebook.imagepipeline.cache.ImageCacheStatsTracker;

/**
 * Created by Administrator on 2017/3/14 0014.
 */

public class MyImageCacheStatsTracker implements ImageCacheStatsTracker {

    /**
     * Called whenever decoded images are put into the bitmap cache.
     */
    @Override
    public void onBitmapCachePut() {

    }

    /**
     * Called on a bitmap cache hit.
     */
    @Override
    public void onBitmapCacheHit() {

    }

    /**
     * Called on a bitmap cache miss.
     */
    @Override
    public void onBitmapCacheMiss() {

    }

    /**
     * Called whenever encoded images are put into the encoded memory cache.
     */
    @Override
    public void onMemoryCachePut() {

    }

    /**
     * Called on an encoded memory cache hit.
     */
    @Override
    public void onMemoryCacheHit() {

    }

    /**
     * Called on an encoded memory cache hit.
     */
    @Override
    public void onMemoryCacheMiss() {

    }

    /**
     * Called on an staging area hit.
     * <p>
     * <p>The staging area stores encoded images. It gets the images before they are written
     * to disk cache.
     */
    @Override
    public void onStagingAreaHit() {

    }

    /**
     * Called on a staging area miss hit.
     */
    @Override
    public void onStagingAreaMiss() {

    }

    /**
     * Called on a disk cache hit.
     */
    @Override
    public void onDiskCacheHit() {

    }

    /**
     * Called on a disk cache miss.
     */
    @Override
    public void onDiskCacheMiss() {

    }

    /**
     * Called if an exception is thrown on a disk cache read.
     */
    @Override
    public void onDiskCacheGetFail() {

    }

    /**
     * Registers a bitmap cache with this tracker.
     * <p>
     * <p>Use this method if you need access to the cache itself to compile your stats.
     *
     * @param bitmapMemoryCache
     */
    @Override
    public void registerBitmapMemoryCache(CountingMemoryCache<?, ?> bitmapMemoryCache) {

    }

    /**
     * Registers an encoded memory cache with this tracker.
     * <p>
     * <p>Use this method if you need access to the cache itself to compile your stats.
     *
     * @param encodedMemoryCache
     */
    @Override
    public void registerEncodedMemoryCache(CountingMemoryCache<?, ?> encodedMemoryCache) {

    }
}


package com.team.zhuoke.application;

import android.app.ActivityManager;
import android.os.Build;

import com.facebook.common.internal.Supplier;
import com.facebook.common.util.ByteConstants;
import com.facebook.imagepipeline.cache.MemoryCacheParams;

/**
 * Created by Administrator on 2017/3/24 0024.
 */

public class MyBitmapMemoryCacheParamsSupplier implements Supplier<MemoryCacheParams> {
    private static final int MAX_CACHE_ENTRIES = 56;
    private static final int MAX_CACHE_ASHM_ENTRIES = 128;
    private static final int MAX_CACHE_EVICTION_SIZE = 5;
//    private static final int MAX_CACHE_EVICTION_SIZE = 10;
    private static final int MAX_CACHE_EVICTION_ENTRIES = 5;
//    private static final int MAX_CACHE_EVICTION_ENTRIES = 10;
    private final ActivityManager mActivityManager;

    public MyBitmapMemoryCacheParamsSupplier(ActivityManager activityManager) {
        mActivityManager = activityManager;
    }

    @Override
    public MemoryCacheParams get() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new MemoryCacheParams(getMaxCacheSize(), MAX_CACHE_ENTRIES, MAX_CACHE_EVICTION_SIZE, MAX_CACHE_EVICTION_ENTRIES, 1);
        } else {
            return new MemoryCacheParams(
                    getMaxCacheSize(),
                    MAX_CACHE_ASHM_ENTRIES,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE);
        }
    }

    private int getMaxCacheSize() {
        final int maxMemory =
                Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
        if (maxMemory < 32 * ByteConstants.MB) {
            return 4 * ByteConstants.MB;
        } else if (maxMemory < 64 * ByteConstants.MB) {
            return 6 * ByteConstants.MB;
        } else {
            return maxMemory / 5;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值