px dp density



The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given byxdpi andydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

在大约dpi是160的屏幕上,1 dpi= 1px ,所以 density = dpi/160


用DisplayMetrics 获取得到的一些值

		DisplayMetrics m = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(m);
		
		float density = m.density;  //1.5 
		int dDpi = m.densityDpi; //The screen density expressed as dots-per-inch.
		int hpx = m.heightPixels;//The absolute height of the display in pixels. 
		int wpx = m.widthPixels;//
		float sd = m.scaledDensity;//A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size. 
		float xdpi = m.xdpi; //The exact physical pixels per inch of the screen in the X dimension. 
		float ydpi = m.ydpi;
 联想 a750 测试结果

08-06 12:23:39.710: I/WhatEverTest(4395): density:1.5
08-06 12:23:39.710: I/WhatEverTest(4395): dDpi:240
08-06 12:23:39.710: I/WhatEverTest(4395): hpx:800
08-06 12:23:39.710: I/WhatEverTest(4395): wpx:480
08-06 12:23:39.710: I/WhatEverTest(4395): sd:1.5
08-06 12:23:39.710: I/WhatEverTest(4395): xdpi:240.0
08-06 12:23:39.710: I/WhatEverTest(4395): ydpi:240.0


奇怪:在上面那个a750的机器上加载icon ic_launcher,在onCreate方法中调用ViewTreeObsever的addOnPreDrawListener,显示的宽高的却是每次去取drawable-mdpi下的图片,按理说,如上面测试显示的a750的dpi=240,density=1.5,应该加载drawable-hdpi下的才对呀! why?。

下面是获取宽高的方法:


	
		setContentView(R.layout.test);
		final ImageView iv = (ImageView) findViewById(R.id.iv);
		ViewTreeObserver observer = iv.getViewTreeObserver();
		observer.addOnPreDrawListener(new OnPreDrawListener() {
			
			@Override
			public boolean onPreDraw() {
				if(!hasMeasure){
					Log.i(TAG,"iv width:"+iv.getMeasuredWidth()+",iv height:"+iv.getMeasuredHeight());
					hasMeasure = true;
				}
				return true;
			}
		});
		


一种测试系统加载图片顺序的方法:

先在所有drawable-(xxhdpi,xhdpi,hdpi,mdpi,ldpi,nodpi)下放上相同分辨率的图片,在布局文件中让其wrap-content,在代码中取一下其宽高,看下取的是哪个文件夹下 图片,如果取的是mdpi的,则把mdpi下的图片删掉再测,看接下来取哪个文件夹下的,依次就可以看出取的顺序。在所有drawable里放72*72的图片,加载顺序是mdpi-hdpi-xhdpi-ldpi-xxhdpi-nodpi

会先到对应dpi文件夹下的找,找不到会往上比自己dpi高的文件找,然后才会往比自己dpi低的下面找(一般概括,不一定在任何情况下都正确,比如上面提到的)


另外,最终看到一张图片显示的大小和当前设备的密度图片本身大小,还有布局文件对显示的设定有关。


The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given byxdpi andydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

在大约dpi是160的屏幕上,1 dpi= 1px ,所以 density = dpi/160


用DisplayMetrics 获取得到的一些值

		DisplayMetrics m = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(m);
		
		float density = m.density;  //1.5 
		int dDpi = m.densityDpi; //The screen density expressed as dots-per-inch.
		int hpx = m.heightPixels;//The absolute height of the display in pixels. 
		int wpx = m.widthPixels;//
		float sd = m.scaledDensity;//A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size. 
		float xdpi = m.xdpi; //The exact physical pixels per inch of the screen in the X dimension. 
		float ydpi = m.ydpi;
 联想 a750 测试结果

08-06 12:23:39.710: I/WhatEverTest(4395): density:1.5
08-06 12:23:39.710: I/WhatEverTest(4395): dDpi:240
08-06 12:23:39.710: I/WhatEverTest(4395): hpx:800
08-06 12:23:39.710: I/WhatEverTest(4395): wpx:480
08-06 12:23:39.710: I/WhatEverTest(4395): sd:1.5
08-06 12:23:39.710: I/WhatEverTest(4395): xdpi:240.0
08-06 12:23:39.710: I/WhatEverTest(4395): ydpi:240.0


奇怪:在上面那个a750的机器上加载icon ic_launcher,在onCreate方法中调用ViewTreeObsever的addOnPreDrawListener,显示的宽高的却是每次去取drawable-mdpi下的图片,按理说,如上面测试显示的a750的dpi=240,density=1.5,应该加载drawable-hdpi下的才对呀! why?。

下面是获取宽高的方法:


	
		setContentView(R.layout.test);
		final ImageView iv = (ImageView) findViewById(R.id.iv);
		ViewTreeObserver observer = iv.getViewTreeObserver();
		observer.addOnPreDrawListener(new OnPreDrawListener() {
			
			@Override
			public boolean onPreDraw() {
				if(!hasMeasure){
					Log.i(TAG,"iv width:"+iv.getMeasuredWidth()+",iv height:"+iv.getMeasuredHeight());
					hasMeasure = true;
				}
				return true;
			}
		});
		


一种测试系统加载图片顺序的方法:

先在所有drawable-(xxhdpi,xhdpi,hdpi,mdpi,ldpi,nodpi)下放上相同分辨率的图片,在布局文件中让其wrap-content,在代码中取一下其宽高,看下取的是哪个文件夹下 图片,如果取的是mdpi的,则把mdpi下的图片删掉再测,看接下来取哪个文件夹下的,依次就可以看出取的顺序。在所有drawable里放72*72的图片,加载顺序是mdpi-hdpi-xhdpi-ldpi-xxhdpi-nodpi

会先到对应dpi文件夹下的找,找不到会往上比自己dpi高的文件找,然后才会往比自己dpi低的下面找(一般概括,不一定在任何情况下都正确,比如上面提到的)


另外,最终看到一张图片显示的大小和当前设备的密度图片本身大小,还有布局文件对显示的设定有关。

你可能感兴趣的:(px dp density)