低FPS与Android SurfaceView [英] Low FPS with android SurfaceView

查看:629
本文介绍了低FPS与Android SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些trubles用一个SurfaceView我的帧率。林做tipical的东西,我在一些教程中(所有这些所说的一样),但我不能在我的三星Galaxy S(旧人,I9000)达到一个像样的帧率。

I have some trubles with my framerate using a SurfaceView. Im doing the tipical stuff i found in some tutorials (all of them said the same), but i cant reach a decent framerate on my samsung galaxy S (the old one, i9000).

这里的code我对循环线程。 FPS在30初始化。

Here's the code i have for the loop thread. FPS is initialized at 30.

@Override
public void run() {
    long ticksPS = 1000/FPS;
    long startTime;
    long sleepTime;
    //fps checker
    long contms=0;
    long lasttimecheck = System.currentTimeMillis();
    int fps=0;
    while (running) {
        long time =  System.currentTimeMillis();
        if(contms>1000) {
            Log.v("FPS",String.valueOf(fps));
            contms=time-lasttimecheck;
            fps=1;
        }
        else {
            fps++;
            contms+=time-lasttimecheck;
        }
        lasttimecheck = time;

        Canvas c = null;
        startTime =time;
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 10)
                   sleep(sleepTime);
            else {
                Log.w("LOWFPS",String.valueOf(contms));
                sleep(10);
            }
     } catch (Exception e) {}
    }
}

在surfaceView,我初始化holder.setFormat(PixelFormat.RGBA_8888)的持有人;但我不知道如果我有做的位图的东西,以避免无用的CPU工作(我保存的局部变量的位图,然后我画他们)。游戏很简单,它应该运行得更快。

In the surfaceView, I initialize the holder with holder.setFormat(PixelFormat.RGBA_8888); but i dont know if i have to do something with the bitmaps to avoid useless CPU work (I save the bitmaps in local variables, then I draw them). The game is simple, it should run much faster.

帧率是相当随机的,有时它的工作原理冰沙,有时它does not,但总是在30FPS下。

The framerate is quite random, sometimes it works smoothie, sometimes it doesnt, but always under the 30FPS.

任何想法???

使用的OnDraw说明修改

EDIT WITH ONDRAW EXPLANATION

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bg, 0, 0, null); //1
    stars.draw(canvas,dx,dy);      //2
    if(playing.on()) reactors.draw_reaccio(canvas,dx,dy); //3
    gotes.draw(canvas,dx,dy);     //4
    reactors.draw(canvas,dx,dy);  //5
    sg.draw(canvas);  //6         
    sr.draw(canvas);  //7
    eraser.draw(canvas);  //8
    playing.draw(canvas); //9
    opcions.draw(canvas); //10
}

1)绘制背景(480×800)
2),这是一个包含的getHeight()基本对象(分与其coordenates(X,Y),并与相关联的图像的ID的列表)的类(约9个不同的分图像)
3)它吸引N * 2个圈,一个有填充和其他每个相关对象行程(约20左右)
4)绘制的比赛中,点点滴滴与动画的主要对象。有9个不同类型滴,和他们每个人都有动画的5个相关图像(我应该把5图像1也许?)
5)相同,滴剂,但没有动画
6〜10)无关,它只是绘制图像

1) Drawing the background (480x800) 2) this is a class that contains a list of "getHeight()" basic objects (stars) with its coordenates (x,y) and the ID with the associated image (about 9 different stars images) 3) it draws n*2 circles, one with fill and another with stroke per related object (about 20 or so) 4) It draws the main object of the game, little drops with an animation. There are 9 different kind of drops, and each of them have 5 related images of the animation (should i put the 5 images in 1 maybe?) 5) same as drops but without animation 6 to 10) irrelevant, it just draw an image

我猜的缓慢是由于:(1)由于数星星(4)becouse的动画,巫婆每隔2-3帧更改为不同的形象,也许是太多的记忆,我猜我应该合并所有的图像,在短短1。

I guess the slowness is due to: (2) because of the number of stars (4) becouse of the animation, witch change every 2-3 frames to a different image and maybe it is too much for memory, i guess i should merge all the images in just 1.

帧率为20-22 FPS与S的Galaxy S I9000

The framerate is about 20-22 FPS with S. Galaxy S i9000

推荐答案

如果您禁用绘图,多少FPS你实现?
只是为了检查显示管线多少占用。

If you disable the drawing, how much fps do you achieve ? Just to check how much the display pipeline is hogging.

我试过以下
创建位图5尺寸720×480的:PIX RGB565格式
从类似你们一个循环SurfaceView显示它们。
唯一的区别是,我有prepared位图,并在循环中没有画他们。
这是什么一个的Nexus-S手机上实现

I had tried following create 5 bitmaps of size 720 x 480 : pix format RGB565 display them on a SurfaceView from a loop similar to yours. The only difference is that I had "prepared bitmaps" and wasn't drawing them in a loop. This is what was achieved on a Nexus-S phone

FPS:55(我试图运行循环快,无需调节)

CPU负载:85%

这是当我决定从渲染JNI :)!我SurfaceView

This is when I decided to render my SurfaceView from JNI :) !!

做了类似的实验,看看你的设备是多少能够节流没有绘制操作。如果它看起来不错,那么你可以分析是否可以在预算之内装修在你的绘图操作。

Do a similar experiment and see how much your device is able to throttle "without" drawing operations. If it looks good, then you can profile whether you can fit-in your your drawing operations within the budget.

这篇关于低FPS与Android SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆