是的,它是一个非常好的主意,只有在第一次运行时,应用程序会生成从SVG特定设备的屏幕大小的图像,并将其存储在缓存中,并在此后一直使用它们。节省大量的CPU,更快的UI加载。
不过,我建议你保存包含的应用版本名称缓存文件。如果你发布了一些更新(比如版本2)和一些不同的svg图像,那么将使用具有不同名称的新文件而不是旧文件。
其常好于Context.getCacheDir()
使用最高可达10MB,系统将清除低时,在存储该文件夹。
此外,作为一种很好的方法,每次初始化Cache
类时,都可以做一些清理工作,即删除一些旧版本或不需要项目。
这里有一个I类主要是使用只保存,并得到一个序列化对象,从应用程序缓存目录:
public class ObjectCacheFile<T> {
private final File mFile;
public ObjectCacheFile(Context context, String name) {
mFile = new File(context.getCacheDir(), name);
}
public File getFile() {
return mFile;
}
public void put(T o) {
try {
if (!mFile.exists()) {
mFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(mFile);
ObjectOutputStream objOut = new ObjectOutputStream(fos);
try {
objOut.writeObject(o);
} finally {
objOut.close();
}
} catch (IOException e) {
Log.e(App.getLogTag(this), "error saving cache file", e);
}
}
@SuppressWarnings("unchecked")
public T get() {
if (!mFile.exists()) {
return null;
}
try {
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(mFile));
try {
return (T) objIn.readObject();
} finally {
objIn.close();
}
} catch (IOException e) {
Log.e(App.getLogTag(this), "error reading cache file", e);
} catch (ClassNotFoundException e1) {
Log.e(App.getLogTag(this), "cache file corrupted, deleting", e1);
mFile.delete();
}
return null;
}
}
感谢。其实我并没有将缓存存储在文件中。我正在使用RAM!这仍然是一个好主意吗? – 2013-02-25 13:28:37
@Seraphim在RAM中使用缓存?你可以只存储一个变量。将项目存储在磁盘上时,缓存更有用。 – 2013-02-25 13:35:32
是否有在我的应用程序的几个地方重复使用相同的PictureDrawable的一些好处? – 2013-02-25 14:02:13