是的,它是一個非常好的主意,只有在第一次運行時,應用程序會生成從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