Thursday, 3 September 2020

Android: Activity orientation is not always changed after screen rotation

I have a problem with a simple android application. It has a SurfaceView with simple drawing, but activity orientation sometimes seems not to be changed after a screen rotation.

This is how an activity looks in the portrait mode:

landscape mode: enter image description here

but sometimes, when I rotate the screen, an activity looks like this in the portrait mode: enter image description here

MainActivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback
{
    private Thread mGameThread;
    private GameApp mGameApp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SurfaceView mainView = (SurfaceView)findViewById(R.id.mainView);
        SurfaceHolder holder = mainView.getHolder();
        holder.addCallback(this);

        mGameApp = new GameApp(getResources(), holder);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        mGameThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                mGameApp.run();
            }
        });
        mGameThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        mGameApp.setSurfaceSize(width, height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        boolean retry = true;
        mGameApp.setRunning(false);
        while (retry)
        {
            try
            {
                mGameThread.join();
                retry = false;
            }
            catch (InterruptedException e)
            {
            }
        }
    }
}

GameApp.java:

package com.example.myapplication;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.SurfaceHolder;

public class GameApp
{
    private Resources mResources;
    private SurfaceHolder mSurfaceHolder;
    private int mCanvasHeight = 1;
    private int mCanvasWidth = 1;
    private volatile boolean mRun = false;

    public GameApp(Resources resources, SurfaceHolder surfaceHolder)
    {
        mResources = resources;
        mSurfaceHolder = surfaceHolder;
    }

    public void setSurfaceSize(int width, int height)
    {
        synchronized (mSurfaceHolder)
        {
            mCanvasWidth = width;
            mCanvasHeight = height;
        }
    }

    public void run()
    {
        setRunning(true);

        while (mRun)
        {
            Canvas canvas = null;
            try
            {
                canvas = mSurfaceHolder.lockCanvas(null);

                synchronized (mSurfaceHolder)
                {
                    if (mRun && canvas != null)
                    {
                        draw(canvas);
                    }
                }
            }
            finally
            {
                if (canvas != null)
                {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    public void setRunning(boolean b)
    {
        mRun = b;
    }

    private void draw(Canvas canvas)
    {
        canvas.drawColor(Color.GREEN);

        Drawable cellImage = mResources.getDrawable(R.drawable.cell);

        final float cellWidth = mCanvasWidth / 6;
        final float cellHeight = mCanvasHeight / 6;
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                float x = i * cellWidth;
                float y = j * cellHeight;

                cellImage.setBounds(Math.round(x), Math.round(y), Math.round(x + cellWidth), Math.round(y + cellHeight));
                cellImage.draw(canvas);
            }
        }
    }
}


from Android: Activity orientation is not always changed after screen rotation

No comments:

Post a Comment