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

Android 實現指針滑動動態效果的方法

編輯:關於 Android 程式設計

要做出一個像儀表指針般平滑移動的動態效果,可以透過 FrameLayout 疊放指針與刻度,再開啟一條執行緒,利用迴圈與休眠來控制指針的 padding 屬性,藉此產生移動的動畫。以下先來看版面配置。

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="2dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/up_icon"
        android:layout_marginTop="0dp"
        android:paddingTop="0dp" />

    <LinearLayout
        android:layout_width="240dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="7dp"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="優"
            android:textSize="12sp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="良"
            android:textSize="12sp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="中等"
            android:textSize="12sp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="不健康"
            android:textSize="12sp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="有毒害"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="240dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="40dp"
        android:orientation="horizontal" >
        <TextView android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center_horizontal"
            android:text="80" android:textSize="12sp" />
        <TextView android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center_horizontal"
            android:text="120" android:textSize="12sp" />
        <TextView android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center_horizontal"
            android:text="160" android:textSize="12sp" />
        <TextView android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center_horizontal"
            android:text="200" android:textSize="12sp" />
        <TextView android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center_horizontal"
            android:text="400" android:textSize="12sp" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/zhizhen"
        android:id="@+id/zhizhen"/>

    <ImageView
        android:id="@+id/dengji_img"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:src="@drawable/dengji_icon" />
</FrameLayout>

上面這段程式碼實現的版面如下:

由於指針壓在下面的滾動條上,因此使用 FrameLayout 來做層疊配置。接著要實現指針的等速移動,就需要開啟一條執行緒,在執行緒中用迴圈搭配休眠,並透過改變指針圖示的 padding 屬性來產生移動動畫。

Handler myHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        // 對 c 的更改與迴圈應該在執行緒中跑,否則 run 僅執行一次
        zhizhen.setPadding(c, 0, 0, 0);
    }
};

class MyThread extends Thread {
    @Override
    public void run() {
        // 發送訊息,通知主執行緒改變 UI
        try {
            while (c <= input) {
                c = c + 1;
                this.sleep(10);
                myHandler.sendEmptyMessage(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

重點觀念:因為 Android 不能在子執行緒直接更新 UI,所以真正的 padding 變更要透過 Handler 發訊息交給主執行緒處理;而迴圈與 sleep 則放在自訂的 Thread 中負責計時與推進,兩者搭配就能讓指針平順地滑到目標位置。

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