根项目build.gradel中的配置
<pre>
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
</pre>
Gradle支持从maven中央仓库和JCenter上获取构件,那这两者有什么区别呢?
maven中央仓库(http://repo1.maven.org/maven2/)是由Sonatype公司提供的服务,它是Apache Maven、SBT和其他构建系统的默认仓库,并能很容易被Apache Ant/Ivy、Gradle和其他工具所使用。开源组织例如Apache软件基金会、Eclipse基金会、JBoss和很多个人开源项目都将构件发布到中央仓库。 maven中央仓库已经将内容浏览功能禁掉了,可在http://search.maven.org/查询构件。
https://jcenter.bintray.com )是由JFrog公司提供的Bintray中的Java仓库。它是当前世界上最大的Java和Android开源软件构件仓库。 所有内容都通过内容分发网络(CDN)使用加密https连接获取。JCenter是Goovy Grape内的默认仓库,Gradle内建支持(jcenter()仓库),非常易于在(可能除了Maven之外的)其他构建工具内进行配置。
JCenter相比mavenCenter构件更多,性能也更好。但还是有些构件仅存在mavenCenter中。
<pre>
project.ext {
buildToolsVersion = "${BUILD_TOOLS_VERSION}"
compileSdkVersion = COMPILE_SDK_VERSION.toInteger()
minSdkVersion = MIN_SDK_VERSION;
targetSdkVersion = TARGET_SDK_VERSION;
preDexLibs = !project.hasProperty('disablePreDex');
}
</pre>
"${BUILD_TOOLS_VERSION}"引入gradle.properties中的字符属性,MIN_SDK_VERSION引入int属性,gradle.properties把整个源码中用到的常量都给整合了,方便以后维护。
<pre>
android {
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.facebook.samples.demo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "${VERSION_NAME}"
testApplicationId "com.facebook.samples.demo.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
}
</pre>
以buildToolsVersion rootProject.ext.buildToolsVersion为例buildToolsVersion引用了以上在根项目中的配置值,其他类似。
Presco的配置项ImagePipelineConfig采用是建造者模式,以下是ImagePipelineConfig的构造函数
<pre>
private ImagePipelineConfig(Builder builder) {
mAnimatedImageFactory = builder.mAnimatedImageFactory;
mBitmapMemoryCacheParamsSupplier =
builder.mBitmapMemoryCacheParamsSupplier == null ?
new DefaultBitmapMemoryCacheParamsSupplier(
(ActivityManager) builder.mContext.getSystemService(Context.ACTIVITY_SERVICE)) :
builder.mBitmapMemoryCacheParamsSupplier;
mBitmapConfig =
builder.mBitmapConfig == null ?
Bitmap.Config.ARGB_8888 :
builder.mBitmapConfig;
mCacheKeyFactory =
builder.mCacheKeyFactory == null ?
DefaultCacheKeyFactory.getInstance() :
builder.mCacheKeyFactory;
mContext = Preconditions.checkNotNull(builder.mContext);
mDecodeMemoryFileEnabled = builder.mDecodeMemoryFileEnabled;
mFileCacheFactory = builder.mFileCacheFactory == null ?
new DiskStorageCacheFactory(new DynamicDefaultDiskStorageFactory()) :
builder.mFileCacheFactory;
mDownsampleEnabled = builder.mDownsampleEnabled;
mEncodedMemoryCacheParamsSupplier =
builder.mEncodedMemoryCacheParamsSupplier == null ?
new DefaultEncodedMemoryCacheParamsSupplier() :
builder.mEncodedMemoryCacheParamsSupplier;
mImageCacheStatsTracker =
builder.mImageCacheStatsTracker == null ?
NoOpImageCacheStatsTracker.getInstance() :
builder.mImageCacheStatsTracker;
mImageDecoder = builder.mImageDecoder;
mIsPrefetchEnabledSupplier =
builder.mIsPrefetchEnabledSupplier == null ?
new Supplier<Boolean>() {
@Override
public Boolean get() {
return true;
}
} :
builder.mIsPrefetchEnabledSupplier;
mMainDiskCacheConfig =
builder.mMainDiskCacheConfig == null ?
getDefaultMainDiskCacheConfig(builder.mContext) :
builder.mMainDiskCacheConfig;
mMemoryTrimmableRegistry =
builder.mMemoryTrimmableRegistry == null ?
NoOpMemoryTrimmableRegistry.getInstance() :
builder.mMemoryTrimmableRegistry;
mNetworkFetcher =
builder.mNetworkFetcher == null ?
new HttpUrlConnectionNetworkFetcher() :
builder.mNetworkFetcher;
mPlatformBitmapFactory = builder.mPlatformBitmapFactory;
mPoolFactory =
builder.mPoolFactory == null ?
new PoolFactory(PoolConfig.newBuilder().build()) :
builder.mPoolFactory;
mProgressiveJpegConfig =
builder.mProgressiveJpegConfig == null ?
new SimpleProgressiveJpegConfig() :
builder.mProgressiveJpegConfig;
mRequestListeners =
builder.mRequestListeners == null ?
new HashSet<RequestListener>() :
builder.mRequestListeners;
mResizeAndRotateEnabledForNetwork = builder.mResizeAndRotateEnabledForNetwork;
mSmallImageDiskCacheConfig =
builder.mSmallImageDiskCacheConfig == null ?
mMainDiskCacheConfig :
builder.mSmallImageDiskCacheConfig;
// Below this comment can't be built in alphabetical order, because of dependencies
int numCpuBoundThreads = mPoolFactory.getFlexByteArrayPoolMaxNumThreads();
mExecutorSupplier =
builder.mExecutorSupplier == null ?
new DefaultExecutorSupplier(numCpuBoundThreads) : builder.mExecutorSupplier;
mImagePipelineExperiments = builder.mExperimentsBuilder.build();
}
</pre>
上述源码主要是设置了一些默认的配置对象,相关知识点
1、ActivityManager框架解析--ImagePipelineConfig
2、Executors.newFixedThreadPool实例-HttpUrlConnectionNetworkFetcher
3、空集合声明小技巧List<Integer> list = Collections.EMPTY_LIST
4、Java ThreadFactory接口用法--DefaultExecutorSupplier
<pre>
/** Initializes Fresco with the default config. */
public static void initialize(Context context) {
ImagePipelineFactory.initialize(context);
initializeDrawee(context);
}
/** Initializes Fresco with the specified config. */
public static void initialize(Context context, ImagePipelineConfig imagePipelineConfig) {
ImagePipelineFactory.initialize(imagePipelineConfig);
initializeDrawee(context);
}
</pre>
以上代码是Presco初始化代码,一个是默认配置的,一个是传递自定义配置的,以下是初始化过程中的知识点。
1、@NotThreadSafe 表示这个类不是线程安全的。如果是线程安全的非要打上这个注解,那也不会报错。(注释详解)--ImagePipelineFactory
2、Java.util.ArrayDeque类--ThreadHandoffProducerQueue
3、Predicate<K> predicate后期补充--MemoryCache/AndroidPredicates
4、AnimatedFactoryProvider获取动画工厂实体的时候用到了反射
5、Java WeakReference的理解与使用--ActivityListenerManager
6、Executor实现AbstractExecutorService实现分析--CallerThreadExecutor
7、AtomicInteger的并发处理--StatefulRunnable
8、MediaUtils包含对mime类型的一些处理
9、IdentityHashMap 使用总结-SharedReference
10、使用isInEditMode解决可视化编辑器无法识别自定义控件的问题