2012-01-26 21 views
22

पर स्क्रीन अभिविन्यास को ठीक करें मैं फोन अभिविन्यास प्राप्त करना चाहता हूं लेकिन स्क्रीन अभिविन्यास को चित्र में रखना चाहता हूं। कोई फर्क नहीं पड़ता कि उपयोगकर्ता फोन को लैंडस्केप या पोर्ट्रेट में बदल देता है, दृश्य वही रहता है, लेकिन मैं यह देख सकता हूं कि यह परिदृश्य या चित्र में बदल गया है या नहीं।फोन अभिविन्यास प्राप्त करें लेकिन पोर्ट्रेट

एंड्रॉयड के लिए गतिविधि की स्थापना: screenOrientation = "चित्र" दोनों ठीक कर देंगे, लेकिन मैं के माध्यम से

public void onConfigurationChanged(Configuration newConfig) { 
    switch (newConfig.orientation) { 
    case Configuration.ORIENTATION_PORTRAIT: 
     Toast.makeText(this, "Portrait", Toast.LENGTH_SHORT).show(); 
     break; 
    case Configuration.ORIENTATION_LANDSCAPE: 
     Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show(); 
     break; 
    default: 
     break; 
    } 
} 

फोन उन्मुखीकरण पता लगाने के लिए किसी को भी इसका अंदाज़ा लगा सकते है कि ठीक करने के लिए किया गया है में सक्षम नहीं होगा?

+0

इस पर एक नज़र डालें: http://stackoverflow.com/a/41104983/2267723 सेंसर मैनेजर का उपयोग करके यह समाधान। –

उत्तर

19

क्या आप एक्सेलेरोमीटर के साथ अपनी आवश्यकता को पूरा कर सकते हैं? यदि ऐसा है, तो शायद इस तरह के कुछ (अनचाहे) टुकड़े आपके उद्देश्यों के अनुरूप होंगे।

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"); 
        } 
        orientation=1; 
       } else { 
        if (orientation!=0) { 
         Log.d("Sensor", "Portrait"); 
        } 
        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

हाय फिलिप। यही वही समाधान है जिसे मैंने स्वयं लागू किया। इस संदर्भ में डिवाइस के अभिविन्यास को प्राप्त करने के लिए सेंसर तक पहुंचने के लिए कोई दूसरा विकल्प नहीं लगता है। – Arman

+4

यदि डिवाइस किसी तालिका पर फ्लैट लगा रहा है तो बहुत अधिक मूल्य नहीं है। – AndroidDev

+2

कोई भी जानता है कि रिवर्स_लैंडस्केप का पता कैसे लगाया जाए? – edrian

0

आप

android:configChanges="orientation|keyboardHidden" 

फिर उस गतिविधि में मैनिफ़ेस्ट फ़ाइल में सेट करना चाहते हैं जब उपयोगकर्ता घुमाने फोन कहीं भी होगी आया सार्वजनिक शून्य onConfigurationChanged में() विधि है कि में प्राप्त करने के लिए।

android:screenOrientation="portrait" 

उसी गतिविधि से हटा दें।

+1

लेकिन अगर कॉन्फ़िगरेशन चेंज() को ओरिएंटेशन चेंज कहा जाता है तो पहले ही किया जा चुका है ... मुझे पहले जानकारी चाहिए। – Arman

2

आप स्क्रीन अभिविन्यास परिवर्तन तो जाहिर है onConfigurationChanged कहा जा कभी नहीं होगा अक्षम करते हैं ...

मुझे लगता है कि एक ही रास्ता accelerometer सेंसर का उपयोग करने, जाँच this link है।

+0

टूटा लिंक। क्या आप इसे अपडेट कर सकते हैं? –

+0

टूटा लिंक अपडेट किया गया। – vitakot

25

यहाँ आसानी से स्क्रीन अभिविन्यास परिवर्तन के प्रबंधन के लिए एक बहु प्रयोजन वर्ग है:

public class OrientationManager extends OrientationEventListener { 

    public enum ScreenOrientation { 
     REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT 
    } 

    public ScreenOrientation screenOrientation; 
    private OrientationListener listener; 

    public OrientationManager(Context context, int rate, OrientationListener listener) { 
     super(context, rate); 
     setListener(listener); 
    } 

    public OrientationManager(Context context, int rate) { 
     super(context, rate); 
    } 

    public OrientationManager(Context context) { 
     super(context); 
    } 

    @Override 
    public void onOrientationChanged(int orientation) { 
     if (orientation == -1){ 
      return; 
     } 
     ScreenOrientation newOrientation; 
     if (orientation >= 60 && orientation <= 140){ 
      newOrientation = ScreenOrientation.REVERSED_LANDSCAPE; 
     } else if (orientation >= 140 && orientation <= 220) { 
      newOrientation = ScreenOrientation.REVERSED_PORTRAIT; 
     } else if (orientation >= 220 && orientation <= 300) { 
      newOrientation = ScreenOrientation.LANDSCAPE; 
     } else { 
      newOrientation = ScreenOrientation.PORTRAIT;      
     } 
     if(newOrientation != screenOrientation){ 
      screenOrientation = newOrientation; 
      if(listener != null){ 
       listener.onOrientationChange(screenOrientation); 
      }   
     } 
    } 

    public void setListener(OrientationListener listener){ 
     this.listener = listener; 
    } 

    public ScreenOrientation getScreenOrientation(){ 
     return screenOrientation; 
    } 

    public interface OrientationListener { 

     public void onOrientationChange(ScreenOrientation screenOrientation); 
    } 
} 

इस तरह अधिक सरल है, पुन: प्रयोज्य, और आप भी REVERSE_LANDSCAPE और REVERSE_PORTRAIT झुकाव मिल सकती है।

अभिविन्यास परिवर्तन होने पर आपको अधिसूचित होने के लिए ओरिएंटेशन लिस्टर को कार्यान्वित करना होगा।

मत भूलना उन्मुखीकरण ट्रैकिंग आरंभ, और फिर orientationManager.disable() कॉल करने के लिए orientationManager.enable() कॉल करने के लिए (यह दो तरीकों OrientationEventListener वर्ग से लिए गए हैं)

अद्यतन: उपयोग के मामले उदाहरण

MyFragment extends Fragment implements OrientationListener { 

    ... 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this); 
     orientationManager.enable();   
    } 

    @Override 
    public void onOrientationChange(ScreenOrientation screenOrientation) { 
     switch(screenOrientation){ 
      case PORTRAIT: 
      case REVERSED_PORTRAIT: 
       MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 
      case REVERSED_LANDSCAPE: 
       MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
      break; 
      case LANDSCAPE: 
       MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      break; 
     } 
    } 
} 
+0

बहुत अच्छा, बहुत बहुत धन्यवाद। इस जवाब को विश्वास नहीं कर सकता केवल एक अपवित्र हो गया। – formatc

+4

ग्रेट समाधान, लेकिन कृपया ध्यान रखें कि यह केवल स्मार्टफ़ोन पर सही परिणाम देता है जिसमें पोर्ट्रेट मोड डिफ़ॉल्ट मोड के रूप में होता है। गोलियों पर (लैंडस्केप के साथ डिफ़ॉल्ट रूप से), अभिविन्यास के परिदृश्य के मामले में अभिविन्यास का मूल्य 0 होगा, यानी, आपको अपने तर्क को लागू करने से पहले अभिविन्यास मान में 270 जोड़ना होगा। डिफ़ॉल्ट डिवाइस अभिविन्यास के लिए, http://stackoverflow.com/questions/4553650/how-to-check-device- प्राकृतिक-default-orientation-on-android-ie-get-landscape –

+0

और 3 साल बाद मैं ' मैं इसे ढूंढ रहा हूं और यह एक सुरुचिपूर्ण समाधान है ... अगर मैं आपको केवल 1 से अधिक ऊपरी हाहा दे सकता हूं। –

-2

यदि कोई व्यक्ति प्रश्न के लिए वेबव्यू/जावास्क्रिप्ट समाधान की तलाश में है, तो नीचे ऐसा कर सकता है।

यह खिड़की पर कस्टम 'फ्लिप' घटनाओं को ट्रिगर करेगा, जिसमें 'अतिरिक्त पैरामीटर' के साथ jquery है। यह भी window.flip, window.orientation के अनुरूप सेट:

$(window).on('flip',function(ev,angle,orientation) { 
    console.log(angle,orientation); 
    alert(window.flip); 
}); 

if (window.DeviceOrientationEvent) { 
    jQuery.flip = { 
     debug  : false, 
     interval : 1000, 
     checked  : false, 
     betaflat : 25, 
     gammaflat : 45, 
     orientation : 'portrait-primary', 
     angles  : { 
      'portrait-primary'  : 0, 
      'portrait-secondary' : 0, 
      'landscape-primary'  : 90, 
      'landscape-secondary' : -90  
     }, 
     timer  : null, 
     check  : function(ev) { 
      if (!this.checked) { 
       var trigger=false; 
       if (this.debug) console.log([ev.alpha,ev.beta,ev.gamma]); 
       if (ev.beta>this.betaflat) { 
        // if beta is big its portrait 
        if (this.debug) console.log('beta portrait pri'); 
        if (this.orientation!='portrait-primary') { 
         this.orientation='portrait-primary'; 
         trigger=true; 
        } 
       } else if (ev.beta<-this.betaflat) { 
        // if beta is big its portrait 
        if (this.debug) console.log('beta portrait sec'); 
        if (this.orientation!='portrait-secondary') { 
         this.orientation='portrait-secondary'; 
         trigger=true; 
        } 
       } else if (ev.gamma>this.gammaflat) { 

        // else if gamma is big its landscape 
        if (this.debug) console.log('gamma landscape pri'); 
        if (this.orientation!='landscape-primary') { 
         this.orientation='landscape-primary'; 
         trigger=true; 
        } 

       } else if (ev.gamma<-this.gammaflat) { 

        // else if gamma is big its landscape 
        if (this.debug) console.log('gamma landscape sec'); 
        if (this.orientation!='landscape-secondary') { 
         this.orientation='landscape-secondary'; 
         trigger=true; 
        } 

       } 
       if (trigger) { 
        if (this.debug) console.log('trigger flip'); 
        window.flip = this.angles[this.orientation]; 
        $(window).trigger('flip',[window.flip,this.orientation]); 
        this.checked=true; 
       } 
      } 
     } 
    } 
    $(document).ready(function() { 
     setInterval(function() {jQuery.flip.checked=false},jQuery.flip.interval); 
     $(window).on('deviceorientation',function(ev) { jQuery.flip.check(ev.originalEvent) }); 
    }); 
} else { 
    if (this.debug) console.log('DeviceOrientationEvent not supported'); 
} 

jQuery वास्तव में जरूरत नहीं है। मैं वैसे भी यह आवश्यक था।

+0

डाउनवोट क्यों? – commonpike

+1

जावास्क्रिप्ट के साथ एक एंड्रॉइड समस्या हल करना वास्तव में सहायक नहीं है। इसमें एक वेबव्यू या स्क्रिप्ट चलाने के समान होना आवश्यक है जो मामला नहीं हो सकता है। – Arman

+0

ओह - अच्छा बिंदु। अपने खुद के बुलबुले में रहना उत्तर अद्यतन कर रहा है। – commonpike

1

मुझे एक ऐसे समाधान की आवश्यकता है जो मुझे केवल मांग पर अभिविन्यास दे। यह मेरे लिए काम करता है:

public class SensorOrientationChecker { 

public final String TAG = getClass().getSimpleName(); 

int mOrientation = 0; 
private SensorEventListener mSensorEventListener; 
private SensorManager mSensorManager; 

private static SensorOrientationChecker mInstance; 

public static SensorOrientationChecker getInstance() { 
    if (mInstance == null) 
     mInstance = new SensorOrientationChecker(); 

    return mInstance; 
} 

private SensorOrientationChecker() { 
    mSensorEventListener = new Listener(); 
    Context applicationContext = GlobalData.getInstance().getContext(); 
    mSensorManager = (SensorManager) applicationContext.getSystemService(Context.SENSOR_SERVICE); 

} 

/** 
* Call on activity onResume() 
*/ 
public void onResume() { 
    mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 
} 

/** 
* Call on activity onPause() 
*/ 
public void onPause() { 
    mSensorManager.unregisterListener(mSensorEventListener); 
} 

private class Listener implements SensorEventListener { 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     float x = event.values[0]; 
     float y = event.values[1]; 

     if (x<5 && x>-5 && y > 5) 
      mOrientation = 0; 
     else if (x<-5 && y<5 && y>-5) 
      mOrientation = 90; 
     else if (x<5 && x>-5 && y<-5) 
      mOrientation = 180; 
     else if (x>5 && y<5 && y>-5) 
      mOrientation = 270; 

     //Log.e(TAG,"mOrientation="+mOrientation+" ["+event.values[0]+","+event.values[1]+","+event.values[2]+"]"); 
                     } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 

} 

public int getOrientation(){ 
    return mOrientation; 
    } 
} 
संबंधित मुद्दे