Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window displayWidth and displayHeight invalid on device rotate #13

Open
pingpongboss opened this issue Feb 5, 2013 · 11 comments
Open

Comments

@pingpongboss
Copy link
Owner

Maybe caching the displayWidth and displayHeight isn't the way to go.
https://github.com/pingpongboss/StandOut/blob/master/library/src/wei/mark/standout/ui/Window.java#L80

Rotating the screen should also check the window size and bounds to make sure they fit in the screen if need be.

@dud3ski
Copy link

dud3ski commented May 20, 2013

here's my workaround for this (for classes extending StandOutWindow):

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        checkForEdges(111, null);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        checkForEdges(111, null);
    }
}

private StandOutLayoutParams checkForEdges(int id, Window window) {
    if (window == null) {
        window = getWindow(id);
    }
    Drawable d = getResources().getDrawable(R.drawable.ic_nos);
    int w = d.getIntrinsicWidth();
    int h = d.getIntrinsicHeight();
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;
    Log.i("A919Tool", "widthPixels: " + widthPixels + ", heightPixels: " + heightPixels);

    StandOutLayoutParams solp = new StandOutLayoutParams(id, w, h);
    if (window.getLayoutParams().x < 0) {
        solp.x = 0;
        solp.y = window.getLayoutParams().y;
        window.setLayoutParams(solp);
    }
    if (window.getLayoutParams().x > widthPixels-w) {
        solp.x = widthPixels-w;
        solp.y = window.getLayoutParams().y;
        window.setLayoutParams(solp);
    }
    if (window.getLayoutParams().x == heightPixels-w) { // like Gravity.RIGHT
        solp.x = widthPixels-w;
        solp.y = window.getLayoutParams().y;
        window.setLayoutParams(solp);
    }
    if (window.getLayoutParams().y < 0) {
        solp.y = 0;
        solp.x = window.getLayoutParams().x;
        window.setLayoutParams(solp);
    }
    if (window.getLayoutParams().y > heightPixels-h-getStatusBarHeight()) {
        solp.y = heightPixels-h-getStatusBarHeight();
        solp.x = window.getLayoutParams().x;
        window.setLayoutParams(solp);
    }

    if (window.getLayoutParams().y == widthPixels-h-getStatusBarHeight()) { //like Gravity.BOTTOM
        solp.y = heightPixels-h-getStatusBarHeight();
        solp.x = window.getLayoutParams().x;
        window.setLayoutParams(solp);
    }

saveX = window.getLayoutParams().x;
    saveY = window.getLayoutParams().y;
    window.edit().setPosition(saveX, saveY).commit();

    this.sPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    Editor edtr = this.sPreferences.edit();
    edtr.putInt("saveX", saveX);
    edtr.putInt("saveY", saveY);
    edtr.commit();
    return solp;
}

@viju85
Copy link

viju85 commented Oct 17, 2013

Does not seem to work. BTW, can we know what this drawable "ic_nos" refers to ?

@viju85
Copy link

viju85 commented Oct 17, 2013

Never mind. found another way to fix this.

@yikhinzaw
Copy link

I also find the way to rotate window. Currently window only allow for portrait orientation. I can capture orientation changing by overriding onConfigurationChanged method.But Xposition never change.

@yikhinzaw
Copy link

Now i can do screen rotation without doing any brocast receiver for orientation changing.I only update window.setPosition method. In that method , you need to define displaywidth and height to get latest width and height.

@viju85
Copy link

viju85 commented Oct 23, 2013

That is what I did as well.
On Oct 23, 2013 12:58 AM, "yikhinzaw" [email protected] wrote:

Now i can do screen rotation without doing any brocast receiver for
orientation changing.I only update window.setPosition method. In that
method , you need to define displaywidth and height to get latest width and
height.


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-26886700
.

@aldolo69
Copy link

aldolo69 commented Nov 7, 2013

i'm facing the same problem but still i've not found a solution by myself. rotating the screen do not update screen width/height so i can not wander a window around. someone have shared the code somewhere??? thanks

@yikhinzaw
Copy link

You need to update in Standout Library. In Library, there has setPosition method in winodw.java. You need to called Display Height and width in those method. as follows,

private Editor setPosition(int x, int y, boolean skip) {

        DisplayMetrics metrics = mContext.getResources()
                .getDisplayMetrics();
        displayHeight = (int) (metrics.heightPixels - 25 * metrics.density);
        displayHeight = metrics.heightPixels;

                                  //others are the same as original code.

    }

@aldolo69
Copy link

aldolo69 commented Nov 7, 2013

tankyou. i'vecopied it in getlayoutparams so w/h are always up to date

@SjoerdvGestel
Copy link

@yikhinzaw thanks, you code works if correct what i think is a mistake.
Booth lines are display height, not width. The code below makes it work as it should.

  DisplayMetrics metrics = mContext.getResources()
            .getDisplayMetrics();
    displayHeight = (int) (metrics.heightPixels - 25 * metrics.density);
    displayWidth = metrics.widthPixels;

                              //others are the same as original code.

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants