2012-01-10 14 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

+0

はい私はそれをエミュレータで実行します.yuryあなたは私のために上記のコードを変更できますか?バックグラウンドスレッドを追加して、100ミリ秒間スリープ状態にしてから、起きて、目を覚まし、加速度計をチェックしてから、スリープしてください。 – Mudasar

+0

自分でこれを行うべきだと思っています。これがこのサービスの仕組みです。エミュレータでは加速度計がないので、エラーが発生するのは驚きではありません。実際のデバイスでこの機能をテストする必要があります。 – Yury

答えて

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

関連する問題