网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android获取设备宽高分辨率

想要获取设备宽高分辨率,最好放到Utils跑龙套里面。

成都创新互联云计算的互联网服务提供商,拥有超过13年的服务器租用、成都二枢机房、云服务器、网页空间、网站系统开发经验,已先后获得国家工业和信息化部颁发的互联网数据中心业务许可证。专业提供云主机、网页空间、域名申请、VPS主机、云服务器、香港云服务器、免备案服务器等。

基础知识:

  1. WindowManager

  • The interface that apps use to talk to the window manager.

  • Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

  • Each window manager instance is bound to a particular Display. To obtain a WindowManager for a different display, use createDisplayContext(Display) to obtain a Context for that display, then use Context.getSystemService(Context.WINDOW_SERVICE) to get the WindowManager.

  • The simplest way to show a window on another display is to create a Presentation.  The presentation will automatically obtain a WindowManager and Context for that display.

    整个Android的窗口机制是基于一个叫做 WindowManager,这个接口可以添加view到屏幕,也可以从屏幕删除view。它面向的对象一端是屏幕,另一端就是View,直接忽略我们以前的Activity或者Dialog之类的东东。其实我们的Activity或者Diolog底层的实现也是通过WindowManager,这个 WindowManager是全局的,整个系统就是这个唯一的东东。它是显示View的最底层了。 (引自http://gundumw100.iteye.com/blog/830235)

方法:

abstract Display

getDefaultDisplay()

Returns the Display upon which this WindowManager instance will create new windows.

abstract voidremoveViewImmediate(View view)

Special variation of removeView(View) that immediately invokes the given view hierarchy's View.onDetachedFromWindow() methods before returning.

2.Display

Provides information about the size and densityof a logical display.

The display area is described in two different ways.

  • The application display area specifies the part of the display that may contain an application window, excluding the system decorations.  The application display area may be smaller than the real display area because the system subtracts the space needed for decor elements such as the status bar.  Use the following methods to query the application display area: getSize(Point), getRectSize(Rect) and getMetrics(DisplayMetrics).

  • The real display area specifies the part of the display that contains content including the system decorations.  Even so, the real display area may be smaller than the physical size of the display if the window manager is emulating a smaller display using (adb shell am display-size).  Use the following methods to query the real display area: getRealSize(Point), getRealMetrics(DisplayMetrics).

A logical display does not necessarily represent a particular physical display device such as the built-in screen or an external monitor.  The contents of a logical display may be presented on one or more physical displays according to the devices that are currently attached and whether mirroring has been enabled.

下面给出代码:

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Utils {

	public static int getScreenWidth(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getWidth();
	}

	public static int getScreenHeight(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getHeight();
	}

	public static float getScreenDensity(Context context) {
		try {
			DisplayMetrics dm = new DisplayMetrics();
			WindowManager manager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);
			manager.getDefaultDisplay().getMetrics(dm);
			return dm.density;
		} catch (Exception ex) {

		}
		return 1.0f;
	}

}


文章标题:Android获取设备宽高分辨率
URL分享:http://bjjierui.cn/article/ghiooi.html

其他资讯