Positioning an Image View by the mobile accelerometer

0

I want to create a game using android studio where my image view is positioned according to the movement I make in the mobile.

For example: When you start the application, the image appears in the center of the phone, and when you move the device sideways the image moves by taking the values of the initial position and the current position of the device.

To get the image to rotate, but still I'm not getting a good sensor precision or moving the image horizontally or vertically, below is the code I'm using so far

MainActivity:

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private TextView tvValorX;
private TextView tvValorY;
private TextView tvValorZ;

private ImageView eixoX;
private View eixoY;

private SensorManager mSensorManager;
private Sensor mAcelerometro;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvValorX = (TextView) findViewById(R.id.tvValorX);
    tvValorY = (TextView) findViewById(R.id.tvValorY);
    tvValorZ = (TextView) findViewById(R.id.tvValorZ);

    eixoX = findViewById(R.id.imagem);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAcelerometro = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mAcelerometro, SensorManager.SENSOR_DELAY_UI);
}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    tvValorX.setText(String.valueOf(x));
    tvValorY.setText(String.valueOf(y));
    tvValorZ.setText(String.valueOf(z));

    float rotation = eixoX.getRotation();
    eixoX.setRotation(rotation + x);

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void btMeusSensoresOnClick(View v) {
    List<Sensor> listaSensores = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    String[] lista = new String[listaSensores.size()];

    for (int i = 0; i < listaSensores.size(); i++) {
        lista[i] = listaSensores.get(i).getName();
    }


  }
}

Application XML:

<LinearLayout 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"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Valor x:" />

<TextView
    android:id="@+id/tvValorX"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Valor y:" />

<TextView
    android:id="@+id/tvValorY"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Valor Z:" />

<TextView
    android:id="@+id/tvValorZ"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="" />

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imagem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="112dp"
        tools:layout_editor_absoluteY="95dp" />

</android.support.constraint.ConstraintLayout>

</LinearLayout>
    
asked by anonymous 18.01.2018 / 17:19

1 answer

1

You have confused things a bit, the accelerometer returns the acceleration of the device, not the initial position and the current position of the device.

Create velocity variables for X and Y and initialize them with where you want the ImageView to initialize, in onSensorChanged add the acceleration value in the variables and get its speed.

To change the position of ImageView , you will need to use LayoutParams with the variables you defined.

We do not recommend making a game directly in Android Studio, these APIs are meant to be used in applications, to make a game, use a game engine like Unity or GameMaker: Studio.

Doing this way ImageView will move at strange speeds, since there is no control of the speed of the sensor update.

    
18.01.2018 / 17:59