Android-自定义图像资源的使用(1)

来源:互联网 发布:剑三道长男神脸数据 编辑:程序博客网 时间:2024/05/24 04:07

【转载】http://blog.csdn.net/wwj_748/article/details/24619491#t1


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
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/logo" />  
  11.   
  12. </LinearLayout>  

效果图:
那张图片是小巫公司的logo,http://www.teamtopgame.com/,这是官网,喜欢玩网游的童鞋这个可以玩一下。

xml图像资源的使用

这个图像资源是使用<bitmap>标签的,这个标签下有很多属性,如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <bitmap  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:src="@[package:]drawable/drawable_resource"  
  5.     android:antialias=["true" | "false"]  
  6.     android:dither=["true" | "false"]  
  7.     android:filter=["true" | "false"]  
  8.     android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |  
  9.                       "fill_vertical" | "center_horizontal" | "fill_horizontal" |  
  10.                       "center" | "fill" | "clip_vertical" | "clip_horizontal"]  
  11.     android:mipMap=["true" | "false"]  
  12.     android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />  

这里我不会给大家一个个介绍是什么意思,希望童鞋们自己去官网查看。
/05_KindOfDrawableUse/res/layout/xml_res.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:src="@drawable/logo"  
  4.     android:tileMode="repeat" >  
  5.   
  6. </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
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="@drawable/res" />  
  11.   
  12.     <ImageView  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="@drawable/res" />  
  16.   
  17.     <ImageView  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:background="@drawable/nine_patch" />  
  21.   
  22. </LinearLayout>  
效果图如下:



XML Nine-Patch 图像资源的使用

这个资源,小巫没怎么用过,具体使用方法:
在drawable目录下,定义以下资源
/05_KindOfDrawableUse/res/drawable/xml_ninepatch.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <nine-patch xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:dither="false"  
  4.     android:src="@drawable/nine_patch" >  
  5.   
  6. </nine-patch>  
这个资源的src是一张.9图片,不能使用普通的图片,不然会报错的哦。

在布局文件中使用:
/05_KindOfDrawableUse/res/layout/xml_ninepatch_res.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="@drawable/xml_ninepatch" />  
  11.   
  12. </LinearLayout>  

效果图如下:


图层资源的使用

图层资源很容易理解,就类似FrameLayout,我们知道帧布局都是一层一层往上覆盖的,对吧。图层资源也是这样子滴,在drawable目录下,定义以下资源:
/05_KindOfDrawableUse/res/drawable/layers.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <item>  
  5.         <bitmap  
  6.             android:gravity="center"  
  7.             android:src="@drawable/logo" />  
  8.     </item>  
  9.     <item  
  10.         android:left="10dp"  
  11.         android:top="10dp">  
  12.         <bitmap  
  13.             android:gravity="center"  
  14.             android:src="@drawable/logo" />  
  15.     </item>  
  16.     <item  
  17.         android:left="20dp"  
  18.         android:top="20dp">  
  19.         <bitmap  
  20.             android:gravity="center"  
  21.             android:src="@drawable/logo" />  
  22.     </item>  
  23.   
  24. </layer-list>  

/05_KindOfDrawableUse/res/layout/layer_res.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/layers" />  
  11.   
  12. </LinearLayout>  

效果图如下:


本篇博客就介绍这几种图像资源,下篇博客继续介绍,不想让各位一口吃掉一个胖子。

0 0
原创粉丝点击