Android屏幕分辨率获取方法--源码剖析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!
在适配的过程中,有时我们会用到屏幕宽高,那么如何获得屏幕的分辨率?
方法有两种:
第一种是通过WindowManager接口获得Diaplay对象,通过Display对象来获得
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();DisplayMetrics outMetrics=new DisplayMetrics();
display.getMetrics(outMetrics);screenHeight = outMetrics.heightPixels;
screenWidth = outMetrics.widthPixels;
//screenHeight = display.getHeight();此种方法已废弃
//screenWidth = display.getWidth();此种方法已废弃或者
从源码上来看,拿height来说:
private final Point mTmpPoint = new Point();
public int getHeight() {
synchronized (mTmpPoint) {
long now = SystemClock.uptimeMillis();
if (now > (mLastGetTime+20)) {
getSizeInternal(mTmpPoint, true);
mLastGetTime = now;
}
return mTmpPoint.y;
}
}主要是找mTmpPoint 的y坐标
private void getSizeInternal(Point outSize, boolean doCompat) {
try {
IWindowManager wm = getWindowManager();
if (wm != null) {
wm.getDisplaySize(outSize);
CompatibilityInfo ci;
if (doCompat && (ci=mCompatibilityInfo.getIfNeeded()) != null) {
synchronized (mTmpMetrics) {
mTmpMetrics.noncompatWidthPixels = outSize.x;
mTmpMetrics.noncompatHeightPixels = outSize.y;
mTmpMetrics.density = mDensity;
ci.applyToDisplayMetrics(mTmpMetrics);
outSize.x = mTmpMetrics.widthPixels;
outSize.y = mTmpMetrics.heightPixels;
}
}
} else {
// This is just for boot-strapping, initializing the
// system process before the window manager is up.
outSize.x = getRawWidth();
outSize.y = getRawHeight();
}
if (false) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Slog.v(TAG, "Returning display size: " + outSize, here);
}
if (DEBUG_DISPLAY_SIZE && doCompat) Slog.v(
TAG, "Returning display size: " + outSize);
} catch (RemoteException e) {
Slog.w("Display", "Unable to get display size", e);
}
}然后我们发现通过CompatibilityInfo对象设置metrics是一种方法,另一种是getRawHeight()
public int getRawHeight() {
int h = getRawHeightNative();
if (DEBUG_DISPLAY_SIZE) Slog.v(
TAG, "Returning raw display height: " + h);
return h;
}
private native int getRawHeightNative();最终是一native方法,通过底层实现。
方法二:通过Resource对象来获得DisplayMetrics来取得
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;总体来说,都是要取得DisplayMetrics,那么我们来看看它的构成:
/**
* Standard quantized DPI for low-density screens.
*/
public static final int DENSITY_LOW = 120; /**
* Standard quantized DPI for medium-density screens.
*/
public static final int DENSITY_MEDIUM = 160; /**
* Standard quantized DPI for 720p TV screens. Applications should
* generally not worry about this density, instead targeting
* {@link #DENSITY_XHIGH} for 1080p TV screens. For situations where
* output is needed for a 720p screen, the UI elements can be scaled
* automatically by the platform.
*/
public static final int DENSITY_TV = 213; /**
* Standard quantized DPI for high-density screens.
*/
public static final int DENSITY_HIGH = 240; /**
* Standard quantized DPI for extra-high-density screens.
*/
public static final int DENSITY_XHIGH = 320;分别有默认的Density值
默认实现方法:
public void setToDefaults() {
widthPixels = 0;
heightPixels = 0;
density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
densityDpi = DENSITY_DEVICE;
scaledDensity = density;
xdpi = DENSITY_DEVICE;
ydpi = DENSITY_DEVICE;
noncompatWidthPixels = 0;
noncompatHeightPixels = 0;
}一般都要实现下面方面,来获得想要的值
public void setTo(DisplayMetrics o) {
widthPixels = o.widthPixels;
heightPixels = o.heightPixels;
density = o.density;
densityDpi = o.densityDpi;
scaledDensity = o.scaledDensity;
xdpi = o.xdpi;
ydpi = o.ydpi;
noncompatWidthPixels = o.noncompatWidthPixels;
noncompatHeightPixels = o.noncompatHeightPixels;
noncompatDensity = o.noncompatDensity;
noncompatScaledDensity = o.noncompatScaledDensity;
noncompatXdpi = o.noncompatXdpi;
noncompatYdpi = o.noncompatYdpi;
}看下Resource类
public Resources(AssetManager assets, DisplayMetrics metrics,
Configuration config, CompatibilityInfo compInfo) {
mAssets = assets;
mMetrics.setToDefaults();
mCompatibilityInfo = compInfo;
updateConfiguration(config, metrics);
assets.ensureStringBlocks();
}看到updateConfiguration(config, metrics);方法
/**
* Store the newly updated configuration.
*/
public void updateConfiguration(Configuration config,
DisplayMetrics metrics) {
updateConfiguration(config, metrics, null);
}/**
* @hide
*/
public void updateConfiguration(Configuration config,
DisplayMetrics metrics, CompatibilityInfo compat) {
synchronized (mTmpValue) {
if (false) {
Slog.i(TAG, "**** Updating config of " + this + ": old config is "
+ mConfiguration + " old compat is " + mCompatibilityInfo);
Slog.i(TAG, "**** Updating config of " + this + ": new config is "
+ config + " new compat is " + compat);
}
if (compat != null) {
mCompatibilityInfo = compat;
}
if (metrics != null) {
mMetrics.setTo(metrics);
}我们会看到这样的结果,所以在形成Resources的时候,metrics已经写入。
可是刚才我们用的是this.getResouces直接来获得的Resource对象ContextThemeWrapper:
@Override
public Resources getResources() {
if (mResources != null) {
return mResources;
}
if (mOverrideConfiguration == null) {
mResources = super.getResources();
return mResources;
} else {
Context resc = createConfigurationContext(mOverrideConfiguration);
mResources = resc.getResources();
return mResources;
}
}主要是在Context里实现,具体怎么实现,估计还是要看native的代码才知道。
屏幕分辨率的问题就先介绍到这儿。
Android屏幕分辨率获取方法--源码剖析的更多相关文章
- java线程基础巩固---线程生命周期以及start方法源码剖析
上篇中介绍了如何启动一个线程,通过调用start()方法才能创建并使用新线程,并且这个start()是非阻塞的,调用之后立马就返回的,实际上它是线程生命周期环节中的一种,所以这里阐述一下线程的一个完整 ...
- Android屏幕亮度调节相关源码
如下代码内容是关于Android屏幕亮度调节相关的代码. public static boolean isAutoBrightness(ContentResolver aContentResolver ...
- 并发编程之 ConcurrentLinkedQueue 源码剖析
前言 今天我们继续分析 java 并发包的源码,今天的主角是谁呢?ConcurrentLinkedQueue,上次我们分析了并发下 ArrayList 的替代 CopyOnWriteArrayList ...
- 并发编程之 ThreadLocal 源码剖析
前言 首先看看 JDK 文档的描述: 该类提供了线程局部 (thread-local) 变量.这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局 ...
- Android源码剖析之Framework层升级版(窗口、系统启动)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 看本篇文章之前,建议先查看: Android源码剖析之Framework层基础版 前面讲了frame ...
- 老李推荐:第5章5节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 获取系统服务引用
老李推荐:第5章5节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 获取系统服务引用 上一节我们描述了monkey的命令处理入口函数run是如何调用optionP ...
- 玩转Android之Picasso使用详详详详详详解,从入门到源码剖析!!!!
Picasso是Squareup公司出的一款图片加载框架,能够解决我们在Android开发中加载图片时遇到的诸多问题,比如OOM,图片错位等,问题主要集中在加载图片列表时,因为单张图片加载谁都会写.如 ...
- 老李推荐:第6章2节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-获取命令字串
老李推荐:第6章2节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-获取命令字串 从上一节的描述可以知道,MonkeyRunner发送给Monkey的命令 ...
- 老李推荐:第5章7节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles
老李推荐:第5章7节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles poptest是国内唯一一家培养测试开 ...
随机推荐
- jquery validate验证规则重用
当多个控件验证规则相同时,如何避免冗余代码并应用相同规则呢? [1st way. addMethod+addClassRules] 场景:维护学生档案时需要维护父母.监护人.紧急联系人的身份证号码,此 ...
- ConEmu windows上的终端工具
ConEmu Windows terminal 官网: http://conemu.github.io/
- hdu 6363 bookshelf
题解讲的很清楚了,直接看代码就懂了 题解:http://bestcoder.hdu.edu.cn/blog/2018-multi-university-training-contest-6-solut ...
- Unity C# 设计模式(三)工厂方法模式
定义: 定义一个创建对象的接口(父类),由子类决定需要实例化哪一个类. 这样,核心工厂类成为了一个抽象角色,不再负责产品的创建,仅提供具体工厂类所必须实现的接口,这样进一步抽象化的好处是使得工厂方法模 ...
- 【BZOJ 1047】[HAOI2007]理想的正方形
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二维的ST表. 每个大的正方形可以由4个小的正方形组成. 然后区域内的最大值最小值.也可以由4个小的张方形部分全部覆盖到. [代码] ...
- 一个最不可思议的MySQL死锁分析
1 死锁问题背景 1 1.1 一个不可思议的死锁 1 1.1.1 初步分析 3 1.2 如何阅读死锁日志 3 2 死锁原因深入剖析 4 2. ...
- python里面 __future__的作用 & 下划线的作用 & 3.0实现不换行
参考这篇文章: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820 ...
- BNU 34974 MATLAB大法好
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=34974 MATLAB大法好 Time Limit: 8000ms Memory Limi ...
- CentOS用rpm升级glibc
CentOS用rpm升级glibc #! /bin/sh # update glibc to 2.23 for CentOS 6 wget http://cbs.centos.org/kojifile ...
- React开发实时聊天招聘工具 -第一章
第一章 课程道学 6个页面 弱化css Antd-mobile作为组件库 Redux 状态管理 React-Router 路由 Axios异步请求 后端Express框架 Socket.io 数据库: ...