1.概述
在Android工程开发中设计软件UI,我们会大量使用外部的资源,例如图片、图标、颜色、动画、自定义属性、数组、音频视频等等。使用AndroidStudio环境开发时,资源文件被集中放置在res目录下,res目录是在main目录下,与代码java目录同级的文件夹。当建立一个新工程时,AndroidStudio会直接帮助我们在res目录下建立目录drawable(图片、图标)、layout(布局)、mipmap(图片)、values(数值)。
mipmap分为mipmap-mdpi、mipmap-hdpi、mipmap-xhdpi、mipmap-xxhdpi、mipmap-xxxhdpi目录,mipmap-mdpi目录下的图片适配在480*320屏幕下适用。
values目录下存在colors、dimens、strings、styles文件,对应存放颜色、尺寸、字符串、自定义风格资源。
博客中UI资源一系列博文主要讲解,这些目录下设置文件在何处使用?如何建立?如何修改?
2.Drawable资源
drawable目录下可以放置两类文件,一种是图片资源 png位图,一种是xml绘制脚本文件。在drawable目录下的图片资源不需要区分适配屏幕,Android系统会依据使用屏幕自动配置清晰图片的尺寸,这个不是本节的重点,会放在mipmap中再讲解。
drawable目录下的xml文件是生产文件的格式,可以利用文件生成多种Drawable实例(矢量图)。包括Layer-List、State-List、Level-List、Transition-Drawable、Inset-Drawable、Clip-Drawable、Scale-Drawable、Shape-Drawable,其中Shape-Drawable, State-Drawable是我们今后会经常用到的Drawable。
在代码中利用资源类 Resources 对绘制脚本文件引用,例如
Drawable myDraw =
(Drawale)getResources().getDrawable(R.drawable.shape);
或者在布局中直接引用该drawable文件,例如
android:background ="@drawable/shape"
2.1 ShapeDrawable
创建一个 ShapeDrawable实例,可以生成一个图形边框或者某个形状的色块,多用于控件或布局background和src设置,较常用场景有背景设置圆弧边框、设置消息脚标、设置按钮背景。使用ShapeDrawable实现一些形状图形, 或则颜色渐变效果, 相比PNG图片, 占用空间更小; 相比自定义View, 实现起来更加简单。
先上一个简单的例子,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1858d6"/>
<corners android:radius="10dp"/>
<stroke android:color="#000000"
android:width="2dp"/>
<padding android:bottom="20dp"
android:left="15dp"
android:right="15dp"
android:top="30dp"/>
</shape>
这是一个蓝色背景,有弧度,有上下左右内边距,有边框的长方形背景。
<shape/> 标签中含有以下设置标签。
<?xml version="1.0" encoding="utf-8"?>
<!--rectangle 长方形 /默认-->
<!--oval 椭圆-->
<!--line 线-->
<!--ring 环形-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--corners标签: 圆角-->
<!--bottomLeftRadius 左下角-->
<!--bottomRightRadius 右下角-->
<!--topLeftRadius 左上角-->
<!--topRightRadius 右上角-->
<!--radius 是四个角, 设置了这个就不需要设置上面的四个了, PS:它的优先级比较低, 会被其他参数覆盖-->
<corners android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip"
android:radius="5dip"
android:topLeftRadius="5dip"
android:topRightRadius="5dip"/>
<!--gradient标签: 简单的说: 让图形变成一个有颜色梯度的-->
<!--angle 是颜色变换的角度, 默认是0, 取值必须是45的 倍数. 0: 是颜色从左边到右边, 90: 是颜色从底部到顶部, -->
<!--startColor centerColor endColor 一起使用: 开始的颜色, 中间的颜色, 结束的颜色-->
<!--centerX centerY是指定位置坐标, 取值是0.0f ~ 1.0f 之间, 例如: android:centerX="0.5f" 表示X方向的中间位置-->
<!--type 颜色渐变的类型, 取值类型有三种: linear/radial/sweep -->
<!--linear 线性变化, 就是颜色从左往右, 从下往上-->
<!--radial 放射变化, 例如: 从一个圆中心到圆的边缘变化-->
<!--sweep 扫描式渐变, 类似雷达扫描的那种图形-->
<!--gradientRadius 和android:type="radial"一起连用, 半径-->
<gradient
android:angle="0"
android:centerColor="#000"
android:centerX="0.5"
android:centerY="0.5"
android:endColor="#FFF"
android:gradientRadius="20dip"
android:startColor="#000"
android:type="linear"
android:useLevel="true"/>
<!--padding标签: 这里的padding是控件中间内容与shape图形图片的距离-->
<paddings
android:bottom="5dip"
android:left="5dip"
android:right="5dip"
android:top="15dip"/>
<!--size标签 shape图形的宽度和高度 这里一般不用设置, 它的优先级没有控件的优先级大, 他指定控件的宽高就好, shape图形会随控件拉伸-->
<size
android:width="50dip"
android:height="10dip"/>
<!--solid标签: shape图形背景色-->
<!--PS: 这个和上面的gradient标签会互斥, 一个是设置背景色, 一个是设置渐变色, 你懂得-->
<solid
android:color="@android:color/white"/>
<!--stroke标签: 边框-->
<!--width 边框的宽度-->
<!--color 边框的颜色-->
<!--下面两个参数是 把边框变成虚线用-->
<!--dashGap 虚线中空格的长度-->
<!--dashWidth 虚线中实线的长度-->
<stroke
android:width="5dip"
android:color="#0000FF"
android:dashGap="2dip"
android:dashWidth="1dip"/>
</shape>