Lud*_*dde 4 android 2d surfaceview ondraw android-canvas
我花了一些时间学习如何在 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
{
private final float mDisplayDensity;
private long mFrameTime = System.nanoTime();
private final Drawable mBackground;
private final Drawable mMonkey;
public MiniGameView(Context context)
{
this(context, null);
}
public MiniGameView(Context context, AttributeSet attrs)
{
super(context, attrs);
mDisplayDensity = getResources().getDisplayMetrics().density;
// Load graphics
mBackground = ResourcesCompat.getDrawable(getResources(), R.drawable.background, null);
mMonkey = ResourcesCompat.getDrawable(getResources(), R.drawable.monkey, null);
Choreographer.getInstance().postFrameCallback(this);
}
// Receive time in nano seconds at last VSYNC. Use this frameTime for smooth animations!
@Override
public void doFrame(long frameTimeNanos)
{
mFrameTime = frameTimeNanos;
Choreographer.getInstance().postFrameCallback(this);
invalidate();
}
// Draw game here
@Override
protected void onDraw(Canvas canvas)
{
drawBackground(canvas);
drawSprites(canvas);
}
private void drawBackground(Canvas canvas)
{
mBackground.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
mBackground.draw(canvas);
}
private void drawSprites(Canvas canvas)
{
double t = mFrameTime * 0.00000001;
int width = canvas.getWidth();
int height = canvas.getHeight();
for(int i=0;i<8;i++)
{
double x = width * (1 + Math.sin(-0.181 * t)) * 0.5;
double y = height * (1 - Math.cos(0.153 * t)) * 0.5;
int size = (int)Math.round((80 + 40 * Math.cos(0.2 * t)) * mDisplayDensity);
drawSprite(canvas, mMonkey, (int) x, (int) y, size, size);
t += 0.8;
}
}
private void drawSprite(final Canvas canvas, final Drawable sprite, int x, int y, int w2, int h2)
{
sprite.setBounds(x - w2, y - h2, x + w2, y + h2);
sprite.draw(canvas);
}
}
Run Code Online (Sandbox Code Playgroud)
我还创建了一个 systrace文件。
关于 SurfaceView 真的没有“神话”。它是高分辨率快速动画的最佳选择……但您必须使用 OpenGL ES。表面上的画布渲染——SurfaceView、TextureView 等等——不是硬件加速的,并且随着像素数量的增加变得越来越昂贵。
SurfaceView 的一个有用功能是您可以将 Surface 设置为固定大小,并让显示硬件将其放大。对于某些类型的游戏,这可以产生足够的性能。的比例看起来像什么的一个例子是在这里; 注意渲染使用 GLES。
可以在本附录中找到有关游戏循环的一般建议。你似乎在做正确的事情。您可能需要考虑添加一个丢帧计数器来查看您的动画故障是否与丢帧相关。(如果 Choreographer 报告的连续时间从 16.7 毫秒跳到 33 毫秒,您就知道您已经减少了一次。)
跟踪动画故障的最佳方法是使用systrace。跟踪可以很容易地准确查看所有线程正在做什么,并确定暂停的因果关系。
| 归档时间: |
|
| 查看次数: |
3421 次 |
| 最近记录: |