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

相关文章

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

java内存泄漏排查过程及解决

《java内存泄漏排查过程及解决》公司某服务内存持续增长,疑似内存泄漏,未触发OOM,排查方法包括检查JVM配置、分析GC执行状态、导出堆内存快照并用IDEAProfiler工具定位大对象及代码... 目录内存泄漏内存问题排查1.查看JVM内存配置2.分析gc是否正常执行3.导出 dump 各种工具分析4.

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实