Skip to main content
adicionou 54 caracteres ao conteúdo
Fonte Link
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(x*100rotation + 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();
    }


  }
}
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));

    eixoX.setRotation(x*100);

}

@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();
    }


  }
}
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();
    }


  }
}
Fonte Link

Posicionar uma Image View pelo acelerômetro do celular

Quero criar um jogo no utilizando o android studio onde a minha image view se posiciona de acordo com o movimento que realizo no celular.

Por exemplo: Ao iniciar a aplicação a imagem aparece no centro do celular, e ao movimentar o aparelho para os lados a imagem vai se movimentando pegando os valores da posição inicial e da posição atual do aparelho.

Conseguir rotacionar a imagem, mas mesmo assim não estou conseguindo uma boa precisão do sensor e nem mover a imagem na horizontal ou vertical, abaixo estar o código que estou usando até o momento

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

    eixoX.setRotation(x*100);

}

@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 da aplicação:

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