顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2010年12月12日 星期日

Population Count of a Uint32

When studying Android audio system, I saw a interest function code in google’s AndioSystem class:

uint32_t AudioSystem::popCount(uint32_t u)

{

    u = ((u&0x55555555) + ((u>>1)&0x55555555));

    u = ((u&0x33333333) + ((u>>2)&0x33333333));

    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));

    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));

    u = ( u&0x0000ffff) + (u>>16);

    return u;

}

Honestly, I cannot understand how this code come from. Fortunately, the code leave a reference link for us: http://www.df/lth/se/~john_e/gems/gem002d.html.

And telling the trust again, for me, it’s still hard to understand after reading the reference algorithm. So, I decide to make a node, and just copy the code when I need it ~ 吐舌頭.

2010年10月11日 星期一

Android Doc, 加油, 好嗎?

最近工作需求正在狂啃 MSDN,然想起了一個月前開始玩 android 的經驗。有時候,documentation 真的比你想像的重要~

一切的故事就從 Install the SDK 開始:Step 1, Preparing you development computer。很好,它給了我 link 去安裝 JDK 與 Eclipse;當時的最新版是 JDK 6 Update 21 與 Eclipse 3.6。

安裝完 JDK 後,突然看到 Eclipse 上說 JDK 6 Update 21 有問題(…);還好,後來找到說現在下載的最新版 JDK 應該修好了(虛驚一場,汗)。

裝完 Eclipse 後照著 Install the SDK 繼續往下,該裝 ADT plugin 了(疑?還要跳到其他文件繼續喔)。然後就看到

Caution: There are known issues with the ADT plugin running with Eclipse 3.6. Please stay on 3.5 until further notice.

(啊你是不會早點講喔~~~!X!)

到這邊為止,我有種被耍的感覺。雖然不是甚麼解決不了的問題,但是搞成這樣真的是很讓人不愉快。安裝文件不就該是 step by step 就好的嗎?哪有人這樣限制放在後面說的啊!於是,我只好移掉裝好的 Eclipse,然後決定把網站裡所有跟安裝有關的文件都看完後,再繼續安裝我可愛的 Android SDK。

現在,對比 MSDN Library 的文件與 developer.android 的 SDK Reference,仍可以明確的感覺到 android 這邊文件的缺乏。大多數 Windows API 都可以在 MSDN 找到真正的使用法,而 android 這邊,總是需要靠著 google 大神四處挖其他開發者的經驗談。

我只能說,也許 Android 的開發者都是高手吧,有些人還去 download SDK 的原始碼來研究要怎麼用,真是見鬼了…。

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。