加速度计挂了

时间:2012-01-10 08:33:35

标签: android android-activity accelerometer

嗨,这是我从链接Moving an image using Accelerometer of android

中获取的代码
package com.emblem.accelerometer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

public class AccelerometerActivity extends Activity implements
    SensorEventListener {
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;

private SensorManager sensorManager = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Get a reference to a SensorManager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mCustomDrawableView = new CustomDrawableView(this);
    setContentView(mCustomDrawableView);
    // setContentView(R.layout.main);

}

// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // the values you were calculating originally here were over
        // 10000!
        x = (int) Math.pow(sensorEvent.values[0], 2);
        y = (int) Math.pow(sensorEvent.values[1], 2);

    }

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

    }

}

// I've chosen to not implement this method
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onResume() {
    super.onResume();
    // Register this class as a listener for the accelerometer sensor
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
    // ...and the orientation sensor
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
    // Unregister the listener
    sensorManager.unregisterListener(this);
    super.onStop();
}

public class CustomDrawableView extends View {
    static final int width = 50;
    static final int height = 50;

    public CustomDrawableView(Context context) {
        super(context);

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF oval = new RectF(AccelerometerActivity.x,
                AccelerometerActivity.y, AccelerometerActivity.x + width,
                AccelerometerActivity.y + height); // set bounds of
                                                    // rectangle
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        invalidate();
    }
}
}

但是活动只显示空白屏幕并且还挂起。 任何人都可以帮忙吗? 感谢

1 个答案:

答案 0 :(得分:1)

我已复制你的代码并编译/运行它,我的手机和模拟器显示都没有错,如图1和图2所示。

值得通知蓝色椭圆在仿真器上保持静态,因为仿真器没有加速度计。另一方面,当我移动或摇动手机时,蓝色椭圆形在一个矩形内保持动态。如果椭圆形仍然在手机上保持静电,则应检查硬件和软件。硬件意味着手机上的加速度计芯片,而软件意味着您的驱动器和加速度计的HAL。

可以轻松安装某些应用程序,例如“android sensor box”来检查您是否可以从加速度计读取3轴值。您还可以查看logcat中的日志以检查传感器服务。

我的 AndroidManifest.xml 如下所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.so_problem"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.so_problem.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

图1在模拟器上运行。 enter image description here

图2在手机上运行。 enter image description here