java类com.squareup.picasso.LruCache的实例源码

PicassoHelper.java 文件源码 项目:rview 阅读 26 收藏 0 点赞 0 评论 0
public static Picasso getDefaultPicassoClient(Context context) {
    if (sDefaultPicasso == null) {
        final File cacheDir = CacheHelper.getImagesCacheDir(context);
        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(chain -> {
                    Response originalResponse = chain.proceed(chain.request());
                    return CacheHelper.addCacheControl(originalResponse.newBuilder()).build();
                })
                .cache(new Cache(cacheDir, CacheHelper.MAX_DISK_CACHE))
                .build();
        OkHttp3Downloader downloader = new OkHttp3Downloader(client);
        sDefaultPicasso = new Picasso.Builder(context.getApplicationContext())
                .defaultBitmapConfig(Bitmap.Config.ARGB_8888)
                .memoryCache(new LruCache(context))
                .downloader(downloader)
                .build();
    }
    return sDefaultPicasso;
}
RadioApplication.java 文件源码 项目:Radio2 阅读 16 收藏 0 点赞 0 评论 0
@Override
public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {

        return;
    }
    LeakCanary.install(this);

    setInstance(this);

    int pool = (Runtime.getRuntime().availableProcessors() * 2) +1 ;

    Log.i("Radio", "Number of core : " + Runtime.getRuntime().availableProcessors() );

    Picasso picasso = new Picasso.Builder(this)
            .memoryCache(new LruCache(16777216))
            .defaultBitmapConfig(Bitmap.Config.RGB_565)
            .executor(new ThreadPoolExecutor(pool, pool, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()))
            .build();

    Picasso.setSingletonInstance(picasso);
}
PicassoModule.java 文件源码 项目:mm.io 阅读 25 收藏 0 点赞 0 评论 0
@Provides
@ApplicationScope
public static Picasso providePicasso(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    boolean largeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
    int memoryClass = am.getMemoryClass();
    if (largeHeap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        memoryClass = am.getLargeMemoryClass();
    }
    int memCacheSize = 1024 * 1024 * memoryClass / 3; // Target ~33% of the available heap.

    Picasso picasso = new Picasso.Builder(context)
            .memoryCache(new LruCache(memCacheSize))
            .build();
    Picasso.setSingletonInstance(picasso);
    return picasso;
}
Utils.java 文件源码 项目:TechDissected 阅读 18 收藏 0 点赞 0 评论 0
public static boolean clearCache(Context context)
{
    boolean result = false;

    LruCache pCache = getPicassoCache(context);
    if(pCache != null) {
        pCache.clear();
        result = true;
    }

    File cacheDir = context.getCacheDir();
    if(cacheDir != null && cacheDir.isDirectory())
        result = deleteDir(cacheDir);

    return result;
}
P1Application.java 文件源码 项目:p1-android 阅读 19 收藏 0 点赞 0 评论 0
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate() {
    super.onCreate();
    me = this;

    // Only for development
    // if (Utils.hasHoneycomb()) {
    // Utils.enableStrictMode();
    // }

    initAuthData();

    picasso = new Picasso.Builder(this)
            .downloader(new OkHttpDownloader(this, 100 * 1024 * 1024))
            .memoryCache(new LruCache(20 * 1024 * 1024)).build();
}
SamplePicassoFactory.java 文件源码 项目:GitHub 阅读 21 收藏 0 点赞 0 评论 0
public static Picasso getPicasso(Context context) {
  if (sPicasso == null) {
      sPicasso = new Picasso.Builder(context)
          .downloader(new OkHttp3Downloader(context, ConfigConstants.MAX_DISK_CACHE_SIZE))
          .memoryCache(new LruCache(ConfigConstants.MAX_MEMORY_CACHE_SIZE))
          .build();
  }
  return sPicasso;
}
PicassoUtils.java 文件源码 项目:AndroidArchitecture 阅读 18 收藏 0 点赞 0 评论 0
public static void setup(@NonNull Context context) {
    Picasso picasso = new Picasso.Builder(context)
            .downloader(new OkHttp3Downloader(context, DISK_CACHE_SIZE))
            .memoryCache(new LruCache(MEMORY_CACHE_SIZE))
            .build();
    Picasso.setSingletonInstance(picasso);
}
PicassoConfigFactory.java 文件源码 项目:ImageLoadPK 阅读 17 收藏 0 点赞 0 评论 0
public static Picasso getPicasso(Context context) {
        if (sPicasso == null) {
            sPicasso = new Picasso.Builder(context)
                    //硬盘缓存池大小
                    .downloader(new OkHttpDownloader(context, ConfigConstants.MAX_CACHE_DISK_SIZE))
                    //内存缓存池大小
                    .memoryCache(new LruCache(ConfigConstants.MAX_CACHE_MEMORY_SIZE))
//                    .defaultBitmapConfig(Bitmap.Config.ARGB_4444)
                    .build();
        }
        return sPicasso;
    }
ImageUtilities.java 文件源码 项目:PGMacTips 阅读 29 收藏 0 点赞 0 评论 0
@Override
protected Void doInBackground(Void... params) {

    Picasso p;

    if (cacheSizeMaxPercent > 0 && cacheSizeMaxPercent < 1) {
        p = new Picasso.Builder(context)
                .memoryCache(new LruCache((int) (
                        cacheSizeMaxPercent * ImageUtilities.getMaxCacheSize())))
                .build();
    } else {
        p = new Picasso.Builder(context)
                .build();
    }

    for (String str : imageURLs) {
        try {
            if (isCancelled()) {
                return null;
            }
            p.with(context).load(str).fetch();
            Thread.sleep(500);
        } catch (OutOfMemoryError e1) {
            //If we run out of memory, make sure to catch it!
            p.with(context).invalidate(str);
            return null;
        } catch (Exception e) {
        }
    }

    return null;
}
App.java 文件源码 项目:greedo-layout-for-android 阅读 23 收藏 0 点赞 0 评论 0
@Override
public void onCreate() {
    super.onCreate();

    Picasso picasso = new Picasso.Builder(this)
            .memoryCache(new LruCache(calculateMemoryCacheSize()))
            .build();
    Picasso.setSingletonInstance(picasso);
}
SinglePicasso.java 文件源码 项目:DiskPicasso 阅读 16 收藏 0 点赞 0 评论 0
/**
 * Initialize picasso.
 *
 * @param context              Android application context preferably.
 * @param memoryCacheSizeBytes Memory cache byte size.
 * @param indicatorEnabled     Enable indicator or not.
 * @param loggingEnabled       Enable logging or not.
 */
public synchronized static void init(Context context, int memoryCacheSizeBytes, boolean indicatorEnabled, boolean loggingEnabled) {
    if (sPicassoInstance == null) {
        Picasso.Builder builder = new Picasso.Builder(context);
        builder.memoryCache(new LruCache(memoryCacheSizeBytes));

        CacheExecutor executor = new CacheExecutor.Builder().setCore(1).setMax(2).build();
        builder.executor(executor);

        sPicassoInstance = builder.build();
        sPicassoInstance.setIndicatorsEnabled(indicatorEnabled);
        sPicassoInstance.setLoggingEnabled(loggingEnabled);
    }
}
ImageLoader.java 文件源码 项目:mc_backup 阅读 26 收藏 0 点赞 0 评论 0
public static synchronized Picasso with(Context context) {
    if (instance == null) {
        lrucache = new LruCache(context);
        Picasso.Builder builder = new Picasso.Builder(context).memoryCache(lrucache);

        final Distribution distribution = Distribution.getInstance(context);
        builder.downloader(new ImageDownloader(context, distribution));
        instance = builder.build();
    }

    return instance;
}
SamplePicassoFactory.java 文件源码 项目:fresco 阅读 54 收藏 0 点赞 0 评论 0
public static Picasso getPicasso(Context context) {
  if (sPicasso == null) {
      sPicasso = new Picasso.Builder(context)
          .downloader(new OkHttp3Downloader(context, ConfigConstants.MAX_DISK_CACHE_SIZE))
          .memoryCache(new LruCache(ConfigConstants.MAX_MEMORY_CACHE_SIZE))
          .build();
  }
  return sPicasso;
}
Utils.java 文件源码 项目:TechDissected 阅读 17 收藏 0 点赞 0 评论 0
/**
 * For some reason, Picasso doesn't allow us to access its
 * cache. I guess we'll just have to force our way in through
 * Java reflection. Returns null if unsuccessful.
 * <br>
 * This may stop working in future versions of the Picasso library.
 *
 * @param context
 * @return
 */
public static LruCache getPicassoCache(final Context context)
{
    try {
        Field cacheField = Picasso.class.getDeclaredField("cache");
        cacheField.setAccessible(true);
        LruCache cache = (LruCache) cacheField.get(Picasso.with(context));
        return cache;
    }
    catch(Exception e) {
        return null;
    }
}
DataModule.java 文件源码 项目:android-couchpotato 阅读 21 收藏 0 点赞 0 评论 0
@Provides @Singleton Picasso providePicasso(Application app, OkHttpClient client) {
  return new Picasso.Builder(app).memoryCache(new LruCache(Utils.calculateMemoryCacheSize(app)))
      .downloader(new OkHttpDownloader(client))
      .listener(new Picasso.Listener() {
        @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception e) {
          Ln.e(e, "Failed to load image: %s", uri);
        }
      })
      .build();
}
AlbumArtLoader.java 文件源码 项目:AndroidMusicBot 阅读 19 收藏 0 点赞 0 评论 0
private AlbumArtLoader() {
  images = new LruCache((int) (Runtime.getRuntime().maxMemory() * (8 / 100f)));
}
Session.java 文件源码 项目:Uoccin 阅读 25 收藏 0 点赞 0 评论 0
private void buildPicasso() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

    File cache = new File(appcx.getCacheDir(), "picasso_cache");
    if (!cache.exists())
        cache.mkdirs();

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .addInterceptor(logging)
            /*.addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    okhttp3.Response originalResponse = chain.proceed(chain.request());*/
                    /*String cacheControl = originalResponse.header("Cache-Control");
                    if (cacheControl == null || cacheControl.contains("no-store") ||
                            cacheControl.contains("no-cache") ||
                            cacheControl.contains("must-revalidate") ||
                            cacheControl.contains("max-age=0")) {
                        return originalResponse.newBuilder()
                                .header("Cache-Control", "public, max-age=" + 5000)
                                .build();
                    } else
                        return originalResponse;*/

                    /*return originalResponse.newBuilder()
                            .header("Cache-Control", "public, max-age=" + 5000)
                            .build();
                }
            })*/
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    return chain.proceed(originalRequest.newBuilder()
                            .header("User-Agent", Commons.USER_AGENT)
                            //.header("Cache-Control", "public, max-age=" + 5000)
                            .build());
                }
            })
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(120, TimeUnit.SECONDS)
            .cache(new Cache(cache, 100000000))
            .build();

    picasso = new Picasso.Builder(appcx)
            .downloader(new OkHttp3Downloader(httpClient))
            .memoryCache(new LruCache(appcx)) // do we need this?
            .build();
    //picasso.setIndicatorsEnabled(true);

    Picasso.setSingletonInstance(picasso);
}
PicassoHandle.java 文件源码 项目:android-sidekick 阅读 20 收藏 0 点赞 0 评论 0
private CacheManager(Context context) {
    this.cache = new LruCache(calculateMemoryCacheSize(context));
}
App.java 文件源码 项目:gdgapp 阅读 30 收藏 0 点赞 0 评论 0
@Override
public void onCreate() {
    super.onCreate();

    if (Const.DEVELOPER_MODE) {
        StrictMode.ThreadPolicy.Builder b = new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
                b.penaltyFlashScreen();
        }
        //StrictMode.setThreadPolicy(b.build());

    }

    if (BuildConfig.DEBUG) {
        Timber.plant(new Timber.DebugTree());
    } else {
        // TODO Crashlytics.start(this);
        // TODO Timber.plant(new CrashlyticsTree());
    }

    // Initialize ACRA Bugreporting (reports get send to GDG[x] Hub)
    ACRA.init(this);

    mInstance = this;

    mPreferences = getSharedPreferences("gdg", MODE_PRIVATE);

    try {
        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

        if(mPreferences.getInt(Const.SETTINGS_VERSION_CODE, 0) < pInfo.versionCode)
            migrate(mPreferences.getInt(Const.SETTINGS_VERSION_CODE, 0), pInfo.versionCode);

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    // Initialize ModelCache and Volley
    getModelCache();
    getBitmapCache();
    GdgVolley.init(this);

    mPreferences.edit().putInt(Const.SETTINGS_APP_STARTS, mPreferences.getInt(Const.SETTINGS_APP_STARTS,0)+1).apply();

    // Initialize Picasso
    mPicasso = new Picasso.Builder(this)
            .downloader(new OkHttpDownloader(this))
            .memoryCache(new LruCache(this))
            .build();
    mPicasso.setDebugging(Const.DEVELOPER_MODE);

    // Initialize GA
    mGaInstance = GoogleAnalytics.getInstance(getApplicationContext());
    mTracker = mGaInstance.getTracker(getString(R.string.ga_trackingId));
    GAServiceManager.getInstance().setDispatchPeriod(0);
    mTracker.setAppName(getString(R.string.app_name));
    mTracker.setAnonymizeIp(true);
    mGaInstance.setDefaultTracker(mTracker);

    mOrganizerChecker = new OrganizerChecker(this, mPreferences);
    mOrganizerChecker.setLastOrganizerCheckTime(mPreferences.getLong("organizer_check_time", 0));
    mOrganizerChecker.setLastOrganizerCheckId(mPreferences.getString("organizer_check_id", null));

    GoogleAnalytics.getInstance(this).setAppOptOut(mPreferences.getBoolean("analytics",false));

    // Init LastLocationFinder
    mLocationFinder = new GingerbreadLastLocationFinder(this);
    mLocationFinder.setChangedLocationListener(this);
    updateLastLocation();
}


问题


面经


文章

微信
公众号

扫码关注公众号