2010年9月20日 星期一

ACTION_UP missing?

I am try to implement a custom VIEW for practice, and I got a strange issue: I want to handle the ACTION_UP event in onTouchEvent() method, but I cannot see the ACTION_UP event.

After some testing, I found the root course of this issue: ACTION_DOWN event must be handled before you received the ACTION_UP event.

Cause my view does not need to care about ACTION_DOWN, so I just put ACTION_UP check and return TRUE for it, otherwise return super.onTouchEvent() instead.

Seems Android makes a assumption that if you don’t care about ACTION_DOWN, you will not need ACTION_UP. And that’s why I cannot get the ACTION_UP event.

After I add a ACTION_DOWN checking and return TRUE, I can receive ACTION_UP as normal.

I don’t know if my guess is correct, I cannot find any document mention this assumption. But that’s the behavior I can see in my test code, and that really solve my issue.

Layout 元件的大小單位轉換

Android 上有幾個在 layout 上可用的單位:

px

Pixels - 對應到螢幕的幾個像素(pixel)

in

Inches - 對應到實際的距離為幾呎

pt

Points - 通常用在 font size,一個 point 為 1/72 inch

換算公式:pt = px * dpi /72

dp (dip)

Density-Independent Pixels - 對應到在 160 dpi 的螢幕上的幾個像素,為 android 用來處理支援不同大小的螢幕的 layout 單位,詳細定義可以參考 android SDK 的文件。在 android 的 layout 中,應該都使用 dp 來給定元件大小,而不應使用 px。

換算公式:dp = px * dpi / 160

sp

Scale-Independent Pixels - 跟 dip 一樣,加上 pt 的觀念。簡單的說,就是對應在 160 dpi 的螢幕上的幾個 pt。在 android 中,文字大小應使用 sp 而非 pt。

換算公式:sp = pt * dpi / 160

換算公式:sp = dp * dpi / 72


舉個例子說明:你有一個 49 dp 的按鈕,在 120 dpi 的螢幕上,裡面的文字大小最大應為: 49 * 120 / 160 = 36.75 sp。