2016-03-17 18 views
0

在我的應用程序中,我想旋轉方向更改時的圖像,當方向改變發生時,是否可以旋轉圖像視圖?在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; 
} 

這是工作不錯,但它只是基於圖像的方向檢查在運行時的方向和旋轉圖像,我需要實時檢查設備的方向和要旋轉圖像以橫向模式,如果設備是在橫向,並反之亦然。任何人都可以幫助我??請提前致謝。

+1

是ovveride onConfigChange方法和檢查縱向還是橫向,做你想讓它什麼 –

+0

是可以旋轉的imageview的時候方向改變? – Gibs

+0

是的,我告訴過你邏輯,你如何檢測設備的方向改變。並把你的邏輯旋轉ImageView裏面的方法 –

回答

1
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); 
    } 
+0

我真的很抱歉,最近的回覆,非常感謝你Khizar Hayat。此代碼作品 – Gibs

+0

歡迎你。並感謝接受答案。 –

+0

有人懷疑什麼是價值6.5 ?? – Gibs

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

也明顯提到這一行,如果上面的代碼是不夠的;對於活動提到這個** android:ConfigChanges =「keyboardHidden | orientation」** – DJphy

+0

謝謝你:我已經試過這段代碼,但這不起作用,我的活動的屏幕方向已經設置爲清單中的「肖像」,因爲我的應用程序只支持肖像模式,它是一個攝像頭應用程序,我有兩個片段第一個片段將處理相機拍攝功能,並將拍攝的圖像發送到第二個片段,第二個片段佈局具有圖像視圖,用於顯示由用戶,所以我想在方向改變時旋轉此片段的圖像視圖。此代碼將僅在將方向更改爲「全面傳感器」時起作用, – Gibs

+0

請提供任何幫助嗎? – Gibs

0
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" 
+0

謝謝你:我已經試過這段代碼,但這不起作用,我的活動的屏幕方向已經設置爲清單中的「肖像」,因爲我的應用僅支持肖像模式,它是一個攝像頭應用程序,並且我有兩個片段第一個片段將處理相機拍攝功能並將拍攝的圖像發送到第二個片段,第二個片段佈局具有用於顯示用戶拍攝的圖像的圖像視圖,所以我想在方向更改時旋轉此片段的圖像視圖。此代碼將起作用只有當我改變方向爲「全傳感器」, – Gibs

+0

任何幫助嗎? – Gibs

+0

當你限制活動肖像,然後這段代碼將無法工作 –

相關問題