0

我想現在如果是觸發佈局之後,是因爲直接從橫向正確的方向切換到橫向左排列事件的重繪事件任何可能的方式。Android的景觀從右到左景觀事件

我試過OrientationEventListener。問題是它觸發的速度很快。我正在做UI更改,它們在景觀重繪之前就發生了,而且看起來很糟糕。

我可以只添加在改變一些延遲,但根據手機上的這一變化可能會持續多跌少。

對於onConfigurationChanged的時機是完美的,當然它僅適用於縱向到橫向或橫向到縱向。

我的應用程序不能在這個變化的停頓,所以使用的onPause,的onStop等是不是一種選擇。

所以我要尋找類似onConfigurationChanged的事件還包括景觀從右到左的景觀變化。

回答

0

你可以做到以下幾點:

int result = this.getResources().getConfiguration().orientation; 
if (result == 1){ 
a//set content view to portrait 

} 
else{ 
//set content view to landscape} 
1

我試過OrientationEventListener。問題是它會觸發 加速。我正在做用戶界面變更,他們發生在 被重畫之前,它看起來很糟糕。

您可以使用handler.post(可運行),這樣,你的用戶界面的變化shouldn`t重繪之前發生,但它繪製完成後,將在主線程中進行排隊和執行。

handler.post(new Runnable() { 

     @Override 
     public void run() { 
     //do the UI changes you want 

     } 
    }); 

如果仍然不`噸助陣,你可以嘗試一個艱難和醜陋的方式利用定位監聽器和onLayout()方法:)

0

我想你幾乎得到它的。我不知道你是否做過類似的事情,但是這個讓你一旦改變方向就可以改變UI。我做了這個例子只是從ROTATION_270ROTATION_90,但你可以添加到代碼,以便我可以涵蓋從PORTRAITREVERSE_PORTRAIT等所有案例... 我的例子是簡單地將文本視圖從「景觀」更改爲「反向景觀「當方向改變時。

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 

</RelativeLayout> 

代碼

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Display; 
import android.view.OrientationEventListener; 
import android.view.Surface; 
import android.widget.TextView; 

public class LandScapeToLandScapeReverse extends Activity 
{ 
    private int mCurrentOrientation; 
    private Display mDisplay; 
    private OrientationEventListener mOrientationEventListener; 

    private TextView mTextView; 

    private static final String TAG = "LandScape To LandScape Reverse"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     Log.d(TAG, "onCreate"); 

     setContentView(R.layout.activity_main); 
     mTextView = (TextView) findViewById(R.id.textview); 

     mDisplay = getWindowManager().getDefaultDisplay(); 
     mCurrentOrientation = mDisplay.getRotation(); 
     mTextView.setText("CurrentOrientation: " + mCurrentOrientation); 

     mOrientationEventListener = new OrientationEventListener(this) 
     { 
      @Override 
      public void onOrientationChanged(int orientation) 
      { 
       // On my phone it seem to change at 233 maybe should start lower 
       // to take into account of phones model variations. 
       if (mCurrentOrientation == Surface.ROTATION_270 && orientation > 230) 
       { 
        mTextView.setText("CurrentOrientation: " + orientation); 
        Log.d(TAG, "orientation = " + orientation 
         + " getRotation: " + mDisplay.getRotation()); 
        if (mDisplay.getRotation() != mCurrentOrientation) 
        { 
         // I think one should disable listener here and enable 
         // again after doing all the UI changes. 
         mOrientationEventListener.disable(); 
         mTextView.setText("Landscape reverse"); 
        } 
       } 
      } 
     }; 

     mOrientationEventListener.enable(); 
    } 

    @Override 
    protected void onDestroy() 
    { 
     super.onDestroy(); 

     Log.d(TAG, "onDestroy"); 

     mOrientationEventListener.disable(); 
    } 
} 

我有一些想法,也許還有一個是方向變化,我們可以覆蓋之前調用的函數。我會玩,如果我找到一個我會更新答案。

+0

我已經試過了。問題在於,在設備旋轉之後,文本框會立即更新,大約在佈局旋轉前500毫秒。 – 2013-03-24 10:43:24

+0

我想我的textview是如此簡單,只要它更新屏幕翻轉,所以我不知道文本已經更新。 – 2013-03-24 10:49:49