没有获得设备的尺寸

时间:2014-02-14 09:01:13

标签: android android-videoview

我正在使用此代码,视图不适合设备的整个屏幕。

    DisplayMetrics metricsLandscape = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metricsLandscape);              
    relVideoHolder.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
    videoView.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
    Log.e("DIMENSIONS", "WIDTH by HEIGHT"+ metricsLandscape.widthPixels+"by"+metricsLandscape.heightPixels);
    Log.e("VIDEOHOLDER dimension WxH", ""+relVideoHolder.getWidth()+"x"+relVideoHolder.getHeight());
    Log.e("VIDEOVIEW dimension WxH", ""+videoView.getWidth()+"x"+videoView.getHeight());

我发现这很奇怪,因为Logcat的结果就在这里;

02-14 17:17:25.881: E/DIMENSIONS(6314): WIDTH by HEIGHT 1196by720
02-14 17:17:25.881: E/VIDEOHOLDER dimension WxH(6314): 720x410
02-14 17:17:25.881: E/VIDEOVIEW dimension WxH(6314): 720x403

我想知道为什么他们有不同的价值观。有时,视图覆盖了所有屏幕,但大多数情况下,它不适合全屏。 请帮我。 顺便说一下,当横向定向时,我将这些代码调用在ConfigurationConhanged上。谢谢。

3 个答案:

答案 0 :(得分:2)

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

如果您不在活动中,可以通过WINDOW_SERVICE获取默认显示:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:

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

答案 1 :(得分:0)

我只是将getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);添加到我的代码中,它就像一个魅力。

答案 2 :(得分:-1)

我认为这可以帮助你!!!

DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dimension);
int w = dimension.widthPixels;
int h = dimension.heightPixels;