Android之Drawable介紹

Drawable

Drawable是一種可以在Canvas上進行繪製的抽象的概念,顏色、圖片等都可以是一個Drawable。

Drawable可以通過XML定義,或者通過代碼創建。

Android中Drawable是一個抽象類,每個具體的Drawable都是其子類。

簡單來講,其可以理解爲:圖像。

它不全是圖片,通過顏色也可以構造出各種各樣的圖片效果,它一般就是當做View的背景使用,有兩種方式,一種是通過XML,一種是通過代碼的方式。

Drawable的分類

主要包括:


BitmapDrawable

表示一種圖片,可以直接引用原始圖片或者通過XML進行描述

例如:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@color/colorPrimary"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:gravity="center"
    android:mipMap="false"
    android:tileMode="disabled"
    />

Bitmap的屬性介紹:

屬性 作用 備註
android:src 圖片資源 ID
android:antialias 圖片抗鋸齒-圖片平滑,清晰度降低 應該開啓
android:dither 開啓抖動效果-用於高質量圖片在低質量屏幕上保存較好的顯示效果(不會失真) 應該開啓
android:filter 開啓過濾-在圖片尺寸拉伸和壓縮時保持較好的顯示效果 應該開啓
android:gravity 圖片小於容器尺寸時,對圖片進行定位-選項之間 用‘ ’來組合使用
android:mipMap 紋理映射-圖像處理技術 默認false
android:tileMode 平鋪模式-repeat單純重複、mirror鏡面反射、clamp圖片四周像素擴散 默認disable關閉
NinePatchDrawable

自動根據寬高進行縮放且不會失真

實際使用,可以直接引用圖片或者通過XML描述。

實例:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@color/colorPrimary"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:gravity="center"
    android:mipMap="false"
    android:tileMode="disabled"
    />
ShapeDrawable

ShapeDrawable通過顏色構造的圖形、,可以是純色的圖形,也可以是有漸變效果的圖形。

shape標籤創建的Drawable實體是GradientDrawable

實例:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"/>
    <gradient
        android:angle="45"
        android:centerX="30"
        android:centerY="30"
        android:centerColor="@color/colorAccent"
        android:endColor="@color/colorPrimary"
        android:startColor="@color/colorPrimaryDark"
        android:gradientRadius="20"
        android:type="linear"
        android:useLevel="true" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
    <size
        android:width="200dp"
        android:height="200dp" />
    <solid
        android:color="@color/colorPrimary"/>
    <stroke
        android:width="10dp"
        android:color="@color/colorAccent"
        android:dashWidth="5dp"
        android:dashGap="3dp"/>

</shape>

屬性介紹:

屬性/標籤 作用 備註
android:shape 圖形的形狀:rectangle矩形、oval橢圓、line橫線、ring圓環 corners標籤對應於矩形;line和ring通過stroke指定線的寬度和顏色; ring圓環有五個特殊的shape屬性
corners標籤 四個角的角度 -
gradient標籤 漸變效果-android:angle表示漸變角度,必須爲45的倍數 android:type指明漸變類型:linear線性,radial徑向、sweep掃描
solid標籤 純色填充 與gradient標籤排斥
stroke標籤 描邊 有描邊線和虛線
size標籤 表示shape的固有大小,並非最終顯示的大小 沒有時getIntrinsicWidth返回-1;能指明Drawable的固有寬高,但如果作爲View背景還是會被拉伸
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章