Android landscape right to landscape left event

岁酱吖の 提交于 2019-12-02 08:08:56

I tried the OrientationEventListener. The problem is that it triggers to fast. I am doing UI changes and they happen before the landscape is being redrawn, and it looks bad.

You can use handler.post(runnable), that way your UI changes shouldn`t happen before the redrawn, but will be queued on the main thread and executed after it finishes drawing.

handler.post(new Runnable() {

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

        }
    });

If this still doesn`t help out you can try a difficult and ugly way to make use of orientation listener and the onLayout() method :)

You can do the following:

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

}
else{
//set content view to landscape}

I think you almost get it. I do not know if you do similar thing, but this one let you doing UI change as soon as the orientation change. I did this example for only changing from ROTATION_270 to ROTATION_90, but you can add to the code so that I can cover all cases from PORTRAIT to REVERSE_PORTRAIT etc... My example is simply changing a textview from "Landscape" to "reverse landscape" when orientation changes.

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>  

Code

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

I got some idea maybe there is a function that is called before the orientation change that we can override. I will play around and if I find one I will update the answer.

You can use DisplayListener which gets triggered in this case, as described in this answer: https://stackoverflow.com/a/23612548/2942294

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!