編輯:關於 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 中負責計時與推進,兩者搭配就能讓指針平順地滑到目標位置。
Android 內存洩露的自動化測試實踐
進行 Android 內存洩露分析時,面對成千上萬個對象,你是否感到頭疼?作為測試人員,在做完洩露測試後,是否有勇氣告訴開發同事「程序已無洩漏、可以放心發布」?內存洩露測
Android 學習筆記(四):Activity 全景解析——生命週期、通信、啟動模式與系統調用
一、Activity 是什麼 Activity 是 Android 四大組件之一,用於承載 View、提供用戶交互入口。它本身並不繪製界面,而是創建一個窗口,在其上進行繪
堅果手機截圖三種玩法 組合鍵、區域與長截圖
錘子科技曾以情懷著稱,當年的T1發表會想必還有人記得。如今堅果手機登場,作為錘子出品,它在細節上確實與其他安卓機有些不同。下面小編就重點演示幾種常用的截圖方式。 堅果手機
Android WebView 入門:打造一個能上網、能讀本地頁面的簡易瀏覽器
想把網頁內容直接嵌進 App,最省力的方式就是使用 WebView。本文示範一個堪用的小瀏覽器,它做到三件事:開啟指定網址、載入 assets 目錄下的離線頁面,以及用實