1. 程式人生 > >Android-自定義影象資源的使用(1)

Android-自定義影象資源的使用(1)

Android-自定義影象資源的使用

2014年4月28日 週一 天氣晴朗 心情平靜有興趣的朋友可以加本人建立的群,裡面有豐富的學習資源哦:299402133(移動開發狂熱者群)
Android中有以下幾種影象資源:
  • 普通影象資源
  • XML影象資源
  • Nine-patch影象資源
  • XML Nine-patch影象資源
  • 圖層(Layer)影象資源
  • 影象狀態(state)資源
  • 影象級別(Level)資源
  • 淡入淡出(transition)資源
  • 嵌入(Inset)影象資源
  • 剪下(Clip)影象資源
  • 比例(Scale)影象資源
  • 外形(Shape)影象資源
好,上面就是提供的一些影象資源了,我們可以自定義這些影象資源供給我們程式使用,讓我們的程式更加好看。下面小巫花點時間逐個給大家介紹一下這些影象資源的使用方法:

普通影象資源的使用

普通影象資源就只是應用一張圖片而已,不需要自己定義如下:/05_KindOfDrawableUse/res/layout/simple_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

效果圖: 那張圖片是小巫公司的logo,http://www.teamtopgame.com/,這是官網,喜歡玩網遊的童鞋這個可以玩一下。

xml影象資源的使用

這個影象資源是使用<bitmap>標籤的,這個標籤下有很多屬性,如下:
<?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"] />

這裡我不會給大家一個個介紹是什麼意思,希望童鞋們自己去官網檢視。/05_KindOfDrawableUse/res/layout/xml_res.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/logo"
    android:tileMode="repeat" >

</bitmap>

這裡用到一張圖片,設定平鋪模式為重複:

Nine-patch 影象資源的使用(重要)

.9圖片老生常談了,做Android開發的沒用過這個工具那就太說不過去的,我們在應用開發當中,時刻需要對圖片進行處理,為了讓圖片被拉伸的時候不會變形和扭曲,讓圖片邊緣部分過渡得更加平滑自然。這就是draw9patch.bat這個工具的作用。D:\software\adt-bundle-windows-x86_64-20131030\sdk\tools
在SDK中的tools目錄下,就有Android提供的各種工具,童鞋們自己學著去使用吧,這個工具的使用這裡小巫就不講解了,需要學習的可以參考其他博主寫的博文,百度、Google常伴你左右。/05_KindOfDrawableUse/res/layout/ninepatch_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/res" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/res" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/nine_patch" />

</LinearLayout>
效果圖如下:

XML Nine-Patch 影象資源的使用

這個資源,小巫沒怎麼用過,具體使用方法:在drawable目錄下,定義以下資源/05_KindOfDrawableUse/res/drawable/xml_ninepatch.xml
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="false"
    android:src="@drawable/nine_patch" >

</nine-patch>
這個資源的src是一張.9圖片,不能使用普通的圖片,不然會報錯的哦。在佈局檔案中使用:/05_KindOfDrawableUse/res/layout/xml_ninepatch_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/xml_ninepatch" />

</LinearLayout>

效果圖如下:

圖層資源的使用

圖層資源很容易理解,就類似FrameLayout,我們知道幀佈局都是一層一層往上覆蓋的,對吧。圖層資源也是這樣子滴,在drawable目錄下,定義以下資源:/05_KindOfDrawableUse/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:gravity="center"
            android:src="@drawable/logo" />
    </item>
    <item
        android:left="10dp"
        android:top="10dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/logo" />
    </item>
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/logo" />
    </item>

</layer-list>

/05_KindOfDrawableUse/res/layout/layer_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

效果圖如下:
本篇部落格就介紹這幾種影象資源,下篇部落格繼續介紹,不想讓各位一口吃掉一個胖子。