簡體   English   中英

發生方向改變時是否可以旋轉imageview? 在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;
}

這可以正常工作,但它僅在運行時檢查方向,然后根據圖像的方向旋轉圖像。我需要實時檢查設備的方向,如果設備是反之亦然。有人可以幫我嗎?謝謝您。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM