如何在 Android 中繪製閉合形狀?


 本例演示瞭如何在 Android 中繪製閉合形狀。

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   android:layout_marginTop="30dp"
   tools:context=".MainActivity">
   <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@drawable/background"/>
</LinearLayout>

在上面的程式碼中,我們已新增 imageview 並將 background 新增為 background.xml。

步驟 3 − 將以下程式碼新增到 drawable/ background.xml 中

<vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:width="24dp"
   android:height="24dp"
   android:autoMirrored="true"
   android:viewportWidth="612"
   android:viewportHeight="612">
   <path
      android:fillColor="#FF000000"
      android:pathData="M612,306C612,137.004 474.995,0 306,0C137.004,0 0,137.004 0,
                        306c0,168.995 137.004,306 306,306C474.995,612 612,474.995 612,
                        306zM168.3,424.032L286.333,306L168.3,187.967l19.667,-19.667L306,
                        286.333L424.032,168.3l19.668,19.667L325.667,306L443.7,424.032L424.032,
                        443.7L306,325.667L187.967,443.7L168.3,424.032z" />
</vector>

讓我們嘗試執行你的應用程式。假設你已將實際的 Android 移動裝置與你的計算機相連。若要從 Android Studio 執行應用程式,請開啟其中一個專案的活動檔案並單擊工具欄中的執行  圖示。選擇你的移動裝置作為選項,然後檢查你的移動裝置,它將顯示你的預設螢幕 –

單擊 此處 下載專案程式碼

更新於: 2020 年 6 月 27 日

338 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.