歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 自定義Button 及Animation的基本使用

Android 自定義Button 及Animation的基本使用

日期:2017/3/1 10:29:19   编辑:Linux編程

在Android開發中常用組件的使用是必不可少的,但是常用組件用來用去也就那麼幾種,滿足不了開發者對應用界面的要求,更滿足不了消費者對商業應用美觀,大方,時尚的要求,所以說學會自定義各種組件十分必要。

本例簡單的自定義了一個Button並結合了四個簡單animation進行展示,當點擊Start按鈕時,四個Button會按照不同的Animation進行運動:

下面我們先看看怎麼實現自定義Button:

首先是布局文件,就是一個AbsoluteLayoutli裡面有五個Button,其中四個是一列的自定義的Button,另一個是系統自帶的Button用於啟動綁定到四個Button上面的Animation。

Android 自定義Button 及Animation的基本使用源碼下載:

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

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

具體下載目錄在 /2012年資料/2月/25日/Android 自定義Button 及Animation的基本使用/

這是單個自定義Button的聲明:

  1. <Button
  2. android:id="@+id/bt1"
  3. android:layout_x="0dp"
  4. android:layout_y="100dp"
  5. android:text="@string/bt1"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:background="@drawable/bt_define" />

咋一看其實並沒什麼不一樣,好像就比平常多了一個background,其實關鍵是在這個background上面

這需要在res文件夾裡新建一個drawable文件件,再在drawable文件夾裡新建一個bt_define.xml文件:

裡面的內容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/bt" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>
  4. <item android:drawable="@drawable/bt_bg" android:state_enabled="true" android:state_pressed="true"/>
  5. <item android:drawable="@drawable/bt_bg" android:state_checked="true" android:state_enabled="true"/>
  6. <item android:drawable="@drawable/bt"/>
  7. </selector>

這相當與一個聲明文件,就是當這Button正常顯示時背景就是bt,但當它被按下,或者獲取焦點的時候就背景就變成了bt_bg,這樣給以用戶才有被按下的感覺。如果單單只有一個圖片作為Button的background時,是沒有按下的效果的這是bt這是bt_bg,這只是一個簡單的定義而已,還有更復雜,更有趣的需要自己去探索了

好了接著再簡單地綁定animation吧

還是首先在res文件夾裡建一個anim的文件夾,再在裡面建幾個xml文件:

分別為淡入淡出效果的,移動效果的,旋轉效果的和縮放效果的

Copyright © Linux教程網 All Rights Reserved