Android-自己定義映像資源的使用(1)

來源:互聯網
上載者:User

標籤:res   布局   興趣   ace   load   soft   左右   src   使用   

Android-自己定義映像資源的使用
2014年4月28日 周一 天氣晴朗 心情平靜
本篇博文給大家介紹一下,在Android開發中經經常使用到的一些映像資源,具體內容麻煩請各位認真查看官網。下面附上一個連結:http://developer.android.com/guide/topics/resources/drawable-resource.html,本篇部落客要給出使用示範範例,讓童鞋們對這些映像資源有個直觀的瞭解。代碼資源:http://download.csdn.net/detail/wwj_748/7263481有興趣的朋友能夠加本人建立的群,裡面有豐富的學習資源哦: 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>

例如以下:

圖層資源的使用圖層資源非常easy理解。就相似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>

例如以下:

本篇部落格就介紹這幾種映像資源。下篇部落格繼續介紹,不想讓各位一口吃掉一個胖子。



Android-自己定義映像資源的使用(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.