Android系统版本以及屏幕相关参数的获取包括长宽,物理尺寸,px,dp,dpi,ppi等,

</pre><pre>

public class MainActivity extends ActionBarActivity{

    private int systemVersion;
    private  TextView  tv;  
    static  Context ctx = null ;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linearlayout);
//        float density = getResources().getDisplayMetrics().density;
        systemVersion = Build.VERSION.SDK_INT;
        System.out.println("系统版本:"+systemVersion);
        System.out.println("通过WindowManager获得屏幕的宽高:"+getScreenDeprecation());
        System.out.println("通过Point获得屏幕的宽高:"+getScreenSize());
        System.out.println("通过getSize的内部实现获得屏幕的宽高:"+getScreenByDisplay());
        System.out.println("通过getRealSize获得屏幕的宽高:"+getRealSize());
        System.out.println("屏幕的密度比值density:"+getDensity());
        System.out.println("屏幕的densityDpi:"+getDensityDpi());
        System.out.println("获取X轴Y轴的PPI:"+getPPI());
        System.out.println("获取X轴Y轴的长度(英寸):"+getxyInch());
        System.out.println("正确的获取对角线方法:"+getScreenSizeOfDevice2());
        System.out.println("错误的获取对角线方法:"+getScreenSizeOfDevice());
    }
    /**
     * 过时的方法获取屏幕的宽高
     * @param context
     * @return
     */
    @SuppressWarnings("deprecation")
	public String getScreenDeprecation(){
    	WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    	int screenWidth = wm.getDefaultDisplay().getWidth();
    	int screenHeight = wm.getDefaultDisplay().getHeight();
    	return screenWidth+"*"+screenHeight;
    }
    /**
     * 新方法获取宽高
     */
	@SuppressLint("NewApi")
	public String getScreenSize(){
		if(systemVersion>=13){
			WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
			Point outSize = new Point();
			wm.getDefaultDisplay().getSize(outSize);
			return outSize.toString();
		}else{
			//用低版本的API解决问题
			return "系统版本低于13";
		}
    }
	/**
	 * 通过Display.getSize(Point outSize)的
	 * 内部实现原理获取屏幕的宽高
	 * 该方法其实就是其内部的具体实现
	 */
	public String getScreenByDisplay(){
		int widthPixels = getResources().getDisplayMetrics().widthPixels;
		int heightPixels = getResources().getDisplayMetrics().heightPixels;
		return widthPixels+"*"+heightPixels;
	}
	/**
	 * getRealSize()
	 * 获取屏幕的完整尺寸,没有减去任何装饰等所占的空间,一般很少用?
	 * @return
	 */
	@SuppressLint("NewApi")
	public String getRealSize(){
		if(systemVersion >= 13){
			WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
			Point outSize = new Point();
			wm.getDefaultDisplay().getRealSize(outSize);
			int x = outSize.x;
			int y = outSize.y;
			return x+","+y;
		}else {
			return "当前系统版本低于13";
		}
	}
	/**
	 * 获取密度比值density
	 */
	public float getDensity(){
		float density = getResources().getDisplayMetrics().density;
		return density;
	}
	/**
	 * 获取densityDpi:每英寸可打印的点数
	 * 另外:对于DisplayMetrics有如下属性
	 * 
	 *     	public static final int DENSITY_XXXHIGH = 640;	
	 *		public static final int DENSITY_XXHIGH = 480;	对应文件夹drawable-xxhdpi
     *		public static final int DENSITY_XHIGH = 320;	对应文件夹drawable-xhdpi
     *		public static final int DENSITY_HIGH = 240;		对应文件夹drawable-hdpi
     *		public static final int DENSITY_MEDIUM = 160;	对应文件夹drawable-mdpi
     * 		public static final int DENSITY_LOW = 120;		对应文件夹drawable-ldpi
	 *
     *		public static final int DENSITY_TV = 213;
     *		public static final int DENSITY_400 = 400;
     *		public static final int DENSITY_560 = 560;
	 */
	public int getDensityDpi(){
		int densityDpi = getResources().getDisplayMetrics().densityDpi;
		return densityDpi;
	}
	/**
	 * 获取X、Y轴的PPI,即每英寸的像素数
	 */
	public String getPPI(){
		float xdpi = getResources().getDisplayMetrics().xdpi;
		float ydpi = getResources().getDisplayMetrics().ydpi;
		return xdpi+","+ydpi;
	}
	/**
	 * 获取x轴和y轴的长度(英寸)
	 */
	@SuppressLint("NewApi")
	public String getxyInch(){
		WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
		Point outSize = new Point();
		wm.getDefaultDisplay().getRealSize(outSize);
		float xdpi = getResources().getDisplayMetrics().xdpi;
		float ydpi = getResources().getDisplayMetrics().ydpi;
		float xInch = outSize.x/xdpi;
		float yInch = outSize.y/ydpi;
		return xInch +","+yInch;
	}
	/**
	 * px转换为dp
	 */
	public int pxTodp(int px){
		float density = getResources().getDisplayMetrics().density;
		int dp = (int) (px/density);
		return dp;
	}
	/**
	 * dp转换为px
	 */
	public int dpTopx(int dp){
		float density = getResources().getDisplayMetrics().density;
		int px = (int) (dp*density);
		return px;
	}
	@SuppressLint("NewApi")
	private String getScreenSizeOfDevice2() {  
	    Point point = new Point();  
	    getWindowManager().getDefaultDisplay().getRealSize(point);  
	    DisplayMetrics dm = getResources().getDisplayMetrics();
	    double x = Math.pow(point.x/ dm.xdpi, 2);  
	    double y = Math.pow(point.y / dm.ydpi, 2);  
	    double screenInches = Math.sqrt(x + y);  
	    return String.valueOf(screenInches);
	}  
	@SuppressLint("NewApi")
	private String getScreenSizeOfDevice() {  
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width=dm.widthPixels;
		int height=dm.heightPixels;
		int dens=dm.densityDpi;
		double wi=(double)width/(double)dens;
		double hi=(double)height/(double)dens;
		double x = Math.pow(wi,2);
		double y = Math.pow(hi,2);
		double screenInches = Math.sqrt(x+y); 
		return String.valueOf(screenInches);
	}  
}


另外android.util.TypedValue类提供了一个函数,支持把所有的单位换算到px,源码输入:

   /**
     * Converts an unpacked complex data value holding a dimension to its final floating 
     * point value. The two parameters <var>unit</var> and <var>value</var>
     * are as in {@link #TYPE_DIMENSION}.
     *  
     * @param unit The unit to convert from.
     * @param value The value to apply the unit to.
     * @param metrics Current display metrics to use in the conversion -- 
     *                supplies display density and scaling information.
     * 
     * @return The complex floating point value multiplied by the appropriate 
     * metrics depending on its unit. 
     */
    public static float applyDimension(int unit, float value,
                                       DisplayMetrics metrics)
    {
        switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值