在Android的Photo Collage应用程序中,我使用Volley来加载图像.我正在使用具有50 mb存储空间的DiskBasedCache(包含在volley中),以防止多次重新下载相同的图像.
上次我检查DiskBasedCache包含大约1000个缓存条目.
当我的应用程序启动时,Volley调用mCache.initialize(),它将在我的Galaxy S4上花费大约10秒钟(!)来执行以下操作:
我发现在启动时读取1000多个文件并不是加载缓存索引的有效方法!:-)
从volley/toolbox/DiskBasedCache.java:
@Override
public synchronized void initialize() {
if (!mRootDirectory.exists()) {
if (!mRootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
}
return;
}
File[] files = mRootDirectory.listFiles();
if (files == null) {
return;
}
for (File file : files) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
CacheHeader entry = CacheHeader.readHeader(fis);
entry.size = file.length();
putEntry(entry.key, entry);
} catch (IOException e) {
if (file != null) { …Run Code Online (Sandbox Code Playgroud) 我花了一些时间学习如何在 Android anno 2016 上创建 2D 渲染游戏循环。
我想实现以下目标:
关于 SurfaceView 的神话:
首先,有几篇文章推荐SurfaceView。乍一看,这似乎是个好主意,因为它使用单独的渲染线程,但事实证明,从 SurfaceHolder 返回的 Canvas无法进行硬件加速!在具有 QuadHD (2560x1440) 分辨率的设备上使用带有软件渲染的 SurfaceView 是非常低效的。
因此我的选择是扩展一个基本的 View 并覆盖 onDraw()。为每次更新调用 invalidate()。
流畅的动画:
我的下一个挑战是流畅的动画。事实证明,在 onDraw() 中读取 System.nanoTime() 是一个坏主意,因为它不会以精确的 1/60 秒间隔被调用,从而在我的精灵上产生生涩的动作。因此,我使用Choreographer为我提供每个 VSYNC 的帧时间。这很好用。
现在的进展:
我觉得我已经非常接近了,但我仍然偶尔会在旧设备上遇到一些滞后。内存使用率非常低,所以我不认为 GC 是背后的原因......似乎我的回调偶尔会错过/跳转 aoad 帧。
我会发布我的代码,我期待着阅读您的评论和建议。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.Choreographer;
import android.view.View;
public class MiniGameView extends View implements Choreographer.FrameCallback …Run Code Online (Sandbox Code Playgroud)