ANDROID:如何计算SurfaceView类中的每秒帧数(FPS)?

时间:2013-08-23 06:15:08

标签: android surfaceview lag frame-rate

如果有人能给我一个小代码,告诉我如何计算我的屏幕每秒的帧数,这将是非常好的。

这是我的基本代码,但您可能不需要这个。

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class MainGame extends Activity implements OnTouchListener
{
MyBringBackSurface ourSurfaceView;
inr xpos = 0; 

@ Override
    protected void onCreate (Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate (savedInstanceState);
    ourSurfaceView = new MyBringBackSurface (this);
    ourSurfaceView.setOnTouchListener (this);
    x [0] = 0;
    y [0] = 0;
    x [1] = 0;
    y [1] = 0;

    requestWindowFeature (Window.FEATURE_NO_TITLE);
    getWindow ().addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); /////////////////////check this NEWLY ADDED
    getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); /////////////////////////////////////////////////CHECK THIS MIGHT NOT WORK
    setContentView (ourSurfaceView); //ourSurfaceView
}


@ Override
    protected void onPause ()
{
    // TODO Auto-generated method stub
    super.onPause ();
    ourSurfaceView.pause ();
}


@ Override
    protected void onResume ()
{
    // TODO Auto-generated method stub
    super.onResume ();
    ourSurfaceView.resume ();
}






public class MyBringBackSurface extends SurfaceView implements Runnable
{
    //vertical
    SurfaceHolder ourHolder;
    Canvas canvas = (Canvas) ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;


    public MyBringBackSurface (Context context)
    {
        // TODO Auto-generated constructor stub
        super (context);
        ourHolder = getHolder ();
    }

    public void pause ()
    {
        isRunning = false;
        while (true)
        {
            try
            {
                ourThread.join ();
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace ();
            }
            break;
        }
        ourThread = null;
    }
    public void resume ()
    {
        isRunning = true;
        ourThread = new Thread (this);
        ourThread.start ();
    }

    public void run ()
    {
        // TODO Auto-generated method stub


        while (isRunning)
        {

            if (!ourHolder.getSurface ().isValid ())
                continue;

            canvas = ourHolder.lockCanvas ();

           Paint paint = new Paint();
           paint.setColor(Color.BLUE);
           canvas.drawCircle(xpos,33,23,paint);
           if(x<canvas.getWidth()){
            xpos++;
            }


                    ourHolder.unlockCanvasAndPost(canvas);
            }



}

请给我一个简单的工作代码 感谢

1 个答案:

答案 0 :(得分:0)

创建一个每秒执行30次的线程:

Call SurfaceHolder.lockCanvas().
Draws into the Canvas (must completely redraw all pixels).
Call SurfaceHolder.unlockCanvasAndPost().

事实上,如果您的线程只是坐在那里而不试图暂停,它将被限制为屏幕的帧速率。

您需要确保此线程与表面视图的管理正确同步 - 例如,如果曲面正在更改或被销毁,那么您的代码应该与线程同步以确保线程在此时停止运行发生的情况。

当然,这确实意味着您正在将软件渲染到画布中。根据你正在做的事情,这可能很适合给你30fps的动画。如果没有,您将需要使用GLSurfaceView。你可能还是想考虑使用它,因为它会为你处理线程部分。