解决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

相关文章

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

分辨率三兄弟LPI、DPI 和 PPI有什么区别? 搞清分辨率的那些事儿

《分辨率三兄弟LPI、DPI和PPI有什么区别?搞清分辨率的那些事儿》分辨率这个东西,真的是让人又爱又恨,为了搞清楚它,我可是翻阅了不少资料,最后发现“小7的背包”的解释最让我茅塞顿开,于是,我... 在谈到分辨率时,我们经常会遇到三个相似的缩写:PPI、DPI 和 LPI。虽然它们看起来差不多,但实际应用

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

SpringBoot启动报错的11个高频问题排查与解决终极指南

《SpringBoot启动报错的11个高频问题排查与解决终极指南》这篇文章主要为大家详细介绍了SpringBoot启动报错的11个高频问题的排查与解决,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一... 目录1. 依赖冲突:NoSuchMethodError 的终极解法2. Bean注入失败:No qu

springboot报错Invalid bound statement (not found)的解决

《springboot报错Invalidboundstatement(notfound)的解决》本文主要介绍了springboot报错Invalidboundstatement(not... 目录一. 问题描述二.解决问题三. 添加配置项 四.其他的解决方案4.1 Mapper 接口与 XML 文件不匹配