Android display.getWidth / height为设备返回错误的大小?

时间:2011-05-19 12:38:52

标签: android

我有一个小班,需要跟踪设备的宽度和高度。

public class BrScreenMetrics {
public static int screenX=0;
public static int screenY=0;

public static void setMetrics(Activity activity)
{ 

    WindowManager w = activity.getWindowManager();
    Display d = w.getDefaultDisplay(); 
    screenX = d.getWidth(); 
    screenY = d.getHeight(); 


}

}

每次新活动我都会撤消该行:

BrScreenMetrics.setMetrics(this);

更新旋转等值...这很有效,除了它返回320x522,而不是我想要的480x800。 我做错了什么?

轻微更新:

我使用此代码尝试使用androids自己的指标类:

    public static void setMetrics(Activity activity)
{ 
   DisplayMetrics metrics = new DisplayMetrics();
   activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);      
   screenX = metrics.widthPixels;
   screenY = metrics.heightPixels;

}

但它仍然是320x522 :(

2 个答案:

答案 0 :(得分:4)

感谢查看清单确实是正确的方式,我最终发现了我需要的东西 最低要求为4-6。

如果有人发现它有用或可以提供更好的方法,那么这是我的实用程序:

package se.brickit.foundation;
//Open comment block above for info.

import android.app.Activity;
import android.view.Display;
import android.view.WindowManager;
import android.webkit.WebSettings.ZoomDensity;

public class BrScreenMetrics {
    private static int screenWidthPX = 0;
    private static int screenHeightPX = 0;
    private static int densityDpi = 0;
    private static DisplayMetrics metrics;

    public static void setMetrics(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidthPX = metrics.widthPixels;
        screenHeightPX = metrics.heightPixels;
        densityDpi = metrics.densityDpi;

    }

    /**
     * @return the screenWidthPX
     */
    public static int getScreenWidthPX() {
        return screenWidthPX;
    }

    /**
     * @param screenWidthPX
     *            the screenWidthPX to set
     */
    public static void setScreenWidthPX(int screenWidthPX) {
        BrScreenMetrics.screenWidthPX = screenWidthPX;
    }

    /**
     * @return the screenHeightPX
     */
    public static int getScreenHeightPX() {
        return screenHeightPX;
    }

    /**
     * @param screenHeightPX
     *            the screenHeightPX to set
     */
    public static void setScreenHeightPX(int screenHeightPX) {
        BrScreenMetrics.screenHeightPX = screenHeightPX;
    }

    /**
     * @return the densityDpi
     */
    public static int getDensityDpi() {
        return densityDpi;
    }

    /**
     * @param densityDpi
     *            the densityDpi to set
     */
    public static void setDensityDpi(int densityDpi) {
        BrScreenMetrics.densityDpi = densityDpi;
    }

    /**
     * @return the metrics
     */
    public static DisplayMetrics getMetrics() {
        return metrics;
    }

    /**
     * @return the density
     */
    public static ZoomDensity getZoomDensity() {
        if (densityDpi < 140)
            return ZoomDensity.CLOSE;
        if (densityDpi < 210)
            return ZoomDensity.MEDIUM;
        return ZoomDensity.FAR;
    }

    public static int getStatusBarHeight() {
        ZoomDensity zd = getZoomDensity();

        if (zd == ZoomDensity.CLOSE)
            return 19;
        else if (zd == ZoomDensity.MEDIUM)
            return 25;
        else 
            return 38;

    }

}

答案 1 :(得分:0)

你用dp获得正确的屏幕尺寸,  如果您想获得以像素为单位的实际尺寸,请将其添加到AndroidManifest.xml

<supported-screens android:anyDensity="true" />

注意:这仅适用于Android 1.6 sdk或更高版本

希望有所帮助:)