2012-01-10 40 views
0

您好,这里是我的代码,我从链接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(); 
    } 
} 
} 

采取但活动显示什么只是一个空白屏幕,并挂起。 任何人都可以帮助什么是错的? 感谢

+0

我认为你应该提供logcat。另外,也许你在模拟器上测试你的应用程序? – Yury 2012-01-10 09:45:32

+0

是的,我在emulater上运行它.. yury你可以修改以上代码吗?添加一个后台线程睡眠100毫秒,然后醒来,检查加速度计,然后睡觉 – Mudasar 2012-01-10 10:27:26

+0

我认为你应该自己做这件事,如果你得到一个特定的错误,你可以问我们。这是这种服务的工作原理。在仿真器中,您没有加速度计,所以我不惊讶您会遇到错误。您应该在真实设备上测试此功能。 – Yury 2012-01-10 17:10:27

回答

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