Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學習之 Drawable 詳解(一)

編輯: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

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"/>

ShapeDrawable

這是很常見的一種 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>
  • android:shape:表示圖形形狀——rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環),默認矩形。line 和 ring 必須有 <stroke> 標簽指定寬度和顏色,否則達不到預期效果。
  • <gradient>:漸變效果,與 <solid> 互斥。
  • <solid>:純色填充,通過 android:color 指定 shape 的顏色。

LayerDrawable

它是一種層次化的 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

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 覆蓋了日常開發中最常見的圖形資源形式,理解它們各自的用法,能讓界面定制更加得心應手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved