Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://stackoverflow.com/questions/36060182/is-it-possible-to-rotate-an-imageview-when-orientation-changes-occur-in-andro
- на главном активити один имадж вью
- package com.example.delmetmp;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.util.Log;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- checkOrientation();
- }
- public void checkOrientation() {
- SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
- sensorManager.registerListener(new SensorEventListener() {
- int orientation = -1;
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.values[1] < 6.5 && event.values[1] > -6.5) {
- if (orientation != 1) {
- Log.d("Sensor", "Landscape");
- findViewById(R.id.imageView).setRotation(90f);
- }
- orientation = 1;
- } else {
- if (orientation != 0) {
- Log.d("Sensor", "Portrait");
- findViewById(R.id.imageView).setRotation(0);
- }
- orientation = 0;
- }
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- // TODO Auto-generated method stub
- }
- }, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement