Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 收音機 UI 實作:自訂 View 打造復古面板

近來在讀收音機的源碼,原本想把它從系統中抽出來做成獨立 App,但收音機功能高度依賴底層驅動與硬體支援,這條路走不通。不過翻著翻著,倒是被它的 UI 設計吸引住了——指針刻度、LED 顯示區、調頻轉盤,頗有幾分復古收音機的味道(圖片醜不醜是美工的責任,哈哈)。既然功能搬不走,就把介面單獨拿出來分享,順便自己也把自訂 View 練習一遍。

程式碼下載:

免費下載地址在 https://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/11月/26日/Android之收音機UI實現

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3.   android:layout_width="fill_parent"
  4.   android:layout_height="fill_parent"
  5.   android:background="@drawable/background_main"
  6.   android:orientation="vertical">
  7.   <RelativeLayout
  8.     android:layout_width="320dip"
  9.     android:layout_height="113dip"
  10.     android:layout_x="0dip"
  11.     android:layout_y="180dip"
  12.     android:background="@drawable/led_disabled"/>
  13.   <RelativeLayout
  14.     android:id="@+id/led_background"
  15.     android:layout_width="320dip"
  16.     android:layout_height="113dip"
  17.     android:layout_x="0dip"
  18.     android:layout_y="180dip"
  19.     android:background="@drawable/led_enabled">
  20.     <ImageView
  21.       android:id="@+id/fm_indicator"
  22.       android:layout_width="wrap_content"
  23.       android:layout_height="wrap_content"
  24.       android:layout_alignParentLeft="true"
  25.       android:layout_alignParentTop="true"
  26.       android:layout_marginLeft="14dip"
  27.       android:layout_marginTop="12dip"
  28.       android:src="@drawable/fm"/>
  29.     <ImageView
  30.       android:id="@+id/headset_indicator"
  31.       android:layout_width="wrap_content"
  32.       android:layout_height="wrap_content"
  33.       android:layout_marginTop="12dip"
  34.       android:layout_toRightOf="@id/fm_indicator"
  35.       android:src="@drawable/headset_indicator"/>
  36.     <TextView
  37.       android:id="@+id/text_hour_type"
  38.       android:layout_width="wrap_content"
  39.       android:layout_height="wrap_content"
  40.       android:layout_alignParentRight="true"
  41.       android:layout_alignParentTop="true"
  42.       android:layout_marginRight="13dip"
  43.       android:layout_marginTop="10dip"
  44.       android:textColor="#000000"/>
  45.     <com.way.view.DigitalTextView
  46.       android:id="@+id/digital_clock"
  47.       android:layout_width="wrap_content"
  48.       android:layout_height="wrap_content"
  49.       android:layout_alignParentTop="true"
  50.       android:layout_marginTop="13dip"
  51.       android:layout_toLeftOf="@+id/text_hour_type"/>
  52.     <ImageView
  53.       android:id="@+id/searching_indicator"
  54.       android:layout_width="wrap_content"
  55.       android:layout_height="wrap_content"
  56.       android:layout_marginTop="13dip"
  57.       android:layout_toLeftOf="@id/digital_clock"
  58.       android:src="@drawable/searching"/>
  59.     <com.way.view.DigitalTextView
  60.       android:id="@+id/digital_freq"
  61.       android:layout_width="wrap_content"
  62.       android:layout_height="wrap_content"
  63.       android:layout_alignParentRight="true"
  64.       android:layout_alignParentTop="true"
  65.       android:layout_marginRight="84dip"
  66.       android:layout_marginTop="15dip"/>
  67.     <SeekBar
  68.       android:id="@+id/freq_indicator"
  69.       android:layout_width="fill_parent"
  70.       android:layout_height="wrap_content"
  71.       android:layout_alignParentBottom="true"
  72.       android:layout_marginBottom="12dip"
  73.       android:layout_marginLeft="7dip"
  74.       android:layout_marginRight="7dip"
  75.       android:progressDrawable="@drawable/blank"
  76.       android:thumb="@drawable/pointer"
  77.       android:thumbOffset="0dip"/>
  78.   </RelativeLayout>
  79.   <FrameLayout
  80.     android:layout_width="220dip"
  81.     android:layout_height="30dip"
  82.     android:layout_x="50dip"
  83.     android:layout_y="291dip"
  84.     android:background="@drawable/scroller_bg">
  85.     <com.way.view.TuneWheel
  86.       android:id="@+id/tune_wheel"
  87.       android:layout_width="fill_parent"
  88.       android:layout_height="fill_parent"/>
  89.     <ImageView
  90.       android:layout_width="fill_parent"
  91.       android:layout_height="fill_parent"
  92.       android:src="@drawable/tune_wheel_highlight"/>
  93.   </FrameLayout>
  94.   <ImageButton
  95.     android:id="@+id/add_button"
  96.     android:layout_width="wrap_content"
  97.     android:layout_height="wrap_content"
  98.     android:layout_x="39dip"
  99.     android:layout_y="332dip"
  100.     android:background="#00000000"
  101.     android:src="@drawable/add_button"/>
  102.   <ImageButton
  103.     android:id="@+id/prev_station_button"
  104.     android:layout_width="wrap_content"
  105.     android:layout_height="wrap_content"
  106.     android:layout_x="108dip"
  107.     android:layout_y="332dip"
  108.     android:background="#00000000"
  109.     android:src="@drawable/prev_station_button"/>
  110.   <ImageButton
  111.     android:id="@+id/search_prev_button"
  112.     android:layout_width="wrap_content"
  113.     android:layout_height="wrap_content"
  114.     android:layout_x="176dip"
  115.     android:layout_y="332dip"
  116.     android:background="#00000000"
  117.     android:src="@drawable/search_prev_button"/>
  118.   <com.way.view.CheckableImageButton
  119.     android:id="@+id/headset_toggle"
  120.     android:layout_width="wrap_content"
  121.     android:layout_height="wrap_content"
  122.     android:layout_x="255dip"
  123.     android:layout_y="332dip"
  124.     android:background="#00000000"/>
  125.   <com.way.view.CheckableImageButton
  126.     android:id="@+id/power_toggle"
  127.     android:layout_width="wrap_content"
  128.     android:layout_height="wrap_content"
  129.     android:layout_x="0dip"
  130.     android:layout_y="403dip"
  131.     android:background="#00000000"/>
  132.   <ImageButton
  133.     android:id="@+id/radio_list_button"
  134.     android:layout_width="wrap_content"
  135.     android:layout_height="wrap_content"
  136.     android:layout_x="80dip"
  137.     android:layout_y="402dip"
  138.     android:background="#00000000"
  139.     android:src="@drawable/radio_list_button"/>
  140.   <ImageButton
  141.     android:id="@+id/next_station_button"
  142.     android:layout_width="wrap_content"
  143.     android:layout_height="wrap_content"
  144.     android:layout_x="146dip"
  145.     android:layout_y="404dip"
  146.     android:background="#00000000"
  147.     android:src="@drawable/next_station_button"/>
  148.   <ImageButton
  149.     android:id="@+id/search_next_button"
  150.     android:layout_width="wrap_content"
  151.     android:layout_height="wrap_content"
  152.     android:layout_x="215dip"
  153.     android:layout_y="402dip"
  154.     android:background="#00000000"
  155.     android:src="@drawable/search_next_button"/>
  156. </AbsoluteLayout>

值得學的幾個手法:LED 區用「禁用/啟用」兩層重疊的背景來切換狀態;DigitalTextView 自訂 View 負責畫出七段數碼風格的時鐘與頻率;TuneWheel 搭配 SeekBar 的 pointer 當調頻刻度;而所有 ImageButton 都把背景設成 #00000000 全透明,只留 src 圖片,讓整塊面板看起來像印刷上去的一樣。_absolute 佈局雖然在新版 Android 已被淘汰,但作為「像素級還原設計稿」的教材,仍很值得一读。

Copyright © Linux教程網 All Rights Reserved