Android中的圖片資源





一個圖像資源是能在屏幕上畫(繪製)的圖形的一般概念,你能在API中恢復(getDrawable(int))或者在其他的XML資源中作爲屬性使用,比如 android:drawable 和 android:icon.下面是一些不同類型的圖片:

Bitmap File 位圖文件

一個位圖圖形文件 (.png .jpg .gif) .創建一個BitmapDrawable

Nine-Patch File 九宮格文件
一個具有可拉伸區域的PNG文件,允許圖片基於內容改變大小,創建一個NinePatchDrawable.

Layer List  層列表
一個管理了一組其他圖片的圖片。它們按數組順序繪製,所以最大索引的元素會被繪製在頂部。創建一個 LayerDrawable

State List狀態列表

一個爲不同的狀態引用了不同的位圖的XML文件 (例如當一個按鈕按下時,使用不同的圖片)。創建一個StateListDrawable

Level List
一個XML文件,定義了一個管理一組可選圖片的圖片,每個分配一個最大的數值,創建一個LevelListDrawable

Transition Drawable
一個XML文件,定義了一張圖片能使兩張圖片之間交叉出現,創建一個TransitionDrawable.

Inset Drawable
一個XML文件,定義了一張圖片以一定的距離嵌入另一張圖片。當一個View需要一張比View實際邊界小一點的背景圖片時非常有用。

Clip Drawable  夾 圖
一個XML文件,定義了一張圖片基於當前的圖片級別值夾住其他的圖片。創建一個ClipDrawable

Scale Drawable 比例圖片
一個XML文件,定義了一個圖片,基於它當前的級別值改變其他圖片的大小。創建一個ScaleDrawable

Shape Drawable 形狀圖片
一個XML文件,定義了一個幾何(geometric)形狀,包括顏色和斜率。創建一個ShapeDrawable

可以參考Animation Resource ,看如何創建一個 AnimationDrawable。

注意:一個在XML中的顏色資源也可以被當做drawable資源使用。例如,當創建一個 狀態列表state list drawable時,你能使用 android:drawable 屬性引用顏色資源。(android:drawable="@color/green").


===============================================================================================

Bitmap 位圖
一種位圖圖片。Android支持三種格式的bitmap文件。.png(最好) .jpg (接受)   .gif(不鼓勵)
你可以直接引用一個位圖文件。用文件名作爲資源ID,或者在XML中創建資源ID別名。
Bitmap文件可以在構建過程中使用aapt工具自動地優化進行無損壓縮。例如,一個 真色 的PNG不會超過256種顏色,可以用調色板轉化爲一個8-bit的PNG圖片.這回導致一個等質量的圖片,但要求更少的內存.所以要知道在該目錄中的二進制文件在構建期間可以改變.如果你計劃使用 bit 流來讀取一張圖片以轉化爲一個bitmap ,請放置圖片在 res/raw/ 文件夾,它們不會被優化。

Bitmap File
一個bitmap文件是一個.png .jpg .gif文件。當你將資源保存在 res/drawable/ 目錄時,Android會爲所有的文件創建一個Draeable資源。

 文件位置:
 res/drawable/filename.png (.png, .jpg, 或 .gif)

 編譯的數據類型:
 BitmapDrawable.

 資源引用方式:
 java中: R.drawable.filename
 XML中:@[package:]drawable/filename

 例子:
在佈局中使用:
<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/myimage" />
在代碼中使用:
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);



XML Bitmap

一個XML bitmap 是一個在XML中定義的資源,它指向一個bitmap文件。其效果是原始位圖文件的一個別名。者XML文件爲該bitmap指定了額外的屬性 ,比如dithering and tiling (猶豫不決的 瓦片 貼磚)。
注意:你可以用一個<bitmap>元素作爲一個<item>的子元素。例如,當創建一個狀態列表或層列表時,你在<item>元素中可以不包含android:drawable屬性,嵌入一個<bitmap>,來定義圖片。

 文件位置:
 res/drawable/filename.xml
 編譯的數據類型:
 BitmapDrawable
 資源引用:
 java中: R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    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"] />

 元素:
 xmlns:android
 定義了Android的命名空間,必須是"http://schemas.android.com/apk/res/android". 僅當bitmap是根元素的時候才必要,否者當嵌入<item>使用時,不需要。
 android:src 圖像資源,該屬性必須有,引用到以一個圖片資源
 android:antialias 別名是否可用。
 android:dither
 android:filter bitmap filter是否可用。filter當圖片拖拽、拉伸使它的表面均勻,穩定
 android:gravity
 android:mipMap   指令提示是否可用 the mipmap hint
 android:tileMode 依附模式,當enable時,bitmap 可重複;但Gravity被忽略。
  其值必須是下列常量值之一:disabled  clamp  repeat  mirror

 例如:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon"
    android:tileMode="repeat" />
 
Nine-Patch 九宮格圖片
一個NinePatch是當在視圖View內的內容超過正常的圖片邊界Android會按比例拉伸區域的一張PNG圖片。典型的例子是分配一張圖片作爲某個屬性(寬或高)設置爲wrap-content的View背景,當視圖隨着內容變化時,九宮格圖片也會按比例匹配視圖的大小。作爲一種常用的bitmap圖片,你可以直接引用或從一個定義的XML文件中引用。
怎樣創建具有拉伸區域的Nine-Patch文件,請看2D Graphics 文檔
 文件位置:
 res/drawable/filename.9.png 文件名作爲資源ID

 編譯的數據類型:
 NinePatchDrawable.

 資源引用方式:
 java中: R.drawable.filename
 XML中:@[package:]drawable/filename

 例子:
<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/myninepatch" />

XML Nine-Patch
一個XML Nine-patch是在XML中定義的一種資源,該資源指向一張Nine-Patch圖片,XML能指定Nine-Patch的dither屬性。
 文件位置:
 res/drawable/filename.xml 文件名作爲資源ID

 編譯的數據類型:
 NinePatchDrawable.
  
 資源引用:
 java中: R.drawable.filename
 XML中: @[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:dither=["true" | "false"] />

 xmlns:android    該屬性必須有,且爲"http://schemas.android.com/apk/res/android".
 android:src      該屬性必須有
 例子:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/myninepatch"
    android:dither="false" />

Layer List
一個管理了一組其他圖片的圖片。它們按數組順序繪製,所以最大索引的元素會被繪製在頂部。每張圖片在一個 <layer-list>元素中用一個<item>項表示。

 文件位置:
 res/drawable/filename.xml

 編譯的資源類型:
 LayerDrawable.

 資源引用:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</layer-list>

 元素:
 <layer-list> 必須包含。它是根元素,包含一個或多個<item>元素。
 屬性:
 xmlns:android 值必須是"http://schemas.android.com/apk/res/android".

 <item> 定義放置的圖片資源,必定是<selector> 元素的一個孩子,接受<bitmap>類型的子元素。
 屬性:
 android:drawable 必須包含。引用一個drawable資源
 android:id  一個新的資源ID ,你能用這個標識符,獲取和修改這張圖片,通過View.findViewById()或Activity.findViewById()
 android:top  Integer 頂部偏移的像素大小。
 android:right Integer 右邊偏移的像素大小。
 android:bottom  Integer 底部偏移的像素大小。
 android:left  Integer  左邊偏移的像素大小。

 默認所有的drawable項 會按比例縮放去適應包含視圖View的大小。因此在不同位置在一個層列表中放置圖片可以提升視圖大小和圖片比例的協調性。
 在列表中避免縮放項,可以在<item>元素內部使用<bitmap>元素,並定義gravity屬性,就不能縮放了。
 例子:
 能根據View縮放的:
 <item android:drawable="@drawable/image" />
 不能縮放的:
<item>
  <bitmap android:src="@drawable/image"
          android:gravity="center" />
</item>

 例子:
 文件位置:
 res/drawable/layers.xml 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

 應用到視圖中的方式:

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/layers" />

State List

一個爲不同的狀態引用了不同的位圖的XML文件 (例如當一個按鈕按下時,使用不同的圖片)。你可以在XML文件中描述狀態列表。<selector>中的每一項<item>代表一張圖片。每一個<item>使用各種屬性去描述哪一個狀態應該使用哪一張圖片。注意:非最佳匹配。
 文件位置:
 res/drawable/filename.xml
 編譯的數據類型:
 StateListDrawable.
 資源引用:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>


 例子:
 文件位置:
 res/drawable/button.xml
 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

 佈局文件中使用:
<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />


Level list:

一個Drawable ,管理了一些圖片。每張圖片分配了一個數值。通過用setLevel() 設置圖片的級別值,從加載資源按android:maxLevel屬性較大的,也可以等於該值。

 文件位置:
 res/drawable/filename.xml文件名作爲資源ID
 編譯資源類型:
 LevelListDrawable.

 
 資源引用:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<level-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/drawable_resource"
        android:maxLevel="integer"
        android:minLevel="integer" />
</level-list>

 例子:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/status_off"
        android:maxLevel="0" />
    <item
        android:drawable="@drawable/status_on"
        android:maxLevel="1" />
</level-list>

一旦其應用到一個View,這級別值會被改變,通過 setLevel() or setImageLevel().


Transiton Drawable

一個XML文件,定義了一張圖片能使兩張圖片之間交叉出現,創建一個TransitionDrawable.
 <transition>元素內的每個<item>項代表一張圖片。注意,只有兩張圖片被支持。向前移動,調用startTransition(),向後移動調用reverseTransition()。

 文件位置:
 res/drawable/filename.xml 文件名會作爲資源ID
 編譯的資源類型:
  TransitionDrawable
 資源引用:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename
 
 語法:
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition>

<transition>是根元素,可以包含一個或多個<item>元素

<item>元素 接受<bitmap>作爲子元素

 例子:
 文件位置:
 res/drawable/transition.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>



應用:
<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />

下面的代碼使從第1項到第2項執行500ms的變換
ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);

Inset Drawable
一個XML文件,定義了一張圖片以一定的距離嵌入另一張圖片。當一個View需要一張比View實際邊界小一點的背景圖片時非常有用。

 文件位置:
 res/drawable/filename.xml
 編譯資源的數據類型:
 InsetDrawable.
 資源引用:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:insetTop="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension" /> 

<inset>必須是根元素。注意dimension值

 例子:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/background"
    android:insetTop="10dp"
    android:insetLeft="10dp" />

Clip Drawable
一個XML文件,定義了一張圖片基於當前的圖片級別值夾住其他的圖片。創建一個ClipDrawable

 文件位置:
 res/drawable/filename.xml  文件名作爲資源ID
 編譯資源的數據類型:
 ClipDrawable.

 資源引用方式:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:clipOrientation=["horizontal" | "vertical"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                     "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                     "center" | "fill" | "clip_vertical" | "clip_horizontal"] />


 例子:
 文件位置:
 res/drawable/clip.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/android"
    android:clipOrientation="horizontal"
    android:gravity="left" />


 應用:
<ImageView
    android:id="@+id/image"
    android:background="@drawable/clip"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />


代碼中:
ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);

嵌入值,level默認爲 0 ,圖片完全嵌入,因此圖片不可見。當level值爲10000時,圖片沒有嵌入,將完全可見。


Scale Drawable

 文件位置:
 res/drawable/filename.xml
 編譯資源的數據類型:
 ScaleDrawable

 資源引用方式:
 java中:R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:scaleHeight="percentage"
    android:scaleWidth="percentage" />


 例子:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />


Shape Drawable
在XML中定義的一般形狀。

 文件位置:
 res/drawable/filename.xml

 編譯的資源數據類型:
 GradientDrawable

 資源引用方式:
 java中: R.drawable.filename
 XML中:@[package:]drawable/filename

 語法:
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>


 元素:
 <shape> 根元素
 屬性:
 xmlns:android 必須有,XML的命名空間。"http://schemas.android.com/apk/res/android".
 android:shape 定義形狀類型。可用的值有:
  "rectangle"  長方形  默認值
  "oval"      橢圓形
  "line"    線    水平線,該形狀要求<stroke>元素,定義線的寬度。
  "ring"   圈 環

 下面的屬性僅當android:shape="ring"時起作用:
 android:innerRadius    內半徑
 android:innerRadiusRatio  內半徑比例   內環半徑用環的寬的比例來表示  。例如, android:innerRadiusRatio="5", 那麼內半徑等於將圈的寬度除以5.ndroid:innerRadius使該值無效。默認值爲9
 android:thickness 厚度 環的厚度。
 android:thicknessRatio   環的厚度用環的寬的比例來表示。例如,android:thicknessRatio="2", 那麼環的厚度等於環的寬度除以2。android:innerRadius使該值無效。默認值爲3.
 android:useLevel  如果爲true ,它將作爲一個LevelListDrawable使用。正常應該是false,否則你的行狀將不會出現。
 
 <corners>
 爲行狀創建一個圓角,僅當行狀是長方形rectangle時有用。
 屬性:
 android:radius  所有角的半徑   dimension 型值 它會使下面這些屬性無效:
  android:topLeftRadius
  android:topRightRadius 
  android:bottomLeftRadius
  android:bottomRightRadius
 注意:每個角開始時必須提供一個大於1的角半徑,否者沒有角會被圓角化。如果你指定的角沒有被圓角化,
  一個變通機制會使用android:radius 設置一個大於1的默認半徑。但是 這也會使你設置的其他每個角都無效。提供0dp ,將不會有任何圓角.

 <gradient>
 爲形狀指定一個斜率(梯度)顏色. 
 屬性:
 android:angle   方向(角度)   爲gradient指定一個方向(角度),單位 度.0 是從左到右 ;90 從下到上 ;它必須是45度的倍數,默認值爲0.
 android:centerX  gradient的中心相對於X軸的位置,值從(0-1.0)
 android:centerY  gradient的中心相對於Y軸的位置,值從(0-1.0)
 
 android:centerColor  Color 在開始和結束顏色之間的可選顏色,使用十六進制值或顏色資源.
 
 android:endColor Color 結束時的顏色.使用十六進制值或顏色資源
 android:gradientRadius   gradient的半徑,僅當android:type="radial"時使用.
 android:startColor Color 開始時的顏色 使用十六進制值或顏色資源
 android:type gradient使用的類型 ,可用的值如下:
   "linear"   線性gradient,默認值
   "radial"   輻射狀(星狀)的gradient ,開始顏色start color 是中心顏色center color
   "sweep"    移動線gradient
 android:useLevel  如果爲true,將作爲LevelListDrawable使用.

 <padding>
 屬性:
 android:left
 android:top
 android:right
 android:bottom
 


 <size>
 形狀的大小
 屬性:
 android:height
 android:width
 注意:默認,形狀按容器View的大小比例縮放,是與這兒定義的尺寸相稱的.當你在ImageView中使用該shape時,你應該嚴格設置android:scaleType 爲"center".

 <solid>
 填充該形狀的實心顏色.
 屬性:
 android:color  Color  應用到該形狀的顏色.十六進制或顏色資源.


 <stroke>
 該形狀的一條斜線.
 屬性:
 android:width  Dimension 線的厚度   一個Dimension值 或Dimension資源
 android:color  Color      線的顏色 一個十六進制值或顏色資源
 android:dashGap  Dimension      線破折號之間的距離   一個Dimension值 或Dimension資源.僅當android:dashWidth 設置時可用.
 android:dashWidth Dimension  每個破折號線的大小   一個Dimension值 或Dimension資源  .僅當android:dashGap 設置時可用.
 

 例子:
 文件位置:
 res/drawable/gradient_box.xml
 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>


 在XML文件中使用:
<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

 在代碼中應用:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

本文爲對android開發文檔的翻譯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章