Android Drawable Resource學習(二)、BitmapDrawable和Bitmap


一、如何創建一個BitmapDrawable對象

Bitmap,代表一個位圖圖像,Android支持三種格式的位圖圖像:.png (preferred),.jpg (acceptable), .gif (discouraged)。

括號裏的說明,代表這三種格式的圖片在Android中的支持情況,.png格式圖片優先,.jpg格式也可以,但是效果沒有.png好,.gif支持最差。

可以直接使用圖片的名稱作爲資源ID,來直接引用一個位圖圖片。也可以再XML文件中創建一個資源別名的ID。

在構建應用的時候,Bitmap文件可能會被appt工具壓縮自動優化爲無損圖像。例如,一個真彩色PNG,不需要超過256的顏色可以被轉換成一個8位PNG和調色板。這將導致一個圖像質量相同,但這需要更少的內存。所以要意識到,在drawable目錄中圖像的二進制文件在構建程序時可以改變。如果你打算讀一個圖像作爲字節流並將它轉換成一個位圖,把你的圖片放在在res /raw/文件夾裏,在那裏他們不會被優化。

1、通過Bitmap File

一個bitmap文件就是一個.png、.jpg,.gif格式的文件。Android會對存儲在res/drawable/目錄下的這些文件創建一個Drawable資源。

文件位置:

       res/drawable/filename.png (.png.jpg, or .gif)    文件名即資源的ID。

編譯資源數據類型:
             指向BitmapDrawable類型的指針。
資源引用:

In Java: R.drawable.filename
In XML: @[package:]drawable/filename
示例:

有這樣存儲的圖片 res/drawable/myimage.png,在layout xml文件中將他顯示在視圖上。

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


在java代碼中檢索出爲一個Drawable對象。

            
[java] view plaincopy
  1. Resources res=getResources();  
  2. Drawable drawable = res.getDrawable(R.drawable.<span class="atv">myimage</span>);  
  3. //實際上這是一個BitmapDrawable對象  
  4. BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;  
  5. //可以在調用getBitmap方法,得到這個位圖  
  6. Bitmap bitmap=bitmapDrawable.getBitmap();  
參考:

2、通過XML Bitmap

一個XML bitmap是一個在XML文件中定義的指向一個bitmap文件的資源。其效果是作爲一個原始位圖文件的別名,並且可以指定一些額外的屬性。

注意:你可以在<item>節點中使用<bitmap>作爲它的子節點。比如,當你定義一個state list或者layer list的時候,可以包括一個android:drawable屬性

Note: You can use a <bitmap> element as a child of an<item> element. Forexample, when creating astate list orlayer list,you can exclude theandroid:drawableattribute from an<item> element and nest a<bitmap> inside it that defines the drawable item.

文件位置:
res/drawable/filename.xmlfilename作爲資源的ID
編譯資源類型
指向BitmapDrawable類型的指針
資源引用
In Java: R.drawable.filenameIn 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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />



節點介紹:
<bitmap>
定義位圖的來源和屬性

屬性:

xmlns:android
類型:String。定義了XML的命名空間,必須是"http://schemas.android.com/apk/res/android"。如果<bitmap>是根元素,那麼他是必須的,如果是嵌套在<itme>裏面,那麼就不是必須的。
android:src
類型:Drawable resource。必需。 引用一個drawableresource.
android:antialias
類型:Boolean。是否開啓抗鋸齒。
android:dither
類型:Boolean。如果位圖與屏幕的像素配置不同時,是否允許抖動.(例如:一個位圖的像素設置是 ARGB 8888,但屏幕的設置是RGB 565)
android:filter
類型:Boolean。是否允許對位圖進行濾波。對位圖進行收縮或者延展使用濾波可以獲得平滑的外觀效果。
android:gravity
類型:關鍵字。定義位圖的重力(gravity),如果位圖小於其容器,使用重力指明在何處繪製

必需是下面的屬性,多個之間用  |  分隔

Value Description
top Put the object at the top of its container, not changing its size.
bottom Put the object at the bottom of its container, not changing its size.
left Put the object at the left edge of its container, not changing its size.
right Put the object at the right edge of its container, not changing its size.
center_vertical Place object in the vertical center of its container, not changing its size.
fill_vertical Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal Place object in the horizontal center of its container, not changing its size.
fill_horizontal Grow the horizontal size of the object if needed so it completely fills its container.
center Place the object in the center of its container in both the vertical and horizontal axis, notchanging its size.
fill Grow the horizontal and vertical size of the object if needed so it completely fills itscontainer. This is the default.
clip_vertical Additional option that can be set to have the top and/or bottom edges of the child clipped toits container's bounds. The clip is based on the vertical gravity: a top gravity clips thebottom edge, a bottom gravity clips the top edge, and neither clips both edges.
clip_horizontal Additional option that can be set to have the left and/or right edges of the child clipped toits container's bounds. The clip is based on the horizontal gravity: a left gravity clipsthe right edge, a right gravity clips the left edge, and neither clips both edges.
android:tileMode
類型:Keyword
定義了tile模式。當tile模式被啓用,位圖是重複的,並且gravity屬性將被忽略。必須是下列之一常量值:
Value Description
disabled Do not tile the bitmap. This is the default value.
clamp Replicates the edge color if the shader draws outside of its original bounds
repeat Repeats the shader's image horizontally and vertically.
mirror Repeats the shader's image horizontally and vertically, alternating mirror images so thatadjacent images always seam.

示例:

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"   android:src="@drawable/icon"    android:tileMode="repeat" />


參考:

二、BitmapDrawable的使用

一個BitmapDrawable就是封裝了一個位圖。直接以文件的方式,就是封裝了一個原始的位圖。以Xml方式,可以對原始的位圖進行一系列的處理,比如說抗鋸齒,拉伸,對齊等等。

要了解BitmapDrawable的使用,還需要明白Bitmap、BitmapFactory等類。Bitmap代表了一個原始的位圖,並且可以對位圖進行一系列的變換操作。BitmapFactory提供一系列的方法用於產生一個Bitmap對象。多用在Canvas中。

關於繪圖和位圖變換以後再學習。BitmapDrawable的使用比較簡單,就是在其他的xml文件中直接引用就可以了,不過要注意在xml中定義BitmapDrawable各個屬性使用和含義。



轉自:http://blog.csdn.net/lonelyroamer/article/details/8147542

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