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

Android Drawable 全解析:六種常見派生類的用法

編輯:關於 Android 程式設計

一、Drawable 簡介

Android 中的 Drawable 種類繁多。它可以是一張位圖,也可以是由顏色和形狀構造出來的圖形,因此有時候它並沒有明確的「尺寸」概念。本篇會重點講解以下幾種常用的派生類:

  • BitmapDrawable
  • LayerDrawable
  • LevelListDrawable
  • TransitionDrawable
  • InsetDrawable
  • ClipDrawable

二、BitmapDrawable

這是最常見的一種,我們平時引用一張 png 作為背景時,系統實際上就是把它包裝成了 BitmapDrawable。它也可以通過 XML 描述,新建 bitmapdrawble.xml:

<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_launcher"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:tileMode="mirror" />

各屬性含義如下:

  • src:指定所用的資源圖片;
  • dither:是否開啟抖動效果。當手機屏幕的像素配置與圖片本身的像素配置不一致時,開啟它可以讓系統自動調整顯示效果,緩解渐变區域的色帶;
  • antialias:是否啟用抗鋸齒;
  • filter:是否開啟位圖濾鏡,縮放時能讓圖像更平滑;
  • tileMode:圖片的平鋪模式,共四種取值——mirror 表示鏡像平鋪,repeat 表示重複平鋪,clamp 表示邊緣拉伸,disable 表示使用默認樣式。

把這個 bitmap 作為主布局的背景來使用:

<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:background="@layout/bitmap"
    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.sunshine.bitmapdrawble.MainActivity">
</RelativeLayout>

tileMode 設為 mirror 時的效果:

tileMode 設為 repeat 時的效果:

tileMode 設為 clamp 時的效果:

三、LayerDrawable

LayerDrawable 是一種分層的 Drawable,會按照 item 的聲明順序依次繪製,後繪製的圖形疊在上方。寫一個 bitmap.xml:

<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/image01" />
    </item>
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/image02" />
    </item>
</layer-list>

item 上的 left/top 用來控制該層相對父容器的偏移。在主布局中定義一個 ImageView 並引用它:

<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.sunshine.bitmapdrawble.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bitmap" />
</RelativeLayout>

效果如下圖:

可以看到兩張圖片同時呈現:image01 位於最底層,image02 疊加在它之上。

四、LevelListDrawable

LevelListDrawable 是分級的 Drawable,核心玩法是通過 setImageLevel() 改變控件的 level 值,從而切換顯示的圖片。先建立 level.xml:

<level-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/image01"
        android:minLevel="0"
        android:maxLevel="10" />
    <item
        android:drawable="@drawable/image02"
        android:minLevel="11"
        android:maxLevel="20" />
</level-list>

規則很直白:當控件的 level 落在哪個區間,就顯示對應的那張圖。上面這份配置表示 level 在 0~10 之間顯示 image01,在 11~20 之間顯示 image02。

把它應用到主布局的 ImageView 上,並放兩個按鈕做切換:

<LinearLayout 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:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/level" />

    <Button
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClik"
        android:text="第一張圖片" />

    <Button
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClik"
        android:text="第二張圖片" />
</LinearLayout>

Activity 中的代碼:

private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.iv);
    iv.setImageLevel(8);
}

public void onClik(View v) {
    switch (v.getId()) {
        case R.id.one:
            iv.setImageLevel(8);
            break;
        case R.id.two:
            iv.setImageLevel(18);
            break;
        default:
            break;
    }
}

點擊第一個按鈕顯示第一張圖,點擊第二個按鈕顯示第二張圖:

五、TransitionDrawable

TransitionDrawable 用於實現兩個 Drawable 之間的淡入淡出(交叉淡化)效果。定義 transion.xml:

<transition xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/image01" />
    <item android:drawable="@drawable/image02" />
</transition>

應用到 ImageView 上:

<ImageView
    android:id="@+id/iv"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/transion" />

Activity 中通過 startTransition() 觸發切換,參數是過渡時長(毫秒):

private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.iv);
}

public void dochange() {
    TransitionDrawable drawable = (TransitionDrawable) iv.getDrawable();
    drawable.startTransition(2000);
}

public void onClik(View v) {
    switch (v.getId()) {
        case R.id.one:
            dochange();
            break;
    }
}

六、ClipDrawable

ClipDrawable 顧名思義,是對 Drawable 做裁剪。定義 clip.xml:

<clip xmlns:android="https://schemas.android.com/apk/res/android"
    android:drawable="@drawable/image01"
    android:clipOrientation="horizontal"
    android:gravity="left" />

其中 clipOrientation 指定裁剪方向,可取 horizontal 或 vertical;gravity 則決定從哪一側開始裁剪。

<ImageView
    android:id="@+id/iv"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/clip" />

Activity 中通過 setLevel() 控制裁剪比例:

iv = (ImageView) findViewById(R.id.iv);
ClipDrawable clip = (ClipDrawable) iv.getDrawable();
clip.setLevel(5000);

level 的取值範圍是 0~10000:0 表示完全裁剪(什麼都不顯示),10000 表示完全不裁剪,5000 則正好顯示一半。利用這個特性配合進度條,可以很輕鬆地做出自定義的進度展示效果。

以上就是這幾種 Drawable 的典型用法。

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