一个简单的用加速度计移动的圆形



我根据目前的情况再次重新编辑了整个主题,因为我对上一个主题有很多看法,但没有回复。与此同时,我有点搞清楚了加速度计是如何工作的。

现在我有一个cyrcle(画布),如果你不介意的话,我想称之为"Zoga"。这个圆形应该根据手机的角度移动。因此,基本上,如果手机向左移动,圆圈会在左侧移动,如果手机向右下方移动,圆圈就会朝那个方向移动。

cyrcle是通过Zoga.java类创建的,整个魔法在GravitachiaActivity.jav.中使用

以下是我的两个问题:
1.)圆形只向左方向移动
2.)圆形在屏幕外移动(在课程左侧)

关于如何解决这个问题有什么想法吗?

注意:我已经附上了我的整个代码,甚至是layout main.xml,以防其他人以后出于教育和学习目的需要该代码:)

GravitaijaActivity.java

package gravity.pack;
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class GravitacijaActivity extends Activity implements SensorEventListener{
    public float xPos = 50.0f, yPos = 50.0f;
    public float xAcc = 0.0f, yAcc = 0.0f;
    public int radius = 30;
    private SensorManager sensorManager;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                    SensorManager.SENSOR_DELAY_GAME);
        FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
        main.addView(new Zoga(this, xPos, yPos, radius));
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent sensorArg) {
    if (sensorArg.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
            xAcc = sensorArg.values[1];
            yAcc = sensorArg.values[2];
        updateZoga();
    }
}
public void updateZoga()
{
    xPos += xAcc;
    yPos += yAcc;
    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.removeAllViews();
    main.addView(new Zoga(this, xPos, yPos, radius));
    try {
        Thread.sleep(1);
    } catch (InterruptedException e) {}
  }         
}


Zoga.java

package gravity.pack;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class Zoga extends View{
    private final float x;
    private final float y;
    private final float r;
    private final Paint mPaint = new Paint (Paint.ANTI_ALIAS_FLAG);
    public Zoga(Context context, float x, float y, float r) {
        super(context);
        mPaint.setColor(0xFFFF0000);
        this.x = x;
        this.y = y;
        this.r = r;
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawCircle(x, y, r, mPaint);
    } 
}


布局main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/main_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF66FF33" />

你犯了一个错误。您正在将加速度值添加到位置值中。相反,你应该不断增加加速度的值,通过这个,你会得到速度的值。现在,如果你继续加速度的值,你就会得到位置的值。现在,您应该将这个位置值添加到函数updateZoga

我在onSensorChange代码中做了一些更改,以在屏幕中移动球。在我的例子中,球没有正确移动,为此我做了改变。这个例子适用于我的.

public void onSensorChanged(SensorEvent sensorEvent)
{
    //Try synchronize the events
    synchronized(this){
    //For each sensor
    switch (sensorEvent.sensor.getType()) {
    case Sensor.TYPE_MAGNETIC_FIELD: //Magnetic sensor to know when the screen is landscape or portrait
        //Save values to calculate the orientation
        mMagneticValues = sensorEvent.values.clone();
        break;
    case Sensor.TYPE_ACCELEROMETER://Accelerometer to move the ball
        if (bOrientacion==true){//Landscape
            //Positive values to move on x
            if (sensorEvent.values[1]>0){
                //In margenMax I save the margin of the screen this value depends of the screen where we run the application. With this the ball not disapears of the screen
                if (x<=margenMaxX){
                    //We plus in x to move the ball
                    x = x + (int) Math.pow(sensorEvent.values[1], 2);
                }
            }
            else{
                //Move the ball to the other side
                if (x>=margenMinX){
                    x = x - (int) Math.pow(sensorEvent.values[1], 2);
                }
            }
            //Same in y
            if (sensorEvent.values[0]>0){
                if (y<=margenMaxY){
                    y = y + (int) Math.pow(sensorEvent.values[0], 2);
                }
            }
            else{
                if (y>=margenMinY){
                    y = y - (int) Math.pow(sensorEvent.values[0], 2);
                }
            }
        }
        else{//Portrait
            //Eje X
            if (sensorEvent.values[0]<0){
                if (x<=margenMaxX){
                    x = x + (int) Math.pow(sensorEvent.values[0], 2);
                }
            }
            else{
                if (x>=margenMinX){
                    x = x - (int) Math.pow(sensorEvent.values[0], 2);
                }
            }
            //Eje Y
            if (sensorEvent.values[1]>0){
                if (y<=margenMaxY){
                    y = y + (int) Math.pow(sensorEvent.values[1], 2);
                }
            }
            else{
                if (y>=margenMinY){
                    y = y - (int) Math.pow(sensorEvent.values[1], 2);
                }
            }
        }
        //Save the values to calculate the orientation
        mAccelerometerValues = sensorEvent.values.clone();
        break;  
    case Sensor.TYPE_ROTATION_VECTOR:  //Rotation sensor
        //With this value I do the ball bigger or smaller
        if (sensorEvent.values[1]>0){
            z=z+ (int) Math.pow(sensorEvent.values[1]+1, 2);
        }
        else{
            z=z- (int) Math.pow(sensorEvent.values[1]+1, 2);                    
        }
    default:
        break;
    }
    //Screen Orientation
    if (mMagneticValues != null && mAccelerometerValues != null) {
        float[] R = new float[16];
        SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);
        float[] orientation = new float[3];
        SensorManager.getOrientation(R, orientation);
        //if x have positives values the screen orientation is landscape in other case is portrait
        if (orientation[0]>0){//LandScape
            //Here I change the margins of the screen for the ball not disapear
            bOrientacion=true;
            margenMaxX=1200;
            margenMinX=0;
            margenMaxY=500;
            margenMinY=0;
        }
        else{//Portrait
            bOrientacion=false;
            margenMaxX=600;
            margenMinX=0;
            margenMaxY=1000;
            margenMinY=0;
        }
    }
    }
}

我画球的视图类

public class CustomDrawableView extends View
{
    static final int width = 50;
    static final int height = 50;
    //Constructor de la figura
    public CustomDrawableView(Context context)
    {
        super(context);
        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }
    //Dibujamos la figura
    protected void onDraw(Canvas canvas)
    {
        //Actividad_Principal x,y,z are variables from the main activity where I have the onSensorChange
        RectF oval = new RectF(Actividad_Principal.x+Actividad_Principal.z, Actividad_Principal.y+Actividad_Principal.z, Actividad_Principal.x + width, Actividad_Principal.y + height);             
        Paint p = new Paint(); 
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        invalidate();
    }
}

}

仅此而已,我希望能帮助我们。

最新更新