こんにちは私はImageViewと加速度計で単純なボールを動かすためにこのコードを書きましたが、動作しません。私はball.scrollTo(xa、ya)を使用しましたが、このコードは可変キャッチ加速度計では機能しません
私のコードを投稿してください:
Main.java
package com.crowley.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
public class Main extends Activity implements SensorEventListener {
final String tag = "AccLogger";
SensorManager sensore=null;
TextView x=null;
TextView y=null;
TextView z=null;
TextView xat=null;
TextView yat=null;
String a;
String b;
private ImageView ball;
int xa=0;
int ya=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensore = (SensorManager) getSystemService(SENSOR_SERVICE);
x= (TextView) findViewById(R.id.x);
y= (TextView) findViewById(R.id.y);
z= (TextView) findViewById(R.id.z);
xat= (TextView) findViewById(R.id.textView1);
yat= (TextView) findViewById(R.id.textView2);
ball = (ImageView) findViewById(R.id.ball);
ball.scrollTo(xa, ya);
ball.setImageResource(R.drawable.ball);
}
public void onSensorChanged(SensorEvent event){
Sensor sensor = event.sensor;
float [] values = event.values;
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
x.setText("x"+ values[0]);
y.setText("y"+ values[1]);
z.setText("z"+ values[2]);
xa=(int)values[0];// this part of code is only test to see int x and y on Activity
ya=(int)values[1];
a=Integer.toString(xa);
b=Integer.toString(ya);
xat.setText(a);
yat.setText(b);
//x.setText("x"+ SensorManager.DATA_X);
//y.setText("y"+ SensorManager.DATA_Y);
//z.setText("z"+ SensorManager.DATA_Z);
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
protected void onResume() {
super.onResume();
Sensor Accel = sensore.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// register this class as a listener for the orientation and accelerometer sensors
sensore.registerListener((SensorEventListener) this, Accel, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
と私のmain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="" />
<TextView
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/x"
android:text="" />
<TextView
android:id="@+id/z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/y"
android:text="" />
<ImageView
android:id="@+id/ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ball" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="71dp"
android:layout_marginRight="66dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView1"
android:layout_marginTop="35dp"
android:text="TextView" />
</RelativeLayout>