我已经在这里获得了这段代码,在我的脑海中,我将它设置为cories是一个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();
}
}
}
}
我认为它不重要,但这里是相应的.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>
&#13;
答案 0 :(得分:0)
首先,如果您将y_axis
match_parent
的高度设置为其他TextView,则无法在屏幕上显示。你应该把它们wrap_content
。
您的cCounter
TextView保留0.0
,因为您在cburn
方法中创建了onSensorChanged
变量。每次调用该方法时,都会再次创建cburn
变量并赋予0
值。您需要将cburn
设为全局。移动这一行:
int cburn = 0;
这里:
public class MainActivity extends Activity implements SensorEventListener {
...
int cburn = 0;
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
}
}