pingpongboss / StandOut

StandOut lets you easily create floating windows in your Android app.
http://pingpongboss.github.com/StandOut
MIT License
1.24k stars 379 forks source link

Window displayWidth and displayHeight invalid on device rotate #13

Open pingpongboss opened 11 years ago

pingpongboss commented 11 years ago

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 commented 11 years ago

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

dud3ski commented 11 years ago

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 commented 10 years ago

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

viju85 commented 10 years ago

Never mind. found another way to fix this.

yikhinzaw commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

That is what I did as well. On Oct 23, 2013 12:58 AM, "yikhinzaw" notifications@github.com 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 .

aldolo69 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

SjoerdvGestel commented 10 years ago

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

}