ImageLoader相关:
Imageloader加载图片初始化操作一般会在Application中执行如下方法:
public static void initImageLoader(Context context) {
// 缓存文件的目录
screenWidth = Utils.getScreenWidth(context);
screenHeight = Utils.getScreenHeight(context);
Log.e(TAG, "screenWidth=================" + screenWidth);
Log.e(TAG, "screenHeight=================" + screenHeight);
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800)// 即保存的每个缓存文件的最大长宽
.threadPoolSize(3) // 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()) // 将保存的时候的URI名称用MD5加密
.memoryCache(new FIFOLimitedMemoryCache(2 * 8 * screenWidth * screenHeight)) //内存缓存策略
.memoryCacheSize(2 * 8 * screenWidth * screenHeight) // 内存缓存的最大值
.diskCacheSize(50 * 1024 * 1024) // 50 M SD卡(本地)缓存的最大值
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCache(new UnlimitedDiskCache(cacheDir))//SD上的缓存策略
.imageDownloader(new BaseImageDownloader(context, 8 * 1000, 40 * 1000)) // 连接时间为8秒,读数据超时时间为40秒
.writeDebugLogs() // Remove for release app
.build();
// 全局初始化此配置
ImageLoader.getInstance().init(config);
}
缓存策略有如下几种:
UsingFreqLimitedMemoryCache (缓存大小超过指定值时,删除最少使的bitmap)
LRULimitedMemoryCache (缓存大小超过指定值时,删除最近最少使用的bitmap--默认值)
FIFOLimitedMemoryCache (缓存大小超过指定值时,按先进先出规则删除的bitmap)
LargestLimitedMemoryCache (缓存大小超过指定值时,删除最大的bitmap)
LimitedAgeMemoryCache (缓存对象超过定义的时间后删除)
使用的时候:ImageLoader.getInstance().displayImage(......);
Glide相关:
统一设置缓存目录及缓存策略
Manifest.xml中声明
<meta-data
android:name="com.tvos.tvguophoneapp.glide.GlideConfiguration"
android:value="GlideModule" />
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 自定义缓存目录
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, GlideCatchConfig.GLIDE_CARCH_DIR, GlideCatchConfig.GLIDE_CATCH_SIZE));
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}
public class GlideCatchConfig {
// 图片缓存最大容量,50M,根据自己的需求进行修改
public static final int GLIDE_CATCH_SIZE = 50 * 1024 * 1024;
// 图片缓存子目录
public static final String GLIDE_CARCH_DIR = "image_catch";
}
Glide相关的缓存策略如下:
1、设置内存缓存
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, yourSizeInBytes));
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheDirectoryName, yourSizeInBytes));
2、设置在SD卡上的缓存
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, cacheDirectoryName, yourSizeInBytes));
同时设置Pro-guard.cfg:
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
WebView相关设置如下:
// 对WebView进行设置
@SuppressWarnings("deprecation")
public static void initWebView(WebView webView) {
String cachePath = ApplicationWrapper.getInstance().getCacheDir() + "/org.chromium.android_webview";
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
webView.getSettings().setSupportZoom(true);//支持缩放
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webview.setInitialScale(100);//设置缩放比例
webSettings.setJavaScriptEnabled(true); // 允许加载JS
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// 默认的缓存方式
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheMaxSize(100 * 1024 * 1024);// 上限设置为100M
webSettings.setAppCachePath(cachePath);
webSettings.setDatabasePath(cachePath);
webSettings.setAppCacheEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
// 启动时如果超过100M则删除WebView的缓存
private void clearWebViewCache() {
try {
String webCachePath = ApplicationWrapper.getInstance().getCacheDir() + "/org.chromium.android_webview";
File webCacheFile = new File(webCachePath);
LogUtil.e(TAG, "webview缓存路径==" + webCacheFile.getAbsolutePath());
double fileSize = Utils.getFolderSize(webCacheFile) / 1024;
long MAX_VALUE = 100 * 1024 * 1024;
LogUtil.e(TAG, "webView缓存大小==" + fileSize + "KB" + "参照值:" + MAX_VALUE + "KB");
if (fileSize >= MAX_VALUE) {
ApplicationWrapper.getInstance().deleteDatabase("webview.db");
ApplicationWrapper.getInstance().deleteDatabase("webviewCache.db");
Utils.deleteFile(webCacheFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Imageloader加载图片初始化操作一般会在Application中执行如下方法:
public static void initImageLoader(Context context) {
// 缓存文件的目录
screenWidth = Utils.getScreenWidth(context);
screenHeight = Utils.getScreenHeight(context);
Log.e(TAG, "screenWidth=================" + screenWidth);
Log.e(TAG, "screenHeight=================" + screenHeight);
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800)// 即保存的每个缓存文件的最大长宽
.threadPoolSize(3) // 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()) // 将保存的时候的URI名称用MD5加密
.memoryCache(new FIFOLimitedMemoryCache(2 * 8 * screenWidth * screenHeight)) //内存缓存策略
.memoryCacheSize(2 * 8 * screenWidth * screenHeight) // 内存缓存的最大值
.diskCacheSize(50 * 1024 * 1024) // 50 M SD卡(本地)缓存的最大值
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCache(new UnlimitedDiskCache(cacheDir))//SD上的缓存策略
.imageDownloader(new BaseImageDownloader(context, 8 * 1000, 40 * 1000)) // 连接时间为8秒,读数据超时时间为40秒
.writeDebugLogs() // Remove for release app
.build();
// 全局初始化此配置
ImageLoader.getInstance().init(config);
}
缓存策略有如下几种:
UsingFreqLimitedMemoryCache (缓存大小超过指定值时,删除最少使的bitmap)
LRULimitedMemoryCache (缓存大小超过指定值时,删除最近最少使用的bitmap--默认值)
FIFOLimitedMemoryCache (缓存大小超过指定值时,按先进先出规则删除的bitmap)
LargestLimitedMemoryCache (缓存大小超过指定值时,删除最大的bitmap)
LimitedAgeMemoryCache (缓存对象超过定义的时间后删除)
使用的时候:ImageLoader.getInstance().displayImage(......);
Glide相关:
统一设置缓存目录及缓存策略
Manifest.xml中声明
<meta-data
android:name="com.tvos.tvguophoneapp.glide.GlideConfiguration"
android:value="GlideModule" />
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 自定义缓存目录
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, GlideCatchConfig.GLIDE_CARCH_DIR, GlideCatchConfig.GLIDE_CATCH_SIZE));
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}
public class GlideCatchConfig {
// 图片缓存最大容量,50M,根据自己的需求进行修改
public static final int GLIDE_CATCH_SIZE = 50 * 1024 * 1024;
// 图片缓存子目录
public static final String GLIDE_CARCH_DIR = "image_catch";
}
Glide相关的缓存策略如下:
1、设置内存缓存
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, yourSizeInBytes));
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheDirectoryName, yourSizeInBytes));
2、设置在SD卡上的缓存
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, cacheDirectoryName, yourSizeInBytes));
同时设置Pro-guard.cfg:
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
WebView相关设置如下:
// 对WebView进行设置
@SuppressWarnings("deprecation")
public static void initWebView(WebView webView) {
String cachePath = ApplicationWrapper.getInstance().getCacheDir() + "/org.chromium.android_webview";
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
webView.getSettings().setSupportZoom(true);//支持缩放
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webview.setInitialScale(100);//设置缩放比例
webSettings.setJavaScriptEnabled(true); // 允许加载JS
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// 默认的缓存方式
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheMaxSize(100 * 1024 * 1024);// 上限设置为100M
webSettings.setAppCachePath(cachePath);
webSettings.setDatabasePath(cachePath);
webSettings.setAppCacheEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
// 启动时如果超过100M则删除WebView的缓存
private void clearWebViewCache() {
try {
String webCachePath = ApplicationWrapper.getInstance().getCacheDir() + "/org.chromium.android_webview";
File webCacheFile = new File(webCachePath);
LogUtil.e(TAG, "webview缓存路径==" + webCacheFile.getAbsolutePath());
double fileSize = Utils.getFolderSize(webCacheFile) / 1024;
long MAX_VALUE = 100 * 1024 * 1024;
LogUtil.e(TAG, "webView缓存大小==" + fileSize + "KB" + "参照值:" + MAX_VALUE + "KB");
if (fileSize >= MAX_VALUE) {
ApplicationWrapper.getInstance().deleteDatabase("webview.db");
ApplicationWrapper.getInstance().deleteDatabase("webviewCache.db");
Utils.deleteFile(webCacheFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}