私の友人と私は、いくつかのオープンソースのグーグルプロジェクトコードであるaccelerometerplayに基づいた大理石のゲームを実装しています。私たちの目的は、Android開発を自分自身に教えるためのおもちゃのプログラムを作成することです。私たちは次のことを経験しています:
問題:
大理石は画面上で非常にゆっくりと回転します。何かがスローモーションで動き回るのを見るようなものです。それはひどいです。どうすればそれをより速く/通常のプレイにすることができますか?
私たちがしたこと:
タイルからマップを描画するコードを強制的にフィード加速度計で再生します。これは物事を遅くしますが。それでもかなりうまく動作します。
元の加速度計の再生に近づけるようにビューを書き直しましたが、大理石の動作が遅すぎるというまったく同じ問題があります。これにより、異なるクラスのアプローチと単純化についての疑問がなくなります。
このフォーラムを検索しました。invalidate()を使用すると処理が遅くなるとのことですが、それが問題であるとは考えていません。画面上でのみ移動する大理石を1つ描画するために、onDraw()を強制終了するべきではありません。
プログラムのビュー部分には、以下のコードが含まれています。アクティビティや物理コードなどの他のクラスは機能しています。このクラスでは、大理石を描画して動き回ろうとしているときに問題が発生していることを100%確信しています。
package com.example.marblez;
import com.example.marblez.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
public class MarblezView extends View implements SensorEventListener{
//Sensor Stuff
private SensorManager mSensorManager;
private Display mDisplay;
//Accelerometer sensor stuff
private Sensor mAccelerometer;
private float mSensorX;
private float mSensorY;
//Variables related to time
private long mCpuTimeStamp;
private long mSensorTimeStamp;
private WindowManager mWindowManager;
//Create the canvas
private Canvas mCanvas;
private MarblezBackground background;
//Create the ball objects
private Bitmap mBall;
private Bitmap ball;
//Finally call marblez system
private final MarblezSystem mMarblezSystem = new MarblezSystem();
//Constructor for Marblez View
@SuppressWarnings("deprecation")
public MarblezView(Context context, Activity activity){
super(context);
//Setup sensor stuff
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = mWindowManager.getDefaultDisplay();
//Set up Maze
background = new MarblezBackground(activity);
//Create our rolling little friend :-)
Options opts = new Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
mBall = BitmapFactory.decodeResource(activity.getApplicationContext().getResources(), R.drawable.ball, opts);
// Rescale the ball to be whatever size you wish it to be
final int dstWidth = (int) 30;//(sBallDiameter * mMetersToPixelsX + 0.5f);
final int dstHeight = (int) 30;//(sBallDiameter * mMetersToPixelsY + 0.5f);
ball = Bitmap.createScaledBitmap(mBall, dstWidth, dstHeight, true);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
/*
* record the accelerometer data, the event's timestamp as well as
* the current time. The latter is needed so we can calculate the
* "present" time during rendering. In this application, we need to
* take into account how the screen is rotated with respect to the
* sensors (which always return data in a coordinate space aligned
* to with the screen in its native orientation).
*/
mSensorX = event.values[0];
mSensorY = -event.values[1];
mSensorTimeStamp = event.timestamp;
mCpuTimeStamp = System.nanoTime();
}
//Automatic call
@Override
public void onDraw(Canvas canvas){
//mCanvas = canvas;
//Draw the maze
//background.drawMaze(canvas);
//Update the position and then draw the marble
final MarblezSystem marblezSystem = mMarblezSystem;
final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);
final float sx = mSensorX;
final float sy = mSensorY;
marblezSystem.updatePositions(sx, sy, now);
final float x = marblezSystem.getPosX();
final float y = marblezSystem.getPosY();
canvas.drawBitmap(ball, x, y, null);
//Invalidate so it draws again
invalidate();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
大理石の速度を低下させているものにレンガの壁をぶつけました。私たちはSamsungGalaxyタブレット、10.1を使用していると思います。大理石がゆっくり動く原因が誰かにわかる場合は、お知らせください。onDrawメソッドで何かをしていますか?ガイドを含むすべての読み物は、これが機能するはずであることを示しているようですが、大理石の動きが非常に遅い理由を理解できないようです。
よろしくお願いします、GeekyOmega
編集:
読者はupdatePositions()を見たいと思っていましたが、これはgoogleオープンソースの例から採用されているので、次のようになります。
package com.example.marblez;
public class MarblezSystem {
private long mLastT;
private float mLastDeltaT;
private Marblez ball;
MarblezSystem() {
/*
* Initially our marble have no speed or acceleration
*/
ball = new Marblez();
}
/*
* Update the position of marble in the system using the
* Verlet integrator.
*/
public void updatePositions(float sx, float sy, long timestamp) {
final long t = timestamp;
if (mLastT != 0) {
final float dT = (float) (t - mLastT) * (1.0f / 1000000000.0f);
if (mLastDeltaT != 0) {
final float dTC = dT / mLastDeltaT;
//Particle ball = mBalls[i];
ball.computePhysics(sx, sy, dT, dTC);
}
mLastDeltaT = dT;
}
mLastT = t;
}
public float getPosX() {
return ball.mPosX;
}
public float getPosY() {
return ball.mPosY;
}
}//End of MarblezSystem