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