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

Android UI 組件深入:ImageButton 與 ZoomButton 圖文解析

編輯:關於 Android 開發

透視 ImageButton 與 ZoomButton:讓圖片按鈕真正「活」起來

單看 ImageButton 這個名字,很容易以為它就是一個普通的圖片按鈕,把圖片往上一放就能用。但真正動手之後你會發現,事情並沒有想像中那麼美好——想要實現「按下時有反饋」的效果,往往還得額外寫一點代碼,或者在 XML 裡多做一層配置。

值得一提的是,ImageButton 還派生了 ZoomButton 這個子類。Android 系統本身就自帶了 btn_minus 與 btn_plus 兩組 Drawable 資源,只要把它們分別指定給 ZoomButton 的 android:src 屬性,就能快速做出「放大」與「縮小」按鈕。可以說,ZoomButton 只不過是把 src 默認指向了系統資源,並在此基礎上補充了一些控制縮放的方法而已。

此外,Android 還提供了一個 ZoomControls 組件。它本質上繼承自 LinearLayout,會把「放大」「縮小」兩個按鈕水平排列在一起,並且允許你分別為兩個按鈕綁定各自的事件監聽器。

圖片按鈕在正常狀態下的效果:

第二個按鈕被按下時的效果:



要實現圖片按鈕的「按下切換」效果,通常有兩條路可走:一是在 Java 代碼裡處理,二是通過 XML 配置文件來實現。

方式一:在 Java 中為按鈕添加觸摸監聽

  1. ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);
  2. //在代碼中切換按鈕不同狀態下的圖片
  3. btn.setOnTouchListener(new View.OnTouchListener(){
  4. @Override
  5. public boolean onTouch(View v, MotionEvent event) {
  6. if(event.getAction() == MotionEvent.ACTION_DOWN){
  7. //按下時,換上按下狀態的圖片
  8. ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.red));
  9. }else if(event.getAction() == MotionEvent.ACTION_UP){
  10. //抬起時,還原為正常狀態的圖片
  11. ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.purple));
  12. }
  13. return false;
  14. }
  15. });

邏輯並不複雜:手指按下時把按鈕的圖片換成按下態,手指抬起時再換回正常態,這樣一來就獲得了直觀的視覺反饋。

方式二:通過 XML 配置 selector 實現背景切換

首先在 res/drawable 下新建一個 selector 文件(例如 button_selector.xml):

  1. <selector xmlns:android="https://schemas.android.com/apk/res/android">
  2. <item android:state_pressed="true"
  3. android:drawable="@drawable/red" />
  4. <item android:drawable="@drawable/purple" />
  5. </selector>

接著在 main.xml 中引用這個 selector:

  1. <LinearLayout
  2. android:id="@+id/root"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <ImageButton
  7. android:id="@+id/imageButton1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/blue" />
  11. <!-- 按下時顯示不同圖片的按鈕 -->
  12. <ImageButton
  13. android:id="@+id/imageButton2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:background="@drawable/button_selector" />
  17. <LinearLayout
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_gravity="center_horizontal"
  21. android:layout_margin="10dp"
  22. android:orientation="horizontal">
  23. <!-- 兩個 ZoomButton,分別使用系統自帶的 btn_plus 與 btn_minus -->
  24. <ZoomButton
  25. android:id="@+id/zoomButton1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:src="@android:drawable/btn_plus" />
  29. <ZoomButton
  30. android:id="@+id/zoomButton2"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:src="@android:drawable/btn_minus" />
  34. </LinearLayout>
  35. <!-- ZoomControls 組件 -->
  36. <ZoomControls
  37. android:id="@+id/zoomControls1"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_gravity="center_horizontal"
  41. android:orientation="horizontal" />
  42. </LinearLayout>

這裡有一個特別容易踩的坑:如果你採用 XML 的方式來設置按鈕的按下效果,就不要再同時指定 android:src 屬性,否則按下切換的效果會「失效」,圖片不會發生變化。

總的來說,這兩種方案各有利弊:代碼方式靈活可控,XML 方式則更為簡潔直觀。實際開發中,可以根據具體場景自由取捨。

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