StandOut icon indicating copy to clipboard operation
StandOut copied to clipboard

Window displayWidth and displayHeight invalid on device rotate

Open pingpongboss opened this issue 13 years ago • 11 comments

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.

pingpongboss avatar Feb 05 '13 07:02 pingpongboss

http://stackoverflow.com/questions/4625167/how-do-i-use-a-service-to-monitor-orientation-change-in-android

pingpongboss avatar Feb 26 '13 22:02 pingpongboss

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

dud3ski avatar May 20 '13 23:05 dud3ski

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

viju85 avatar Oct 17 '13 20:10 viju85

Never mind. found another way to fix this.

viju85 avatar Oct 17 '13 23:10 viju85

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 avatar Oct 18 '13 07:10 yikhinzaw

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.

yikhinzaw avatar Oct 23 '13 07:10 yikhinzaw

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/pingpongboss/StandOut/issues/13#issuecomment-26886700 .

viju85 avatar Oct 23 '13 14:10 viju85

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

aldolo69 avatar Nov 07 '13 04:11 aldolo69

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.

    }

yikhinzaw avatar Nov 07 '13 09:11 yikhinzaw

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

aldolo69 avatar Nov 07 '13 20:11 aldolo69

@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.

}

SjoerdvGestel avatar May 18 '14 16:05 SjoerdvGestel