《Android 開發藝術探索》
《官方文檔》
http://www.javashuo.com/article/p-ofyhrlzz-go.html
http://www.runoob.com/w3cnote/android-tutorial-drawable1.htmlhtml
首先學習一個東西,先找找最權威的解釋,固然是官方文檔了,我先先來看看官方文檔是如何描述Drawable的.android
Provides classes to manage a variety of visual elements that are intended for display only, such as bitmaps and gradients. These elements are often used by widgets as background images or simply as indicators (for example, a volume level indicator).
提供一些類來管理一系列的僅用來顯示的可視化元素,好比bitmap和漸變. 這些元素常常被控件用做背景或者簡單地做爲指示器(Indicator)(好比說,音量指示器)。
You can create most of these drawables using XML, as described in Drawable Resources.
你能夠經過xml在Drawable目錄下聲明大多數的Drawableide
如今咱們對Drawable有了一個大概的認識,即Drawable時一種可視化資源,好比說簡單的顏色,圖片,Shape等,同時Drawable也是一個類,可是這個類的實例能夠經過Xml文件的形式來建立。Drawable類是一個抽象類,位於 android.graphics.drawable
包下,全部的Drawable都繼承於它。經過合理的使用Drawable咱們能夠減小對圖片的依賴,減少APK的體積。學習
Drawable能夠經過代碼定義,也能夠經過Xml文件定義,可是代碼定義比較繁瑣也不利於複用,因此咱們通常習慣經過XML文件來定義Drawable。ui
Google 提供了多種Drawable,接下來咱們一一學習它們。google
ColorDrawable僅僅表明的是一種顏色,因此也比較簡單。設計
<?xml version="1.0" encoding="utf-8"?> <color xmlns:android="http://schemas.android.com/apk/res/android" android:color="#FFF" />
經過代碼定義3d
var colorDrawable = ColorDrawable(0xFFF)
GradientDrawable,表明的是一個漸變色,會與 Shape
結合着用,Shape定義形狀 GradientDrawable
定義漸變顏色.XML標籤是 gradient
.code
漸變類型的圖例cdn
angle的圖例
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="0" android:centerColor="#5F3f" android:centerX="0.2" android:centerY="0.2" android:endColor="#000" android:startColor="#FFF" android:type="linear" /> </shape>
BitmapDrawable就是一個Bitmap,使用的時候直接引用Bitmap便可,可是經過使用XML來描述Bitmap能夠設置更多的效果.BitmapDrawable的屬性以下:
屬性 | 描述 |
---|---|
scr | 圖片資源,該屬性是必須設置的 |
antialias | 是否開啓抗鋸齒,開啓後會讓圖片變的平滑,建議開啓 |
dither | 抖動效果,當高質量的圖片顯示在低質量的屏幕上的時候,不會過於失真,建議開啓 |
filter | 過濾效果,當圖片被壓縮/拉伸的時候保存較好的顯式效果 |
gravity | 當圖片小於容器的大小的時候,經過此屬性能夠爲圖片定位 |
tileMode | 平鋪模式,設置了此值的話gravity會失效分爲 [disable] [clamp] [repeat] [mirro] |
mipMap | 紋理映射,不經常使用,默認值爲false,我也沒搞懂 |
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:antialias="true" android:dither="true" android:filter="true" android:src="@drawable/dog" />
能夠用來描述一個 .9圖
,我的感受比較雞肋,由於AS的.9圖設計器很是好使,這裏就很少說了,我想我幾乎是不會使用XML來製做.9圖的。你們若是感興趣,能夠去學習一下。
我想這個Drawable是最經常使用的Drawable了,用來定義各類形狀。下面是ShapeDrawable的屬性:
上面的思惟導圖中列出的,ShapeDrawable的屬性,咱們來看一看這些屬性的詳細信息.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring">
shape 屬性定義的ShapeDrawable的形狀,其中有4個值 oval(橢圓形)
line(線形)
rectangle(正方形)
ring(環形)
。
單色填充形狀,須要注意的是,此屬性和 gradient 屬性是衝突的,只能生效一個。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFF" /> </shape>
這一屬性在上面已經介紹過,這裏就不在贅述。
設置圓角,能夠四個角統一設置,也能夠分別設置。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="45dp" /> </shape>
padding 內邊距,這個屬性比較奇怪,雖然它定義在Shape中,可是它表明的是View的內容與View的邊距l。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#000" /> <padding android:left="10dp" android:top="20dp" /> </shape>
效果圖以下:
size 表明的是Shape的__固有大小__,可是通常來講,經過size設置的大小並非shape的最終的顯式大小,在這裏咱們須要瞭解一下固有大小的概念,Drawable類有兩個方法 getIntrinsicWidth
和 getIntrinsicHeight
來獲取Drawable的固有寬高,對於BitmapDrawable來講,固有寬高表明的是Bitmap的寬和高。而Shape是默認沒有固有寬高的(默認值爲-1),可是若是經過了size屬性爲Shape設置了寬和高,那麼Shape就有了固有寬高,可是這僅僅表明的是ShapeDrawable的固有寬高,可是若是Shape做爲背景的話是不受固有寬高影響的依然會根據View的大小拉伸或壓縮。
stroke表明的是Shape的邊框.
<stroke android:width="1dp" android:color="#FFF" android:dashGap="1dp" android:dashWidth="3dp" />
屬性解釋:
這一片文章,咱們主要學習了5個Drawable,其中Shape是最重要的。接線來的文章中,咱們還會繼續學習其餘的Drawable直到學完系統的13種Drawable.