現在アンドロイドアプリで円が放射運動をするSurfaceViewの開発をしたのですが、FPSが低すぎて、ボールが滑らかに運動しているように見えなくて困っています。
あまりAndroidについて知らないので、基本的なところの理解不足が原因かもしれませんがなにか改善方法があればご教示ください。
よろしくお願いいたします。
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable { private Paint mPaint = null; private Canvas mCanvas = null; static final long FPS = 20; static final long FRAME_TIME = 1000 / FPS; static final int BALL_R = 10; SurfaceHolder surfaceHolder; Thread thread; DisplayMetrics dm = Resources.getSystem().getDisplayMetrics(); int width = dm.widthPixels; int height = dm.heightPixels; float cx = 0, cy = 0; int screen_width, screen_height; float g = (float) 9.8; int temp1, temp2; double velocity = (width-60)/(35*Math.cos(Math.PI*4/9)); float vcos = (float) (velocity*Math.cos(Math.PI*4/9)); float vsin = (float) (velocity*Math.sin(Math.PI*4/9)); public MySurfaceView(Context context){ super(context); surfaceHolder = getHolder(); surfaceHolder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder holder) { thread = new Thread(this); thread.start(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { thread = null; } @Override public void run() { Canvas canvas = null; Paint paint = new Paint(); Paint bgPaint = new Paint(); // Background bgPaint.setStyle(Paint.Style.FILL); bgPaint.setColor(Color.WHITE); // Ball paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLUE); long startTime = System.currentTimeMillis(); float time = 0; float cvy; int temp1, temp2; temp1 = (int) (velocity * 10000); temp2 = temp1 % 10; temp1 -= temp2; velocity = ((float)temp1)/10000; temp1 = (int) (vcos * 10000); temp2 = temp1 % 10; temp1 -= temp2; vcos = ((float)temp1)/10000; temp1 = (int) (vsin * 10000); temp2 = temp1 % 10; temp1 -= temp2; vsin = ((float)temp1)/10000; while(thread != null){ time = (System.currentTimeMillis()-startTime)/1000; temp1 = (int) (time * 10000); temp2 = temp1 % 10; temp1 -= temp2; time = ((float)temp1)/10000; cvy = g*time - vsin; cx = vcos*time; cy = cvy*time/2-vsin*time/2; try{ canvas = surfaceHolder.lockCanvas(); canvas.drawCircle( cx+BALL_R, cy+height-200, BALL_R, paint); } catch(Exception e){} finally { surfaceHolder.unlockCanvasAndPost(canvas); } canvas = surfaceHolder.lockCanvas(); canvas.drawColor(0, PorterDuff.Mode.CLEAR); surfaceHolder.unlockCanvasAndPost(canvas); long end = System.currentTimeMillis(); Log.d(getClass().getName(), "measure: " + (end-startTime)); } } }
回答1件
あなたの回答
tips
プレビュー