Android 毕加索单例用法
我正在我的应用程序中使用毕加索 首先,我只使用以下格式:Android 毕加索单例用法,android,singleton,picasso,Android,Singleton,Picasso,我正在我的应用程序中使用毕加索 首先,我只使用以下格式: Picasso.with(context)....into(imgView); 这样的话,我假设我把毕加索当作一个单身汉。是吗 其次,我想使用setindicatorEnabled。但是,无法将其添加到上述格式,因为它不是静态方法。是否有任何方法可以按上述格式使用此函数 第三,如果我需要使用Picasso.Builder(…).build()创建一个自定义实例,以便使用SetIndicatorEnabled,那么在应用程序的各个活动中实
Picasso.with(context)....into(imgView);
这样的话,我假设我把毕加索当作一个单身汉。是吗
其次,我想使用
setindicatorEnabled
。但是,无法将其添加到上述格式,因为它不是静态方法。是否有任何方法可以按上述格式使用此函数
第三,如果我需要使用
Picasso.Builder(…).build()
创建一个自定义实例,以便使用SetIndicatorEnabled
,那么在应用程序的各个活动中实现单例使用的最佳方法是什么?是的,当您使用Picasso.with(context)
要使用已启用的设置指示器
Picasso mPicasso = Picasso.with(context);
mPicasso.setIndicatorsEnabled(true);
mPicasso....load().into(imageView);
如果你使用构建器,你应该创建你自己的singleton来保存你的毕加索实例,并在创建完成后将其清理干净。不要每次使用毕加索时都使用builder,因为它会创建一个新实例。我相信Picasso.with(context)只需要获取您的上下文并调用getApplicationContext,然后使用应用程序上下文存储Picasso的单例实例。下面是实现单例Picasso类的一个好方法
public class ImageHandler {
private static Picasso instance;
public static Picasso getSharedInstance(Context context)
{
if(instance == null)
{
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
}
return instance;
}
}
然后它在代码中的实现如下所示:
ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().resize(width, height).into(image, new Callback() {
@Override
public void onSuccess() {
layout.setVisibility(View.VISIBLE);
}
@Override
public void onError() {
}
});
请注意,如果没有必要,您不必实现回调当前的方法似乎是使用
setSingletonInstance
在您创建的应用程序中运行此命令:
Picasso.setSingletonInstance(Picasso.Builder(context).build()
Do I?
:请参阅(yes)setindicatorEnabled
:with
返回一个实例。在该实例上调用setIndicatorEnabled
毕加索的API方法已更改,请使用最新的。如果将该返回语句放在If-else子句之外并删除重复项,则代码将更干净