如何在 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.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="512"
   android:viewportHeight="512">
   <path
      android:fillColor="#D80027"
      android:pathData="M512,198.525l-176.891,-25.704L256,12.53l-79.108,160.291L0,
                        198.525l128,124.769L97.784,499.47L256,416.291l158.217,
                        83.179l-30.219,-176.176L512,198.525zM256,376.749l-111.731,
                        58.74l21.338,-124.415l-90.393,-88.111l124.92,-18.152L256,
                        91.613l55.867,113.198l124.918,18.152l-90.395,88.111l21.34,
                        124.415L256,376.749z" />
</vector>

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

點選 此處 下載專案程式碼

更新日期:27-6 月-2020

1K+ 檢視次數

開啟您的職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.