您的位置:首页 > 移动开发 > Android开发

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

2015-12-08 14:12 507 查看
前言

几个月以前升级了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
plaincopy

void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,

int32_t width, int32_t height, int32_t orientation) {

if (displayId == 0) {

DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;

info.width = width;

info.height = height;

info.orientation = orientation;

}

}

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

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

[cpp] view
plaincopy

bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,

int32_t* width, int32_t* height, int32_t* orientation) const {

}

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

[cpp] view
plaincopy

void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {

int32_t oldDeviceMode = mDeviceMode;

......

// Get associated display dimensions.

if (mParameters.associatedDisplayId >= 0) {

if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,

mParameters.associatedDisplayIsExternal,

&mAssociatedDisplayWidth, &mAssociatedDisplayHeight,

&mAssociatedDisplayOrientation)) {

LOGI(INDENT "Touch device '%s' could not query the properties of its associated "

"display %d. The device will be inoperable until the display size "

"becomes available.",

getDeviceName().string(), mParameters.associatedDisplayId);

//这块打印消息直接暴露了 触摸屏设备获取屏幕分辨率的踪迹,看来触摸屏确实是通过获取

//当前屏幕分辨率来映射坐标值的

mDeviceMode = DEVICE_MODE_DISABLED;

return;

}

}

// Configure dimensions.

int32_t width, height, orientation;

if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {

width = mAssociatedDisplayWidth; //此处保留了之前获取的宽度

height = mAssociatedDisplayHeight;//此处保留了之前获取的高度

orientation = mParameters.orientationAware ? //此处保留了屏幕的方向

mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;

} else {

width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;

height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;

orientation = DISPLAY_ORIENTATION_0;

}

看来是有必要追踪是谁调用了setDisplayInfo

通过source insight搜索发现

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

[cpp] view
plaincopy

void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {

JNIEnv* env = jniEnv();

......

{ // acquire lock

AutoMutex _l(mLock);

outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed

* POINTER_SPEED_EXPONENT);

outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;

outConfig->showTouches = mLocked.showTouches;

outConfig->setDisplayInfo(0, false /*external*/,

mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);

outConfig->setDisplayInfo(0, true /*external*/,

mLocked.displayExternalWidth, mLocked.displayExternalHeight,

mLocked.displayOrientation);

}

}

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

[cpp] view
plaincopy

void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,

int32_t width, int32_t height, int32_t orientation) {

LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",

width, height, orientation);

if (displayId == 0) {

DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;

info.width = width;

info.height = height;

info.orientation = orientation;

}

}

重新制作固件烧录后重启发现:

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

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

第一次:

[cpp] view
plaincopy

[inputreader] [setDisplayInfo] : width=1920 height=1080 orientation=0

第二次

[cpp] view
plaincopy

[inputreader] [setDisplayInfo] : width=1280 height=800 orientation=0

这一结果也解释了触摸屏不准的情况,原来是将1280*800的分辨率映射到1920*1080的显示屏上。

解决方法

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

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

[cpp] view
plaincopy

void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {

JNIEnv* env = jniEnv();

......

outConfig->setDisplayInfo(0, false /*external*/,

mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);

//outConfig->setDisplayInfo(0, true /*external*/,

// mLocked.displayExternalWidth, mLocked.displayExternalHeight,

// mLocked.displayOrientation);

}

}

2.在setDisplayInfo中修改如下:

[cpp] view
plaincopy

void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,

int32_t width, int32_t height, int32_t orientation) {

LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",

width, height, orientation);

if (displayId == 0) {

DisplayInfo& info = mExternalDisplay;

info.width = width;

info.height = height;

info.orientation = orientation;

DisplayInfo& minfo = mInternalDisplay;

minfo.width = width;

minfo.height = height;

minfo.orientation = orientation;

}

}

这样修改就将第一次传来的屏幕分辨率保存到两个不同变量中供不同情况使用。至此android4.0的屏幕校准问题解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: