Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 347c4ffa authored by jinwu's avatar jinwu
Browse files

Fix the issue of rotation in 3D activity

The image is fit by width, this will cause the image is beyond the
GLView when in landscape. So relayout the GLView after rotation.

Change-Id: Id056b698e9873e9d56feb8f217a4a42cf7372e69
CRs-Fixed: 2093443
parent d1e64f40
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -176,7 +176,11 @@ public class GLView extends GLSurfaceView {
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed && mListener != null) {
            // if the loyout is changed by rotation not by reLayoutGLView,
            // then save width and heigh of the view for caculating.
            if (left == 0 && top == 0) {
                mListener.onLayout(right - left, bottom - top);
            }
        }
    }
}
+30 −1
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ package com.android.gallery3d.app.dualcam3d;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
@@ -50,6 +51,10 @@ public class ThreeDimensionalActivity extends Activity {
    private GLView mImageView;
    private Controller mController;
    private LoadImageTask mTask;
    private int mWidth, mHeight;
    private int mOrientation;
    private Bitmap mBitmap = null;
    private boolean mOriChanged = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -64,6 +69,10 @@ public class ThreeDimensionalActivity extends Activity {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mOrientation != newConfig.orientation) {
            mOriChanged = true;
            mOrientation = newConfig.orientation;
        }
    }


@@ -81,7 +90,11 @@ public class ThreeDimensionalActivity extends Activity {
            public void onClick(float x, float y) {}

            @Override
            public void onLayout(int width, int height) {}
            public void onLayout(int width, int height) {
                mWidth = width;
                mHeight = height;
                reLayoutGLView(false);
            }
        });
    }

@@ -103,6 +116,7 @@ public class ThreeDimensionalActivity extends Activity {
    protected void onDestroy() {
        mTask.cancel(true);
        mTask = null;
        mBitmap = null;
        mImageView.recycle();
        super.onDestroy();
    }
@@ -118,6 +132,19 @@ public class ThreeDimensionalActivity extends Activity {
        mTask.execute(uri);
    }

    private void reLayoutGLView(boolean force) {
        if (!mOriChanged && !force) return;
        mOriChanged = false;
        if (mBitmap == null) return;
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        if (mWidth*height/width > mHeight) {
            int scaledWidth = width*mHeight/height;
            int move = (mWidth - scaledWidth)/2;
            mImageView.layout(move, 0, mWidth-move, mHeight);
        }
    }

    private class LoadImageTask extends AsyncTask<Uri, Void, GDepth.Image> {
        @Override
        protected GDepth.Image doInBackground(Uri... params) {
@@ -143,6 +170,8 @@ public class ThreeDimensionalActivity extends Activity {
                if (image != null && image.valid()) {
                    mImageView.setImageBitmap(image.bitmap);
                    mImageView.setDepthMap(image.depthMap);
                    mBitmap = image.bitmap;
                    reLayoutGLView(true);
                } else {
                    finish();
                }
+1 −13
Original line number Diff line number Diff line
@@ -93,23 +93,11 @@ public class Renderer implements GLSurfaceView.Renderer {
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.d("TAG", "onSurfaceChanged:" + width + "x" + height);
        float ratio = (float) width / height;
        if (width > height) {
            ratio = (float) height / width;
        }

        float sinY = (float) Math.sin(Math.toRadians(Settings.FIELD_OF_VIEW / 2)) / ratio;
        float cosY = (float) Math.cos(Math.toRadians(Settings.FIELD_OF_VIEW / 2));
        float fovY = (float) Math.toDegrees(Math.atan2(sinY, cosY) * 2.f);
        Matrix.perspectiveM(mProjectionMatrix, 0, fovY, ratio, 0.1f, 500.f);
        if (width > height) {
            // Caculate the width after rotating 90 or 270.
            int scaledWidth = height * height / width;
            // Caculate the offset in X level
            int offsetX = width/2 - scaledWidth/2;
            GLES20.glViewport(offsetX, 0, scaledWidth, height);
        } else {
        GLES20.glViewport(0, 0, width, height);
        }
        mSurfaceRect = new RectF(0, 0, width, height);
        setImageInvertMatrix();
    }