如何在 Android 應用中建立水波動畫?


在開始編寫程式碼之前,我們應該瞭解 Android 中的水波動畫是什麼。水波動畫就像波浪的動量。在 Android 中,它透過背景屬性出現在 TextView、Button 等檢視上。

此示例演示瞭如何將水波動畫整合到檢視中。

步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml 中。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <Button
      android:id="@+id/result"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Result Data"
      android:background="@drawable/rippple"
      android:textSize="20sp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

在上面的程式碼中,我們添加了一個按鈕,其背景為水波效果。

步驟 3 - 在 drawable 中建立一個名為 ripple.xml 的檔案,並新增以下程式碼

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:color="#f816a463"
   tools:targetApi="lollipop">
   <item android:id="@android:id/mask">
      <shape android:shape="rectangle">
         <solid android:color="#f816a463" />
      </shape>
   </item>
</ripple>

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟您的一個專案活動檔案,然後單擊工具欄中的執行  圖示。

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕

現在點選結果資料,它將顯示如下所示的結果 -

點選 這裡 下載專案程式碼

更新於: 2019年7月30日

489 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.