0

如果有人能给我一个关于如何计算屏幕每秒帧数的小代码,那就太好了。

这是我的基本代码,尽管您可能不需要它。

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);
            }



}

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

4

1 回答 1

0

Make a thread that does this 30 times a second:

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

In fact if your thread just sits there doing that without trying to pause, it will be throttled to the frame rate of the screen.

You need to make sure that this thread is correctly synchronized with management of the surface view -- for example if the surface is changing or being destroyed, your code there should synchronize with the thread to make sure the thread stops running while this happens.

Of course this does mean that you are doing software rendering into the canvas. Depending on what you are doing, this may be fine to give you 30fps animation. If not, you'll need to use GLSurfaceView. You may want to consider using that anyway, just because it takes care of the threading part for you.

于 2013-08-23T06:18:42.127 回答