私はこのコードをここに持っています。私の頭には、カリオストーリーの文字列をcCounterの値に設定しました.cCounterは、cburnという整数からテキストを取得する文字列です。 。問題は、私がアプリケーションを実行すると、caloriestextは0.0に設定され、更新されません。センサーイベントの変更時の変数のインクリメント
私はこれがおそらく初歩的なようだと知っていますが、私は一週間中働くように努力してきました。あなたが提供できるお手伝いをしてくれてありがとう。
package shake.shake;
/**
* Created by ink on 3/24/16.
*/
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class main extends Activity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private final float NOISE = (float) 3.3;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Context context = this;
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.app_soundtrack);
mediaPlayer.start();
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
Context context = this;
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.app_soundtrack);
mediaPlayer.stop();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.shake1);
img.setBackgroundResource(R.drawable.shake_animation);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.y_axis);
TextView tvZ= (TextView)findViewById(R.id.z_axis);
TextView cCounter = (TextView)findViewById(R.id.caloriestext);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
cCounter.setText("0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
int cburn = 0;
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
cCounter.setText(Float.toString(cburn));
if (deltaX > deltaY) {
frameAnimation.start();
cburn++;
} else if (deltaY > deltaX) {
frameAnimation.start();
cburn++;
} else {
frameAnimation.stop();
}
}
}
}
私はそれはあなたがy_axis
match_parent
、他のTextViewsの高さを加えた場合、ここで重要な、しかし、対応する.xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="243dp"
android:layout_height="66dp"
android:id="@+id/caloriestext"
android:gravity="center"
android:text="Calories:"
android:textSize="20pt"
android:textStyle="bold"
android:layout_column="12"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="12pt"
android:id="@+id/calories"
android:gravity="center" />
<ImageView
android:id="@+id/shake1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/shake1"
android:layout_gravity="center" />
<TextView
android:id="@+id/y_axis"
android:layout_height="match_parent"
android:layout_width="1pt"
/>
<TextView
android:id="@+id/z_axis"
android:layout_height="match_parent"
android:layout_width="1pt"
/>
<TextView
android:id="@+id/x_axis"
android:layout_height="match_parent"
android:layout_width="1pt"
/>
</LinearLayout>
でしょうないでcburn ++ ifとelse文は、それを変更した場合は? –
ああ、そうです! 'onSensorChanged'メソッドの中にcburnを作成します。この行を 'int cburn = 0;'プライベートセンサーmAccelerometerの下に移動してください;私の答えを更新しています! – iamkaan