Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義 View 實戰:用像素判斷做出電視遙控式環形按鈕(上)

編輯:關於 Android 程式設計

這篇的目標很明確:用最樸素的方式,做出下圖這種環形排列的按鈕效果,並且把每一步都講清楚。

老規矩,先看效果:

這是一個點餐界面,六種菜品意味著六個按鈕。點擊「開始點餐」後的幕布上升動畫留到下篇,這篇只解決一件事:這個樣式的布局怎麼實現。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="你好!"
        android:textSize="20sp">

        <ImageView
            android:id="@+id/split_ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/total1111111" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/a"
            android:tag="telId1" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/b"
            android:tag="telId12" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/c"
            android:tag="telId3" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/d"
            android:tag="telId4" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/e"
            android:tag="telId5" />

        <com.example.opendor.TrapezoidImageButton
            android:id="@+id/telId6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/f"
            android:tag="telId6">

            <Button
                android:id="@+id/bill"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_above="@+id/clear"
                android:layout_marginBottom="20dp"
                android:background="@drawable/local_order"
                android:gravity="center"
                android:onClick="bill"
                android:textSize="20sp" />

            <Button
                android:id="@+id/clear"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="5dp"
                android:background="@drawable/clear_data"
                android:gravity="center"
                android:onClick="clear"
                android:textSize="20sp" />

            <Button
                android:id="@+id/order"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignBaseline="@id/clear"
                android:layout_marginBottom="20dp"
                android:background="@drawable/start_menu"
                android:gravity="center"
                android:onClick="order"
                android:textSize="20sp" />
        </com.example.opendor.TrapezoidImageButton>
    </TextView>
</RelativeLayout>

結構其實很單純:一個相對布局,一個 TextView 承載所有子 View,一個鋪底的大背景 ImageView,六個自定義 View(也就是六個菜品按鈕),最後三個普通 Button。

接下來才是重點——這個自定義 View 到底怎麼寫?核心只有兩個方法。

第一個是 onTouchEvent:

public boolean onTouchEvent(MotionEvent event) {
    if (isTouchPointInView(event.getX(), event.getY())
            || event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    } else {
        return false;
    }
}

邏輯很直白:如果觸摸點落在「有效區域」內,就走父類的實現;否則直接返回 false。熟悉事件傳遞機制的人應該一點就通——Android 默認都是 super 實現,事件會一路向上傳到 Activity,最終由 onClick 響應。返回 false,等於這次點擊被丟棄。

第二個方法是 isTouchPointInView,判斷的依據是像素透明度:

protected boolean isTouchPointInView(float localX, float localY) {
    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    draw(canvas);

    int x = (int) localX;
    int y = (int) localY;
    if (x < 0 || x >= getWidth()) return false;
    if (y < 0 || y >= getHeight()) return false;

    int pixel = bitmap.getPixel(x, y);
    if ((pixel & 0xff000000) != 0) {
        return true;
    } else {
        return false;
    }
}

做法是先按當前 View 的尺寸建一張 ARGB_8888 的 Bitmap(用 RGB_565 也可以,更省內存),再用它建一個 Canvas,把內容原樣繪制上去。

接著 X、Y 就是手指按下的坐標(默認從屏幕左上角起,向右為 +X,向下為 +Y)。如果這個點根本不在當前畫布範圍內,直接返回 false,onTouchEvent 隨之返回 false,點擊不觸發。

最後取出該位置的像素值 pixel,與一個高 8 位全為 1 的 ARGB 值做按位與,結果不為 0 就返回 true,否則返回 false。

這句話值得展開說說:

0xff000000 的二進制是 1111 1111 0000 0000 0000 0000 0000 0000,而按位與的規則是 0&0=0、0&1=0、1&1=1。

所以低 24 位無論 pixel 是什麼,結果都是 0;高 8 位只有在 pixel 的高 8 位存在 1 時才不為 0。也就是說,(pixel & 0xff000000) 為 0 的充要條件是 pixel 的 alpha 通道全透明。

再看 Bitmap#getPixel(x, y) 幾個常見返回值:

BLACK:-16777216
BLUE:-16776961
GRAY:-7829368
GREEN:-16711936
TRANSPARENT:0
WHITE:-1
YELLOW:-256

只有 TRANSPARENT 是 0,其餘都是負數,高位都不為 0。所以這裡用 getPixel(x, y) == 0 就能判斷該像素是否透明。

那為什麼要判斷透明與否?看這張圖就明白了——這正是上面六個菜品按鈕的素材:

圖中用紅框圈出來的部分其實是透明的,只有「熱菜」那一塊不透明。梯形之外的區域雖然也在 View 的矩形邊界之內,但像素是全透明的。

所以整段代碼的含義可以濃縮成一句話:只有當手指按在「熱菜」這個不透明區域上時,才觸發點擊事件;按在周圍的透明區域則忽略。

如果不用這招,六個梯形按鈕疊在一起時,矩形命中範圍會互相遮擋,點擊永遠落在最上層那個 View 上,效果就完全亂了。

源碼已上傳 GitHub,歡迎下載,覺得有幫助的話給個 star:
Android-The-ring-button

下篇再講其中的動畫實現。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved