編輯:Android 程式設計入門
Drawable 有很多種,它們都表達一種「圖像」的概念,但並非全部都是圖片。根據 Google Android API 的定義:
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
大致意思是:Drawable 是一種圖像概念的抽象。通常你會把它當成一種能在屏幕上顯示的資源類型來處理,Drawable 類提供了一個通用的 API 來處理各種不同形式的圖像資源。與 View 不同,Drawable 不能接收事件,也不能與用戶交互。
下面介紹幾種常見的 Drawable。
BitmapDrawable 幾乎是最簡單的一種,它表示一張圖片。開發中我們通常直接引用圖片資源,例如 R.drawable.image(drawable 目錄下有 image.jpg 或 image.png)。也可以用 XML 來描述:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
android:src="@mipmap/ic_launcher"
android:antialias="true|false"
android:dither="true|false"
android:filter="true|false"
android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
android:mipMap="true|false"
android:tileMode="disabled|clamp|repeat|mirror"/>
這是很常見的一種 Drawable,通常通過編寫 XML 創建,因此略顯複雜。它通常用顏色來構建圖形,既可以是純色,也可以具有漸變效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle|oval|line|ring">
<corners android:bottomLeftRadius="integer" android:bottomRightRadius="integer"
android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" />
<gradient android:angle="integer" android:centerColor="integer" android:centerX="integer"
android:centerY="integer" android:endColor="color" android:gradientRadius="integer"
android:startColor="color" android:type="linear|radial|sweep" android:useLevel="true|false" />
<padding android:bottom="integer" android:left="integer" android:right="integer" android:top="integer" />
<size android:width="integer" android:height="integer" />
<solid android:color="color" />
<stroke android:width="integer" android:color="color" android:dashGap="integer" android:dashWidth="integer" />
</shape>
它是一種層次化的 Drawable,在 <layer-list> 結點下有多個 <item>,後面的 <item> 會疊加在前面的 <item> 之上。下面是一個文本輸入框的例子:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#0ac39e" />
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
<item android:bottom="1dp" android:left="1dp" android:right="1dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
StateListDrawable 對應 <selector> 標簽,這個大家應該比較熟悉——我們常給 Button 設置一個 selector。它表示 Drawable 的集合,集合中的每個 Drawable 都對應 View 的一種狀態,系統會根據 View 的當前狀態選擇相應的 Drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black"/><!-- 默認狀態 -->
<item android:state_focused="true" android:drawable="@android:color/holo_orange_dark"/><!-- 獲取焦點 -->
<item android:state_pressed="true" android:drawable="@android:color/holo_red_dark"/><!-- 被點擊 -->
</selector>
以上幾種 Drawable 覆蓋了日常開發中最常見的圖形資源形式,理解它們各自的用法,能讓界面定制更加得心應手。
動手實現 Android 圖片輪播控件
1. 輪播控件的組成部分 我們以知乎日報 Android 客戶端的輪播控件為例,拆解一下它的主要組成。 首先,我們需要一個用來顯示圖片的 View 對象。觀察上圖底部中
Android 補間動畫 TweenAnimation 用法整理
補間動畫(Tween Animation)是 Android 最直覺的動畫方案:你只要描述「從哪個狀態變到哪個狀態、花多久時間」,系統就會自動補上中間的過渡影格。以下把程
安卓六大基礎佈局詳解:線性、相對、表格、網格、絕對與框架
佈局檔(layout)決定了畫面的骨架。Android 內建的六大基礎佈局各有適用場景,選對容器往往比寫一堆樣式屬性更省事。 1. 線性佈局(LinearLayout)
Android Intent與Intent Filter完整解析:元件溝通的核心機制
什麼是Intent? 在Android中,Intent是一種用來描述「要做什麼」的抽象訊息。它可以用來啟動Activity、發送廣播、啟動或綁定Service。Inten