Android學習筆記:Android資源總結

來源:互聯網
上載者:User

Android應用資源可分為兩大類:

1、無法直接存取的原生資源,儲存在asset目錄下,應用程式可以使用AssetManager來訪問這些資源

2、可以通過R資源清單類訪問的資源,儲存在res目錄下

 

先來看原生資源,指的就是跟android無任何關係可以獨立存在的檔案,在android項目中經常放置的位置有:asset目錄、/res/raw/目錄、/res/xml/目錄(該目錄只放xml)。

擷取這些地方的原聲資源的方法:

asset:

AssetManager am = getAssets();    //擷取該應用的AssetManagerAssetFileDescriptor afd = am.openFd("shot.mp3");    //擷取指定檔案對應的AssetFileDescriptorMediaPlayer mediaPlayer = new MediaPlayer();mediaPlayer.setDataSource(afd.getFileDescriptor());    //使用MediaPlayer載入指定的音效檔

 

/res/raw/目錄:

Android SDK會在R清單類中為該目錄下的資源產生一個索引項目

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.bomb);

或者 java代碼中通過調用Resources的openRawResource(int id)方法來擷取該資源的二進位輸入資料流。

 

/res/xml/目錄:

通過getXml(int id)擷取XmlResourceParser,通過XmlResourceParser對該xml進行pull解析

也可以通過openRawResource(int id)擷取對應的輸入資料流進行操作。

 

看完了原生資源之後,接下來我們分析一下Android應用資源,存放的路徑為/res/路徑下的各個子目錄

非原生資源中的資源根據標籤又可以分為兩類:resource資源和非resrouce資源

其中,resource資源中存放各種簡單的xml檔案,包括字串值、整數值、顏色值、數組等一個簡單的常量。

而非resource資源套件括很多種,他們和resource的區別就是這一類檔案可以說是一個組合的效果,是帶有邏輯在裡面的,不像resource資源就一簡單常量。

resource資源套件括:string、color、array、style、dimen、boolean、integer...

非resource資源套件括:anim、color、drawable、menu(這裡的color和上面的color的區別是此處的color檔案不是一個些簡單的常量組合成,而是帶有邏輯關係)

 

resource資源很簡單,所有的父標籤統一是<resource>,只是不同的資源對應的子標籤不同,統一的格式如下:

<resource>    <子標籤名 name="xxx">xxx</子標籤名>    ......    ......</resource>

array資源的每個子標籤下面還有一些<item>標籤,作為數組的值。

 

下面我們關鍵來看非resource資源:

AnimationDrawable,該資源可放置在/res/anim/路徑下。

動畫資源的父標籤<set>,有四個子標籤可以指定:<alpha>、<scale>、<translate>、<rotate>,在指定rotate的時候要設定pivotX和pivotY為變換的“中心點”

使用也非常簡單:

Animation anim = AnimationUtils.loadAnimation(context,R.anim.my_anim);

StateListDrawable,用於組織多個Drawable對象,當使用該資源的時候,StateListDrawable對象所顯示的Drawable對象會隨著目標組件狀態的改變而自動切換

該資源的父標籤<selector>,可以包含多個子標籤<item>,該子標籤可以指定如下屬性:

1、android:color或者android:drawable

2、android:state_xxx,指定一個特殊狀態

<selector xmlns:android=http://schemas.android.com/apk/res/android>    <item android:color="hex_color" android:state_pressed=["true" | "false"]/></selector>

LayerDrawable,可以包含一個Drawable數組,系統會按這些Drawable對象的數組順序來繪製他們,索引最大的Drawable對象將會被繪製在最上面

該資源的父標籤<layer-list>,可以包含多個子標籤<item>,該子標籤可以指定如下屬性:

1、android:drawable

2、android:id

3、android:buttom | top | left | right,用於指定一個長度值,用於指定將該Drawable對象繪製到目標組件的指定位置。

<layer-list xmlns:android=http://schemas.android.com/apk/res/android>    <item android:id="@android:id/background" androdi:drawable="@drawable/grow"/>    ......    <item>        <bitmap android:src="@drawable/icon" android:gravity="center"/>    </item>    ....</layer-list>

ShapeDrawable,用於定義一個基本的幾何圖形的形狀和屬性

該資源的父標籤<shape>,可以指定屬性:android:shape=["rectangle" | "oval" | "line" | "ring"],可以包含多個子標籤<corners>、<gradient>、<padding>、<size>、<solid>、<stroke>,該子標籤可以指定如下屬性:

定義ShapeDrawable對象的完整文法格式如下:

<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:useslevel=["true" |"false"]/>    <!-- 定義幾何形狀的內邊框 -->    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer"/>      <!-- 定義幾何圖形的大小 -->    <size        android:width="integer"        android:color="color"        android:dashWidth="integer"        android:dashGap="integer"/>    <!-- 定義使用單種顏色填充 -->    <solid        android:color="color"/>    <!-- 定義為幾何圖形繪製邊框 -->    <stroke        android:width="integer"        android:color="color“        android:dashWidth="integer"        android:dashGap="integer"/></shape>

ClipDrawable資源,代表從其他為圖上截取的一個“圖片片斷”,使用標籤<clip>可以指定如下屬性:

1、android:drawable,指定截取的源drawable對象

2、android:clipOrientation,指定截取方向

3、android:gravity,指定截取時對齊

使用ClipDrawable對象時,可調用setLevel(int level)方法來設定截取的地區大小,當level為0時,截取的圖片片斷為空白;當level為10000時,截取整張圖片

 

菜單資源,通常放在/res/menu/目錄下,父標籤為<menu>,且無需指定任何屬性,可以包含如下子標籤:

1、<item>

    a、android:id

    b、android:title

    c、android:icon

    d、android:alphabeticShortCut,為功能表項目指定字元快速鍵

    e、android:numericShortCut,為功能表項目指定數字快速鍵

    f、android:checkable,設定該功能表項目是否可選

    g、android:checked,設定該功能表項目是否已選中

    h、android:visible

    i、android:enable

2、<group>,可以包含多個<item>

3、checkableBehavior指定菜單的選擇行為,可選擇none、all、single

4、visible

5、enable

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.