当方向发生变化时是否可以旋转图像视图?在 Android

Is it possible to rotate an imageview when orientation change's occur ? in Android

在我的应用程序中,我想在方向改变时旋转图像,

为了做到这一点,我尝试了这个代码片段;

 public Bitmap rotateImage(String image_path){

    Log.e("Inside rotATE IMAGE", "ROTATE"+image_path);
    Matrix matrix = new Matrix();
    matrix.postRotate(getImageOrientation(image_path));

    bitmap= decodeFile(image_path);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
            bitmap.getHeight(), matrix, true);


    return  rotatedBitmap;
}

public static int getImageOrientation(String imagePath){
    int rotate = 0;
    try {

        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(
                imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        Log.e("ori","or" +orientation);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rotate;
}

这工作正常,但它只在 运行 时间检查方向并根据图像的方向旋转图像,我需要实时检查设备的方向并想要旋转如果设备处于横向,则图像为横向模式,反之-versa.Can有人帮我吗??提前谢谢。

ON configuration changed u can detect like this; u will get a call back;

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        getImageOrientation();
        rotateImage();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        getImageOrientation();
        rotateImage();
    }
}
Try this simple code in your activity:

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            iv.setRotation(90f);
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            iv.setRotation(0);
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

and following line in your manifest in activity tag
            android:configChanges="orientation|keyboardHidden|screenSize"
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");
                        iv.setRotation(90f);
                    }
                    orientation = 1;
                } else {
                    if (orientation != 0) {
                        Log.d("Sensor", "Portrait");
                        iv.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);
    }