我正在尝试使用我在Android应用程序中使用TranslateAnimation的sensorchange(在加速度计上)移动一个imageview(球图像)。但是应用程序不会移动imageview。我的日志看起来应用程序甚至没有注册加速度计,我只看到屏幕上的球图像视图在一个地方不变。有人可以帮我解决我在这里做错了什么吗?我的代码和日志如下:
public class MainActivity extends Activity implements SensorEventListener {
private ImageView ball=null;
public static int nwidth, nheight;
public static float xPosition, yPosition,oldx=0,oldy=0;
private float xAcceleration=0.0f,xVelocity = 0.0f;
private float yAcceleration=0.0f,yVelocity = 0.0f;
private float zAcceleration=0.0f;
public float frameTime = 0.999f;
private float mAlpha = 0.9f;
private static final String TAG = "Readings";
String toWrite=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ball = (ImageView) findViewById(R.id.image_ball);
getScreenSize();
setContentView(R.layout.activity_main);
}
private void getScreenSize()
{
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Rect windowSize = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(windowSize);
nwidth = displaySize.x - Math.abs(windowSize.width());
nheight = displaySize.y - Math.abs(windowSize.height());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent)
{
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
yAcceleration = lowPass(sensorEvent.values[1], yAcceleration);
xAcceleration = lowPass(sensorEvent.values[2],xAcceleration);
zAcceleration = lowPass(sensorEvent.values[0],zAcceleration);
toWrite = sensorEvent.values[0] + "," + sensorEvent.values[2] + "\n";
writeCSV(toWrite);
Log.i(TAG, "x:" + xAcceleration + " y:" + yAcceleration + "z:" + zAcceleration);
updateBall();
}
}
}
// simple low-pass filter
float lowPass(float current, float filtered) {
return mAlpha * current + (1.0f - mAlpha) * filtered;
}
private void writeCSV(String txtData)
{
try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ "Readings.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile,true);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData);
myOutWriter.close();
fOut.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
private void updateBall() {
//for landscape orientation, treating z axis as x.
xVelocity = (-zAcceleration * frameTime);
float xS = (xVelocity)*frameTime;
xPosition += xS;
// yPosition += yS;
yPosition++;
setPosition();
Log.i(TAG,"x:" + xAcceleration +" y:" + yAcceleration);
}
private void setPosition()
{
TranslateAnimation animation = new TranslateAnimation(oldx, xPosition, oldy, yPosition);
animation.setDuration(10);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener(ball));
ball.startAnimation(animation);
oldx=xPosition;
oldy=yPosition;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
class MyAnimationListener implements Animation.AnimationListener {
ImageView imageView=null;
public MyAnimationListener(ImageView image)
{
imageView=image;
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.clearAnimation();
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(imageView.getWidth(), imageView.getHeight());
// lp.setMargins(50, 100, 0, 0);
imageView.setLayoutParams(lp);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
08-12 01:03:21.348: I/art(3404): Late-enabling -Xcheck:jni
08-12 01:03:21.386: E/art(3404): Failed sending reply to debugger: Broken pipe
08-12 01:03:21.386: I/art(3404): Debugger is no longer active
08-12 01:03:21.516: D/OpenGLRenderer(3404): Render dirty regions requested: true
08-12 01:03:21.522: D/Atlas(3404): Validating map...
08-12 01:03:21.568: I/OpenGLRenderer(3404): Initialized EGL, version 1.4
08-12 01:03:21.609: D/OpenGLRenderer(3404): Enabling debug mode 0
谢谢!
答案 0 :(得分:0)
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager mSensorManager;
Sensor mSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
}
}