Position an Image View by mobile accelerometer

Asked

Viewed 138 times

0

I want to create a game on using android studio where my image view positions according to the movement I perform on mobile.

For example: When starting the application the image appears in the center of the cell phone, and when moving the device to the sides the image moves taking the values of the initial position and the current position of the device.

Can rotate the image, but still I’m not getting a good accuracy of the sensor and neither move the image horizontally or vertically, below be 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();
    }


  }
}

XML of the application:

<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>

1 answer

1

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

Create speed variables for X and Y and initialize them with wherever you want the ImageView boot, in onSensorChanged add the acceleration value to the variables and you will have your speed.

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

It is not recommended to make a game directly in Android Studio, these Apis are thought to be used in applications, to make a game, use a game engine like Unity or Gamemaker: Studio.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.