Android-總結Drawable用法

今天爲了適配啓動頁背景圖,接觸到了BitmapDrawable保證了啓動頁的背景圖不變形。想一想以前真的沒用過,因此仍是有必要詳細瞭解一下Drawable,至少在遇到一些問題時能夠及時的找到解決方案。android

Drawable的分類

Drawable通常都是用XML定義的,可是咱們也能夠自定義Drawable,可是代碼會比較複雜。bash

BitmapDrawablw

能夠理解爲它是一張帶規則的圖片,通常狀況下若是能夠在ImageView裏面設置屬性的就不必使用,可是若是一張圖片要作到像放在控件裏適配就有必要使用了,例如啓動頁背景圖佈局

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="clip_horizontal|clip_vertical"
    android:dither="true"
    android:src="@drawable/splash_bg"
    android:tileMode="disabled"/>
    <!--屬性解釋-->
    <!--android:src 圖片資源id-->
    <!--android:antialias是否開啓抗鋸齒,應該開啓-->
    <!--android:dither是否開啓抖動效果,應該開啓-->
    <!--android:filter是否開啓過濾效果,應該開啓-->
    <!--android:gravity對圖片進行定位-->
    <!--android:mipMap紋理映射,通常爲false-->
    <!--android:tileMode平鋪模式四個值:disabled(默認值即關閉平鋪模式)、clamp、repeat、mirror-->
複製代碼

若是直接使用圖片,因爲不能控制圖片的位置,有些手機會變形很嚴重。學習

NinePatchDrawable

與BitmapDrawable相似不過NinePatchDrawable的效果相似於.9圖,實際使用時其實直接使用.9圖就好動畫

ShapeDrawable

開發時用的比較多,能夠理解爲只有純顏色或漸變色的圖片。只是有些地方須要注意一下。ui

以上代碼是一個指定漸變百分比的漸變shape,其中centerY與centerColor必須同時設置才能生效,不然只是普通的漸變,另外,其實咱們的系統進度條也能夠設置漸變spa

<ProgressBar
             style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:max="1000"
                android:progress="0"
                android:progressDrawable="@drawable/progress_green" />

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<!--progress_green-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="50dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/progress_shape"
            android:scaleWidth="100%" />
    </item>
</layer-list>
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<!--progress_shape-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:endColor="@color/theme_color"
        android:startColor="@color/theme_color1"
        android:type="linear" />
</shape>
複製代碼

進度條max的值因爲沒有小數,因此設置1000效果會好不少,加上動畫效果後效果以下.net

-c

LayerDrawable

對應的標籤是layer-list,表示一種層次化的Drawable。不少時候均可以使用佈局嵌套完成,可是使用layor-list能夠減小布局的層級結構,例如3d

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <stroke android:width="1dp" android:color="#45DAA8" />
        </shape>
    </item>

    <item>
        <bitmap android:src="@drawable/gou"
            android:gravity="bottom|right" />
    </item>
</layer-list>
複製代碼

效果圖code

StateListDrawble

這個開發中也常常用到,標籤對應的是selector。好比咱們的點擊效果、還有選擇後與選擇前的不一樣效果,使用selector能夠在代碼中設置一個屬性改變很方便。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:drawable="@drawable/bg_btn_circle_gradient_full">
    </item>

    <item android:state_enabled="false">

        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="#aaffffff">
            <item>
                <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
                    <corners android:radius="360dp" />
                    <solid
                        android:color="@color/gray_eeeeee"
                        />
                </shape>
            </item>
        </ripple>

    </item>
</selector>
複製代碼

這裏結合了ripple標籤,能夠作出水波紋效果。效果以下

LevelListDrawable

也是一個Drawable的集合,對應的標籤是level-list。有點相似於layer-list,不過level-list是根據等級(0~10000)來顯示的,而layer-list則是顯示在一塊兒的。開發中比較少用到,若是一個按鈕有多種狀態能夠考慮使用代替if-else判斷顯示

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_heat" android:maxLevel="1"/>
    <item android:drawable="@drawable/icon_cool" android:maxLevel="2"/>
</level-list>
複製代碼
imageview.getDrawable().setLevel(1);
複製代碼

TransitionDrawable

能夠實現兩個Drawable之間的淡入淡出,對應的標籤是transition。

ScaleDrawable

對應的標籤是scale,能夠根據level指定Drawable縮放到必定比例。

這兩個效果我以爲能夠在啓動頁的背景圖作一點動畫效果或者作全局的動畫,其餘地方可使用動畫代替。

InsetDrawable

對應的標籤是inset,能夠把Drawable內嵌進來,能夠用於加大點擊區域。

ClipDrawable

對應的標籤是clip,能夠根據level裁剪出另一個drawabl。挺有用的一個Drawable能夠看看下面的博客 效果圖:

-c

blog.csdn.net/briblue/art…

自定義Drawable

其實Drawable是一個抽象的概念,能夠理解爲Canvas以前的一個封裝。就是把資源封裝成Drawable再繪製到畫布上。Drawable能夠說是內存級的畫布。

過程與自定義View時的繪畫是類似的,具體也上面ClipDrawable的博客。

參考資料

《Android開發藝術探索》


最開始堅持的地方,記錄學習與生活的點點滴滴
相關文章
相關標籤/搜索