如何从SurfaceView得到窗口的高度? [英] How to get window height from inside SurfaceView?
本文介绍了如何从SurfaceView得到窗口的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何得到窗口的高度,当我从表面上看里面
这是我的布局:(表面观早已在的LinearLayoutcamera_ preVIEW传递给它
现在的问题是如何获取的窗口布局,这是相同的的FrameLayout?
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向> <的FrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_gravity =中心
机器人:layout_weight =1> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:比重=中心
机器人:layout_gravity =中心
机器人:ID =@ + ID / camera_ preVIEW>
< / LinearLayout中>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =50dp
机器人:背景=#999999
机器人:重力=顶| CENTER_HORIZONTAL
机器人:ID =@ + ID / top_cover>
< / LinearLayout中>
<的LinearLayout
机器人:背景=#999999
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底部| CENTER_HORIZONTAL
机器人:layout_marginRight =0dp
机器人:layout_marginTop =0dp
机器人:填充=10dp
机器人:ID =@ + ID / bottom_cover
机器人:比重=中心>
<按钮
机器人:ID =@ + ID / button_capture
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:文字=拍照/>
< / LinearLayout中>
< /&的FrameLayout GT;< / LinearLayout中>
解决方案
您可以通过获取屏幕尺寸:
窗口管理WM =(窗口管理器)ctx.getSystemService(Context.WINDOW_SERVICE);
显示显示= wm.getDefaultDisplay();
和则(API为13级及以上)做的:
点大小=新点();
display.getSize(大小);
INT宽度= size.x;
INT高度= size.y;
在previous水平,用这些去precated code:
显示显示= getWindowManager()getDefaultDisplay()。
INT宽度= display.getWidth(); //德precated
INT高度= display.getHeight(); //德precated
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>
解决方案
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: Android: How to get screen dimensions
这篇关于如何从SurfaceView得到窗口的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文