如何在 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>

在上述程式碼中,我們使用了影像檢視並添加了背景作為 background.xml。

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

<?xml version="1.0" encoding="utf-8"?>
<vector
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:autoMirrored="true"
   android:height="24dp"
   android:viewportHeight="492.719"
   android:viewportWidth="492.719"
   android:width="24dp" >
   <path android:fillColor="#D80027"
      android:pathData="M492.719,166.008c0,-73.486 -59.573,-133.056 -133.059,-133.056c-47.985,
                        0 -89.891,25.484 -113.302,63.569c-23.408,-38.085 -65.332,-63.569 -113.316,
                        -63.569C59.556,32.952 0,92.522 0,166.008c0,40.009 17.729,75.803 45.671,
                        100.178l188.545,188.553c3.22,3.22 7.587,5.029 12.142,5.029c4.555,
                        0 8.922,-1.809 12.142,-5.029l188.545,-188.553C474.988,241.811 492.719,
                        206.017 492.719,166.008z"/>

</vector>

讓我們嘗試執行應用程式。我假設你已經將你的實際 Android 移動裝置連線到了電腦。若要從 Android studio 執行應用程式,請開啟一個專案的活動檔案並從工具欄中單擊“執行  ”圖示。選擇你的移動裝置作為選項,然後檢查將顯示預設螢幕的移動裝置 –

單擊 此處 下載專案程式碼

更新於: 2020 年 6 月 27 日

532 次瀏覽

職業生涯起步

完成課程並獲得認證

開始
廣告
© . All rights reserved.