0

横向きの右向きから横向きの左向きに直接切り替えるイベントのために、レイアウトが再描画されているときにイベントをトリガーする方法があれば、今すぐしたいと思います。

OrientationEventListener を試してみました。問題は、それが速くなるということです。UI の変更を行っていますが、ランドスケープが再描画される前に変更が行われ、見栄えが悪くなります。

変更に多少の遅延を追加することもできますが、電話によっては、変更が多かれ少なかれ続く可能性があります.

onConfigurationChanged の場合、タイミングは完璧ですが、もちろん、縦向きから横向き、または横向きから縦向きの場合にのみ機能します。

私のアプリケーションはこの変更で一時停止してはならないため、onPause、onStop などを使用することはできません。

だから私は onConfigurationChanged に似たイベントを探しています。これは右から左への変更もカバーします。

4

4 に答える 4

1

OrientationEventListener を試してみました。問題は、それが速くなるということです。UI の変更を行っていますが、ランドスケープが再描画される前に変更が行われ、見栄えが悪くなります。

handler.post(runnable) を使用すると、再描画の前に UI の変更が発生することはありませんが、メイン スレッドでキューに入れられ、描画が終了した後に実行されます。

handler.post(new Runnable() {

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

        }
    });

それでも解決しない場合は、向きリスナーと onLayout() メソッドを利用するための難しくて醜い方法を試すことができます:)

于 2013-03-23T11:23:54.440 に答える
0

私はあなたがほとんどそれを得ると思います。同様のことをしているかどうかはわかりませんが、これは、向きが変わるとすぐに UI の変更を行うことができます。この例では、 からROTATION_270への変更のみを行いましたが、 からへROTATION_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();
    }
}  

オーバーライドできる方向変更の前に呼び出される関数があるのではないかと思いました。遊んでみて、見つけたら答えを更新します。

于 2013-03-24T06:18:35.440 に答える
-1

次のことができます。

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

}
else{
//set content view to landscape}
于 2013-03-23T10:49:32.420 に答える