Getting the size of the window WITHOUT title/notification bars

后端 未结 5 806
傲寒
傲寒 2021-02-07 04:19

I\'ve been playing around with Android development and one of the things I\'d like to be able to do is dynamically create a background image for my windows, similar to the one b

相关标签:
5条回答
  • 2021-02-07 04:37

    Use View.MeasureSpec.getSize method in onMeasure override.

        @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        ...
    }
    
    0 讨论(0)
  • 2021-02-07 04:41

    To accomplish this in my app, I had to find a View's dimensions within the main Activity. It looked something like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        gestureScanner = new GestureDetector(this);   //Object for handling gestures.
    
        mvView = new MyView(this);  //MyView is class that extends View.
        setContentView(myView);
    }
    
    @Override
    public void onShowPress(MotionEvent e) {
    
        //Returns the size of the entire window, including status bar and title.
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    
        //Try to find the dimension of the view without the status/title bars.
        int iViewHeight = mvMountain.getHeight();
        int iViewWidth = mvMountain.getWidth();
    
    
        Toast toast = Toast.makeText(this," View:" + iViewWidth + ","+iViewHeight + " Window: " + dm.widthPixels + " by " + dm.heightPixels, 2);
        toast.show();
    }
    
    0 讨论(0)
  • 2021-02-07 04:44

    I have a tutorial on my blog that give you the ability to save your screen size on launch, You can read it here: http://evgeni-shafran.blogspot.com/2011/01/android-screen-size-problem.html

    Basicly you need to overide an onMeasure method of the first layout that hold you full screen, and get the width and height from there

    0 讨论(0)
  • 2021-02-07 04:52

    in the first onDraw, call getWidth, getHeight. these will not be valid before then.

    if you are not using a custom view/layout then you can instead call getWidth/getHeight during the first onWindowFocusChanged

    0 讨论(0)
  • 2021-02-07 04:57

    have a look at getwidth() and getheight() maybe? that should give you the size of the screen. though, I don't know If it takes the bars or not. But I don't think so...

    0 讨论(0)
提交回复
热议问题