2

How do I get the window height when I'm inside a surface view

This is my layout: (The surface view has already the LinearLayout "camera_preview" passed to it.

Now the question is how do I get the Window layout, which is identical to the FrameLayout ?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:id="@+id/camera_preview">            
        </LinearLayout>


        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"        
        android:background="#999999"
        android:gravity="top|center_horizontal"
        android:id="@+id/top_cover">            
        </LinearLayout>


        <LinearLayout
            android:background="#999999"    
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_marginRight="0dp"
            android:layout_marginTop="0dp"
            android:padding="10dp"
            android:id="@+id/bottom_cover"
            android:gravity="center">
            <Button
                android:id="@+id/button_capture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Capture" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>
4

1 回答 1

3

You can get the screen dimensions via:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

and then do (for API level 13 and up):

Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

On previous levels, these deprecated code was used:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Source: Get screen dimensions in pixels

于 2012-11-15T13:48:06.750 回答