編輯:關於 Android 開發
系統內建的 ProgressBar 預設是橫向長條,若想要一個轉個不停的環形載入動畫,其實不必急著寫自定義 View。把 shape 標籤寫成 ring,再外包一層 rotate,接上一個 style,就能得到乾淨俐落的圓形進度條。
在 drawable 目錄新增一個 XML 資源檔(New → Drawable resource file),把預設的根標籤 selector 改成 shape,再用 rotate 在外層包一層,讓漸層顏色沿著圓周掃過:
<?xml version="1.0" encoding="utf-8"?>
<!-- 把 shape 嵌套在 rotate 裡面,實現環形旋轉 -->
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="1080.0">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="9"
android:useLevel="false">
<gradient
android:startColor="#ffffff"
android:endColor="#4ec5ff"
android:type="sweep" />
</shape>
</rotate>
兩個關鍵點:gradient 的 type 設成 sweep(掃描式漸層),顏色才會繞著圓心鋪開;rotate 的 fromDegrees 與 toDegrees 差距越大,動畫跑道越長,這裡寫到 1080 就是讓它多繞幾圈再停。
<style name="ring">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminateDrawable">@drawable/buttonringstyle</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ly.blogtest.MainActivity">
<ProgressBar
style="@style/ring"
android:id="@+id/progressBar" />
</RelativeLayout>
只要 indeterminateDrawable 指向剛剛那張環形圖,ProgressBar 就會自動轉起來,一行 Java 都不用寫。
<shape>
<!-- 實心 -->
<solid android:color="#ff9d77"/>
<!-- 漸變 -->
<gradient
android:startColor="#ff8c00"
android:endColor="#FFFFFF"
android:angle="270" />
<!-- 描邊 -->
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<!-- 圓角 -->
<corners android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
solid:填色,只有一個 android:color。
padding:內容與邊界之間的內間距,可用 left、right、top、bottom 分別指定。
gradient:漸層填色,分三種類型。
corners:圓角,只對 rectangle 有效。radius 是全域設定,會被下面四個單角設定覆寫:topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius。半徑開大(例如 200dp)就會變成弧形邊。
stroke:描邊。color 是顏色,width 是寬度,dashWidth 與 dashGap 則用來畫虛線——前者是實線段長度,後者是段與段之間的間距。
把 Eclipse 專案搬進 Android Studio:直接匯入與間接轉換兩種做法
從 Eclipse 過渡到 Android Studio 時,舊專案總得想辦法帶過去。實務上有兩條路:一是讓 Android Studio 直接吃下 Eclipse 專案
把 Android 專案打成 jar,並把依賴的 jar 一起合併進去
前言:關於 .jar 檔案 日常開發中經常會用到第三方的 .jar。其實 .jar 就是一個類似 .zip 的壓縮包,裡面裝的是程式碼——注意它不包含資源檔(res、圖片
Android 模仿社群貼文:多圖選擇、預覽與刪除互動
Android 模仿社群貼文的多圖上傳界面 發貼文或報修反饋頁面通常需要文字輸入與多張圖片選擇。以下以 GridView 搭配 SimpleAdapter,說明如何做出可
GitHub 上的 Android UI 靈感庫:開源介面設計參考
為什麼要參考開源 UI 專案 Android 開發入門容易,但要做出細節完整、互動自然的介面並不容易。GitHub 上有許多 UI 聚合專案,收錄大量開源元件與效果範例,