解决android4.0 触摸屏分辨率映射不准

2024-05-13 19:32

本文主要是介绍解决android4.0 触摸屏分辨率映射不准,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言 

  android4.0,在触摸屏这块一直有个令人头疼的问题,通常触摸屏的分辨率应该是根据当前屏幕分辨率而定。

但android4.0上就有点奇怪,不管怎么换屏幕的分辨率,触摸屏始终有不准的情况。最近着手研究了这块,通过修改inputreader.cpp 

及 com_android_server_InputManager.cpp终于解决的触摸屏不准的问题。


正文

  由于驱动是直接从android2.3的linux内核中移植过来,而android2.3上又可以正常使用且没有任何区域触摸无反应的情况,因此可以

排除linux-->android层之间的接口问题。


分析

  触摸屏属于输入设备,而且在整个输入过程中肯定是有坐标转换的,否则系统是无法获取触摸屏的准确点击位置。而在android中输入

事件可以到input相关代码中查找。


源码追踪

  仔细分析了下framework代码,可以发现framework//base/services/input/InputReader.cpp 中对触摸事件设置了一个分辨率映射值:

[cpp]  view plain copy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.     if (displayId == 0) {  
  4.         DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;  
  5.         info.width = width;  
  6.         info.height = height;  
  7.         info.orientation = orientation;  
  8.     }  
  9. }  

此处DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;说明该函数可能有两次调用,用于设置两种情况的输入设备。


而附近又对应了获取显示器信息函数

[cpp]  view plain copy
  1. bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t* width, int32_t* height, int32_t* orientation) const {  
  3.   
  4. }  

并且在该文件中有多处调用了该函数,其中以下位置比较可疑

[cpp]  view plain copy
  1. void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {  
  2.     int32_t oldDeviceMode = mDeviceMode;  
  3. ......  
  4.  // Get associated display dimensions.  
  5.     if (mParameters.associatedDisplayId >= 0) {  
  6.         if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,  
  7.                 mParameters.associatedDisplayIsExternal,  
  8.                 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,  
  9.                 &mAssociatedDisplayOrientation)) {  
  10.             LOGI(INDENT "Touch device '%s' could not query the properties of its associated "  
  11.                     "display %d.  The device will be inoperable until the display size "  
  12.                     "becomes available.",  
  13.                     getDeviceName().string(), mParameters.associatedDisplayId);  
  14.             //这块打印消息直接暴露了 触摸屏设备获取屏幕分辨率的踪迹,看来触摸屏确实是通过获取  
  15.             //当前屏幕分辨率来映射坐标值的  
  16.             mDeviceMode = DEVICE_MODE_DISABLED;  
  17.             return;  
  18.         }  
  19.     }  
  20.   
  21.     // Configure dimensions.  
  22.     int32_t width, height, orientation;  
  23.       
  24.     if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {  
  25.         width = mAssociatedDisplayWidth;  //此处保留了之前获取的宽度  
  26.         height = mAssociatedDisplayHeight;//此处保留了之前获取的高度  
  27.         orientation = mParameters.orientationAware ? //此处保留了屏幕的方向  
  28.                 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;  
  29.     } else {  
  30.         width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;  
  31.         height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;  
  32.         orientation = DISPLAY_ORIENTATION_0;  
  33.          
  34.     }  
看来是有必要追踪是谁调用了   setDisplayInfo  

通过source insight搜索发现

com_android_server_InputManager.cpp中对InputReaderConfiguration::setDisplayInfo调用了两次,代码如下:

[cpp]  view plain copy
  1. void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {  
  2.     JNIEnv* env = jniEnv();  
  3.    ......  
  4.     { // acquire lock  
  5.         AutoMutex _l(mLock);  
  6.   
  7.         outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed  
  8.                 * POINTER_SPEED_EXPONENT);  
  9.         outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;  
  10.   
  11.         outConfig->showTouches = mLocked.showTouches;  
  12.   
  13.         outConfig->setDisplayInfo(0, false /*external*/,  
  14.                 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);  
  15.         outConfig->setDisplayInfo(0, true /*external*/,  
  16.                 mLocked.displayExternalWidth, mLocked.displayExternalHeight,  
  17.                 mLocked.displayOrientation);  
  18.     }   
  19. }  

立刻在setDisplayInfo函数中增加打印消息

[cpp]  view plain copy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.   
  4.     LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",  
  5.             width, height, orientation);  
  6.     if (displayId == 0) {  
  7.         DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;  
  8.         info.width = width;  
  9.         info.height = height;  
  10.         info.orientation = orientation;  
  11.     }  
  12. }  
重新制作固件烧录后重启发现:

两次setDisplayInfo分别设置了连个不同分辨率的值下来

笔者用的是lvds输出,分辨率设置为1920*1080

第一次:

[cpp]  view plain copy
  1. [inputreader] [setDisplayInfo] : width=1920 height=1080 orientation=0  
第二次
[cpp]  view plain copy
  1. [inputreader] [setDisplayInfo] : width=1280 height=800 orientation=0  
这一结果也解释了触摸屏不准的情况,原来是将1280*800的分辨率映射到1920*1080的显示屏上。



解决方法

通过测试结果可以发现第一次获取的屏幕分辨率是准确的,因此于是通过以下方式修改,解决了触摸不准的情况

1.注释getReaderConfiguration中第二次调用setDisplayInfo

[cpp]  view plain copy
  1. void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {  
  2.     JNIEnv* env = jniEnv();  
  3.    ......  
  4.         outConfig->setDisplayInfo(0, false /*external*/,  
  5.                 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);  
  6.         //outConfig->setDisplayInfo(0, true /*external*/,  
  7.         //       mLocked.displayExternalWidth, mLocked.displayExternalHeight,  
  8.         //        mLocked.displayOrientation);  
  9.     }   
  10. }  

2.setDisplayInfo中修改如下:

[cpp]  view plain copy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.     LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",  
  4.             width, height, orientation);  
  5.     if (displayId == 0) {  
  6.         DisplayInfo& info = mExternalDisplay;  
  7.         info.width = width;  
  8.         info.height = height;  
  9.         info.orientation = orientation;  
  10.   
  11.         DisplayInfo& minfo =  mInternalDisplay;  
  12.         minfo.width = width;  
  13.         minfo.height = height;  
  14.         minfo.orientation = orientation;          
  15.     }  
  16. }  

这样修改就将第一次传来的屏幕分辨率保存到两个不同变量中供不同情况使用。至此android4.0的屏幕校准问题解决

这篇关于解决android4.0 触摸屏分辨率映射不准的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/986621

相关文章

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁

MySQ中出现幻读问题的解决过程

《MySQ中出现幻读问题的解决过程》文章解析MySQLInnoDB通过MVCC与间隙锁机制在可重复读隔离级别下解决幻读,确保事务一致性,同时指出性能影响及乐观锁等替代方案,帮助开发者优化数据库应用... 目录一、幻读的准确定义与核心特征幻读 vs 不可重复读二、mysql隔离级别深度解析各隔离级别的实现差异

Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法

《Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法》本文解析Spring框架中BeanCreationExce... 目录引言一、问题描述1.1 报错示例假设我们有一个简单的Java类,代表一个用户信息的实体类:然后,