Android多點觸摸的實現(xiàn)——16patch(一)_第1頁
Android多點觸摸的實現(xiàn)——16patch(一)_第2頁
Android多點觸摸的實現(xiàn)——16patch(一)_第3頁
Android多點觸摸的實現(xiàn)——16patch(一)_第4頁
Android多點觸摸的實現(xiàn)——16patch(一)_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Android多點觸摸的實現(xiàn)1.6patch(一)第一章摘要在Linux內(nèi)核支持的基礎(chǔ)上,Android在其2.0源碼中加入多點觸摸功能。由此觸摸屏在Android的frameworks被完全分為2種實現(xiàn)途徑:單點觸摸屏的單點方式,多點觸摸屏的單點和多點方式。第二章軟件位在Linux的input.h中,多點觸摸功能依賴于以下幾個主要的軟件位:.#define SYN_REPORT 0#define SYN_CONFIG 1#define SYN_MT_REPORT 2.#define ABS_MT_TOUCH_MAJOR0x30/* Major axis of touching ellipse

2、 */#define ABS_MT_TOUCH_MINOR0x31/* Minor axis (omit if circular) */#define ABS_MT_WIDTH_MAJOR0x32/* Major axis of approaching ellipse */#define ABS_MT_WIDTH_MINOR0x33/* Minor axis (omit if circular) */#define ABS_MT_ORIENTATION0x34/* Ellipse orientation */#define ABS_MT_POSITION_X0x35/* Center X el

3、lipse position */#define ABS_MT_POSITION_Y0x36/* Center Y ellipse position */#define ABS_MT_TOOL_TYPE0x37/* Type of touching device */#define ABS_MT_BLOB_ID 0x38/* Group a set of packets as a blob */在Android中對應(yīng)的軟件位定義在RawInputEvent.java中:.public class RawInputEvent . public static final int CLASS_TOU

4、CHSCREEN_MT = 0x00000010;. public static final int ABS_MT_TOUCH_MAJOR = 0x30; public static final int ABS_MT_TOUCH_MINOR = 0x31; public static final int ABS_MT_WIDTH_MAJOR = 0x32; public static final int ABS_MT_WIDTH_MINOR = 0x33; public static final int ABS_MT_ORIENTATION = 0x34; public static fina

5、l int ABS_MT_POSITION_X = 0x35; public static final int ABS_MT_POSITION_Y = 0x36; public static final int ABS_MT_TOOL_TYPE = 0x37; public static final int ABS_MT_BLOB_ID = 0x38;.public static final int SYN_REPORT = 0; public static final int SYN_CONFIG = 1;public static final int SYN_MT_REPORT = 2;.

6、在Android中,多點觸摸的實現(xiàn)方法在具體的代碼實現(xiàn)中和單點是完全區(qū)分開的。在Android代碼的EventHub.cpp中,單點屏和多點屏由如下代碼段來判定:int EventHub:open_device(const char *deviceName)if (test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)&& test_bit(ABS_MT_POSITION_X, abs_bitmask)&& test_bit(ABS_MT_POSITION_Y, abs_bitmask) device->classes |= C

7、LASS_TOUCHSCREEN | CLASS_TOUCHSCREEN_MT;/ LOGI("It is a multi-touch screen!"); /single-touch?else if (test_bit(BTN_TOUCH, key_bitmask)&& test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask) device->classes |= CLASS_TOUCHSCREEN;/ LOGI("It is a single-touch sc

8、reen!");.我們知道,在觸摸屏驅(qū)動中,通常在probe函數(shù)中會調(diào)用input_set_abs_params給設(shè)備的input_dev結(jié)構(gòu)體初始化,這些input_dev的參數(shù)會在Android的EventHub.cpp中被讀取。如上可知,如果我們的觸摸屏想被當(dāng)成多點屏被處理,只需要在驅(qū)動中給input_dev額外增加以下幾個參數(shù)即可:input_set_abs_params(mcs_data.input, ABS_MT_POSITION_X, pdata->abs_x_min, pdata->abs_x_max, 0, 0);input_set_abs_params

9、(mcs_data.input, ABS_MT_POSITION_Y, pdata->abs_y_min, pdata->abs_y_max, 0, 0);input_set_abs_params(mcs_data.input, ABS_MT_TOUCH_MAJOR, 0, 15, 0, 0); /相當(dāng)于單點屏的ABX_PRESSUREinput_set_abs_params(mcs_data.input, ABS_MT_WIDTH_MAJOR, 0, 15, 0, 0); /相當(dāng)于單點屏的ABS_TOOL_WIDTH注:為了讓我們的驅(qū)動代碼支持所有的Android版本,無論是多點

10、屏還是單點屏,一般都會保留單點屏的事件,如ABS_TOUCH, ABS_PRESSURE, ABS_X, ABS_Y等。另外,由于在Android2.0前支持多點的frameworks大多是用HAT0X,HAT0Y來實現(xiàn)的,所以一般也會上報這2個事件。第三章同步方式由于多點觸摸技術(shù)需要采集到多個點,然后再一起處理這些點,所以在軟件實現(xiàn)中需要保證每一波點的準(zhǔn)確性和完整性。因此,Linux內(nèi)核提供了input_mt_sync(struct input_dev * input)函數(shù)。在每波的每個點上報后需要緊跟一句input_mt_sync(), 當(dāng)這波所有點上報后再使用input_sync()進(jìn)行

11、同步。例如一波要上報3個點:/* 上報點1*/.input_mt_sync(input);/* 上報點2*/.input_mt_sync(input);/* 上報點3*/.input_mt_sync(input);input_sync(input);注:即使是僅上報一個點的單點事件,也需要一次input_my_sync。在Android的KeyInputQueue.java中,系統(tǒng)創(chuàng)建了一個線程,然后把所有的Input事件放入一個隊列:public abstract class KeyInputQueue Thread mThread = new Thread("InputDevic

12、eReader") public void run() android.os.Process.setThreadPriority( android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY); try RawInputEvent ev = new RawInputEvent(); while (true) InputDevice di; / block, doesn't release the monitor readEvent(ev);if (ev.type = RawInputEvent.EV_DEVICE_ADDED) sync

13、hronized (mFirst) di = newInputDevice(ev.deviceId); mDevices.put(ev.deviceId, di); configChanged = true; else if (ev.type = RawInputEvent.EV_DEVICE_REMOVED) synchronized (mFirst) Log.i(TAG, "Device removed: id=0x" + Integer.toHexString(ev.deviceId); di = mDevices.get(ev.deviceId); if (di !

14、= null) mDevices.delete(ev.deviceId); configChanged = true; else Log.w(TAG, "Bad device id: " + ev.deviceId); else di = getInputDevice(ev.deviceId); / first crack at it send = preprocessEvent(di, ev); if (ev.type = RawInputEvent.EV_KEY) di.mMetaKeysState = makeMetaState(ev.keycode, ev.valu

15、e != 0, di.mMetaKeysState); mHaveGlobalMetaState = false; if (di = null) continue; if (configChanged) synchronized (mFirst) addLocked(di, SystemClock.uptimeMillis(), 0, RawInputEvent.CLASS_CONFIGURATION_CHANGED, null); if (!send) continue; synchronized (mFirst) . if (type = RawInputEvent.EV_KEY &

16、;& (classes&RawInputEvent.CLASS_KEYBOARD) != 0 && (scancode < RawInputEvent.BTN_FIRST | scancode > RawInputEvent.BTN_LAST) /* 鍵盤按鍵事件 */ . else if (ev.type = RawInputEvent.EV_KEY) /* 下面是EV_KEY事件分支,只支持單點的觸摸屏有按鍵事件, * 而支持多點的觸摸屏沒有按鍵事件,只有絕對坐標(biāo)事件*/ if (ev.scancode = RawInputEvent.BTN_T

17、OUCH && (classes&(RawInputEvent.CLASS_TOUCHSCREEN |RawInputEvent.CLASS_TOUCHSCREEN_MT) = RawInputEvent.CLASS_TOUCHSCREEN) /* 只支持單點的觸摸屏的按鍵事件 */ else if (ev.scancode = RawInputEvent.BTN_MOUSE && (classes&RawInputEvent.CLASS_TRACKBALL) != 0) /* 鼠標(biāo)和軌跡球 */ . else if (ev.type = Raw

18、InputEvent.EV_ABS && (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) /* 下面才是多點觸摸屏上報的事件 */ if (ev.scancode = RawInputEvent.ABS_MT_TOUCH_MAJOR) di.mAbs.changed = true; di.mAbs.mNextDatadi.mAbs.mAddingPointerOffset + MotionEvent.SAMPLE_PRESSURE = ev.value; else if (ev.scancode = RawInput

19、Event.ABS_MT_POSITION_X) di.mAbs.changed = true; di.mAbs.mNextDatadi.mAbs.mAddingPointerOffset + MotionEvent.SAMPLE_X = ev.value; else if (ev.scancode = RawInputEvent.ABS_MT_POSITION_Y) di.mAbs.changed = true; di.mAbs.mNextDatadi.mAbs.mAddingPointerOffset + MotionEvent.SAMPLE_Y = ev.value; else if (

20、ev.scancode = RawInputEvent.ABS_MT_WIDTH_MAJOR) di.mAbs.changed = true; di.mAbs.mNextDatadi.mAbs.mAddingPointerOffset + MotionEvent.SAMPLE_SIZE = ev.value; /* 上面這段就是多點觸摸屏要用到的事件上報部分; * 使用一個數(shù)組mNextData來保存,其中di.mAbs.mAddingPointerOffset * 是當(dāng)前點的偏移量,在每個點中還在MotionEvent中定義了X,Y,PRESSURE * SIZE等偏移量,多點觸摸屏的壓力值

21、由絕對坐標(biāo)事件ABS_MT_TOUCH_MAJOR確定。 */ else if (ev.type = RawInputEvent.EV_ABS && (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) /* 這里是對單點觸摸屏上報坐標(biāo)事件的新的處理方法,同樣使用了數(shù)組來保存 */ if (ev.scancode = RawInputEvent.ABS_X) di.mAbs.changed = true; di.curTouchValsMotionEvent.SAMPLE_X = ev.value; else if (ev.sc

22、ancode = RawInputEvent.ABS_Y) di.mAbs.changed = true; di.curTouchValsMotionEvent.SAMPLE_Y = ev.value; else if (ev.scancode = RawInputEvent.ABS_PRESSURE) di.mAbs.changed = true; di.curTouchValsMotionEvent.SAMPLE_PRESSURE = ev.value; di.curTouchValsMotionEvent.NUM_SAMPLE_DATA + MotionEvent.SAMPLE_PRES

23、SURE = ev.value; else if (ev.scancode = RawInputEvent.ABS_TOOL_WIDTH) di.mAbs.changed = true; di.curTouchValsMotionEvent.SAMPLE_SIZE = ev.value; di.curTouchValsMotionEvent.NUM_SAMPLE_DATA + MotionEvent.SAMPLE_SIZE = ev.value; . /* 下面是關(guān)鍵的同步處理方法 */ if (ev.type = RawInputEvent.EV_SYN && ev.scan

24、code = RawInputEvent.SYN_MT_REPORT && di.mAbs != null) /* 在這里實現(xiàn)了對SYN_MT_REPORT事件的處理, * 改變了di.mAbs.mAddingPointerOffset的值,從而將 * 新增的點的參數(shù)保存到下一組偏移量的位置。 */ . final int newOffset = (num <= InputDevice.MAX_POINTERS) ? (num * MotionEvent.NUM_SAMPLE_DATA) : (InputDevice.MAX_POINTERS * MotionEvent.

25、NUM_SAMPLE_DATA); di.mAbs.mAddingPointerOffset = newOffset; di.mAbs.mNextDatanewOffset + MotionEvent.SAMPLE_PRESSURE = 0; . else if (send | (ev.type = RawInputEvent.EV_SYN && ev.scancode = RawInputEvent.SYN_REPORT) /* 這里實現(xiàn)了對SYN_REPORT事件的處理 * 如果是單點觸摸屏,即使用di.curTouchVals數(shù)組保存的點 * 轉(zhuǎn)化為多點觸摸屏的mNext

26、Data數(shù)組保存 * 最后是調(diào)用InputDevice中的generateAbsMotion處理這個數(shù)組。這個函數(shù) * 的具體實現(xiàn)方法將在后面補(bǔ)充 */ . ms.finish(); /重置所有點和偏移量 .由于上層的代碼仍然使用ABS_X, ABS_Y這些事件,為了使多點觸摸屏代碼有良好的兼容性,在KeyInputQueue.java的最后,我們將多點事件類型轉(zhuǎn)化為單點事件類型,返回一個新的InputDevice:private InputDevice newInputDevice(int deviceId) int classes = getDeviceClasses(deviceId);

27、String name = getDeviceName(deviceId);InputDevice.AbsoluteInfo absX; InputDevice.AbsoluteInfo absY; InputDevice.AbsoluteInfo absPressure; InputDevice.AbsoluteInfo absSize; if (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_X, &qu

28、ot;X"); absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_Y, "Y"); absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure"); absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size"); else if (classes

29、&RawInputEvent.CLASS_TOUCHSCREEN) != 0) absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X"); absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y"); absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure"); absSize = loadAbsolute

30、Info(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size"); else absX = null; absY = null; absPressure = null; absSize = null; return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize); 第四章觸摸事件數(shù)組的處理上面我們曾說到generateAbsMotion這個方法,它們在InputDevice類的內(nèi)部類MotionState中實現(xiàn),該類被定義為Input

31、Device類的靜態(tài)成員類(static class),調(diào)用它們可以直接使用:InputDeviceClass.MotionStateClass.generateAbsMotion()。public class InputDevice static class MotionState /下面是這個內(nèi)部類的幾個函數(shù) ./* mLastNumPointers 為上一個動作在觸屏上按鍵的個數(shù) */int mLastNumPointers = 0;final int mLastData = new intMotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS;/* mNe

32、xtNumPointers 為下一個動作在觸屏上按鍵的個數(shù) */* 通過對這2個值大小的判斷,可以確認(rèn)新的動作方式 */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 boole

33、an 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 = updat

34、ePointerIdentifiers(); final int numPointers = mLastNumPointers; /* 對行為的判斷 */ if (nextNumPointers != lastNumPointers) /前后在觸屏上點個數(shù)不同,說明有手指up或down if (nextNumPointers > lastNumPointers) if (lastNumPointers = 0) /上次觸屏上沒有按鍵,新值又大,說明有按鍵按下 action = MotionEvent.ACTION_DOWN; mDownTime = curTime; else /有新點按下,分配給新點ID號 action = MotionEvent.ACTION_POINTER_DOWN | (upOrDownPointer <&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論