传感器事件变化的递增变量



我在这里得到了这段代码,在我的脑海中,我将caloriestext字符串设置为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>

首先,如果将y_axis的高度设置为match_parent,则其他TextViews将不会在屏幕上显示。你应该把它们做成wrap_content

您的cCounter TextView保持为0.0,因为您在onSensorChanged方法内部创建了cburn变量。每次调用该方法时,都会再次创建cburn变量并为其分配0值。您需要使cburn全球化。移动此行:

int cburn = 0;

此处:

public class MainActivity extends Activity implements SensorEventListener {
    ...
    int cburn = 0;
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
    }
}

最新更新