第四章 触摸事件数组的处理
上面我们曾说到generateAbsMotion这个方法,它们在InputDevice类的内部类MotionState中实现,该类被定义为InputDevice类的静态成员类(static class),调用它们可以直接使用:
InputDeviceClass.MotionStateClass.generateAbsMotion()。
public class InputDevice { …………………………… static class MotionState {//下面是这个内部类的几个函数 ………………………………. /* mLastNumPointers 为上一个动作在触屏上按键的个数 */ int mLastNumPointers = 0; final int[] mLastData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS]; /* mNextNumPointers 为下一个动作在触屏上按键的个数 */ /* 通过对这2个值大小的判断,可以确认新的动作方式 */ int mNextNumPointers = 0; final int[] mNextData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS) + MotionEvent.NUM_SAMPLE_DATA]; …………………………………. int[] generateAveragedData(int upOrDownPointer, int lastNumPointers, int nextNumPointers) { //平滑处理 ……………………………………. } private boolean assignPointer(int nextIndex, boolean allowOverlap) {//指派按键 …………………………………… } private int updatePointerIdentifiers() {//更新按键ID …………………………………. } void removeOldPointer(int index) { …………………………………… } MotionEvent generateAbsMotion(InputDevice device, long curTime, long curTimeNano, Display display, int orientation, int metaState) { …………………………………… int upOrDownPointer = updatePointerIdentifiers(); final int numPointers = mLastNumPointers; ……………………………………… /* 对行为的判断 */ if (nextNumPointers != lastNumPointers) { //前后在触屏上点个数不同,说明有手指up或down if (nextNumPointers > lastNumPointers) { if (lastNumPointers == 0) { //上次触屏上没有按键,新值又大,说明有按键按下 action = MotionEvent.ACTION_DOWN; mDownTime = curTime; } else {//有新点按下,分配给新点ID号 action = MotionEvent.ACTION_POINTER_DOWN | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT); } } else {//新动作比原来pointer数量少 if (numPointers == 1) { //原来只有1个点按下,所以现在的动作是全部按键up action = MotionEvent.ACTION_UP; } else { //原来有多点按下,现在是ACTION_POINTER_UP动作, action = MotionEvent.ACTION_POINTER_UP | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT); } } currentMove = null; } else { //前后触屏pointer个数相同,所以是移动动作ACTION_MOVE action = MotionEvent.ACTION_MOVE; } /* 后面则是根据屏幕的height和width以及屏幕方向orientation对这些点进行二次处理 */ …………………………………… } MotionEvent generateRelMotion(InputDevice device, long curTime, long curTimeNano, int orientation, int metaState) { /* 轨迹球等的处理方式 */ ………………………………………….. } void finish() { //结束这轮动作 mNextNumPointers = mAddingPointerOffset = 0; mNextData[MotionEvent.SAMPLE_PRESSURE] = 0; } ……………………………………. } ………………………………. …………………………………… } |
第五章 接口
我们平时所看到的用2个手指对图片放大缩小、旋转等手势都是由应用程序编写浏览器实现的。这些应用程序大多会使用Android2.0以上的在MotionEvent.java中实现的新的接口。所以,我们还需要给MotionEvent类补充尽量全的接口。这里可以完全参照google新的android代码。
第六章 总结
综上,在硬件支持基础上,Android1.6如果要实现多点触摸功能,主要工作可简述为以下几个方面:
1、 驱动中,除了增加多点的事件上报方式,还要完全更改单点的事件上报方式。
2、 Android的Frameworks层需要修改的文件有:EventHub.cpp,RawInputEvent.java,KeyInputQueue.java,InputDevice.java,MotionEvent.java。
3、 编写新的支持多点触摸功能的多媒体浏览器。
4、 为了代码简练,android2.0在轨迹球和单点屏事件方式中也全使用了新的变量名,以方便多点屏事件同样能使用这些变量,所以修改时还需要注意许多细节方面。