ANDROID: How To Calculate Frames Per Second (FPS) in SurfaceView Class?
if someone could just give me a small code of how to calculate the frames
per second of my screen that would be just great.
This is my basic code, though you might not need this.
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);
}
}
PLEASE GIVE ME A SIMPLE WORKING CODE Thanks
No comments:
Post a Comment