Android API Guides---Drawable Resources

来源:互联网 发布:老虎证券 财报数据 编辑:程序博客网 时间:2024/04/29 06:08
绘图资源
也可以看看
2D图形
可绘制资源是可以被绘制到屏幕上,哪些是你可以用的API,如getDrawable(INT)检索或应用到另一个XML资源与属性,比如Android的图形一般概念:绘制和android:图标。有几种不同类型的可绘制的:


位图文件
位图图形文件(巴纽,JPG格式,或.gif)。创建一个BitmapDrawable。
九宫格文件
与伸缩区域的PNG文件,使图像大小调整基于内容(.9.png)。创建一个NinePatchDrawable。
层列表
被拉伸,管理等可绘制的数组。这些绘制在阵列顺序,因此具有最大索引的元件在上面绘制。创建一个LayerDrawable。
国家名单
引用不同的位图图形的不同状态的XML文件(例如,使用不同的图像时,按下一个按钮)。创建一个StateListDrawable。
级别列表
定义用于管理若干备用可绘的可拉伸的XML文件,每个分配的最大数值。创建一个LevelListDrawable。
过渡绘制对象
定义可以淡入淡出2绘图资源之间绘制的XML文件。创建一个TransitionDrawable。
插图绘制对象
它定义了镶石按指定距离的另一绘制的可绘制的XML文件。当需要查看背景drawble比视图的实际范围较小,这非常有用。
剪辑绘制对象
它定义了一个绘制一个XML文件,该文件夹在此基础上绘制对象的当前电平值另一个可绘制。创建一个ClipDrawable。
规模可绘制
定义改变另一个可绘制的是根据它的电流电平值的大小被拉伸的XML文件。创建一个ScaleDrawable
形状绘制对象
限定了几何形状,其中包括颜色和渐变的XML文件。创建一个ShapeDrawable。
也看到了如何创建AnimationDrawable动画资源文件。


注意:颜色资源也可以用来作为使用XML的绘制。例如,创建一个状态列表绘制时,可以参考为Android颜色资源:可绘制属性(机器人:可绘制=“@色/绿”)。


位图


位图图像。 Android支持三种格式的位图文件:png格式(首选),.JPG(接受),.GIF(灰心)。


您可以直接引用的位图文件,使用文件名作为资源ID,或创建XML别名资源ID。


注:位图文件,可以通过在构建过程中AAPT工具无损图像压缩自动优化。例如,一个真​​彩色的PNG,不需要超过256种颜色可以用颜色调色板转换成一个8位的PNG。这将导致相等质量的图像,但它需要较少的存储器英寸所以,要知道,放在这个目录下的图像二进制文件可以在生成过程中发生改变。如果你计划,以将其转换为位图读取图像作为比特流,把你的照片在res /生/文件夹而不是,在那里他们将不会被优化。


位图文件


位图文件是为.png,.jpg或.gif文件。 Android的创建这些文件,当您保存在res /绘制/目录下的可绘制资源。


文件位置:
RES /绘制/ filename.png(.PNG,.JPG或.gif)
文件名被用作资源ID。
编译的资源数据类型:
资源指针BitmapDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
例:

随着保存在RES /绘制/ myimage.png的图像,这种布局XML应用图像视图:

<ImageView    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:src="@drawable/myimage" />
下面的应用程序代码检索图像作为绘制对象:

Resources res = getResources();Drawable drawable = res.getDrawable(R.drawable.myimage);
也可以看看:
2D图形
位图绘制对象
XML位图
一个XML位图是XML定义的资源指向位图文件。效果为原始位图文件的别名。该XML可以指定位图的附加属性,如抖动和瓷砖。
注意:您可以使用<位>元素作为<item>元素的一个孩子。例如,创建一个状态列表或图层列表时,可以排除了android:从它里面的<item>元素和嵌套<位图>定义绘制项目可绘制属性。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针BitmapDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?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"] />
ELEMENTS:
<bitmap>
定义位图的源和它的属性。
属性:


xmlns:android
串。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。这是必需的仅在<位图>是根元素,它在<位图>嵌套一个的<item>内时,没有必要的。
android:src
绘制资源。需要。参照绘制资源。
android:antialias
布尔。启用或禁用抗锯齿。
android:dither
布尔。启用或禁用抖动位图,如果位图不具有相同的像素结构的屏幕(例如:一个ARGB 8888位图与一个RGB 565屏幕)。
android:filter
布尔。启用或禁用位图筛选。当位图收缩或伸展平滑的外观过滤使用。
android:gravity

关键词。定义该位图的严重性。重力表示,其中以定位可绘在其容器如果位图是比容器更小。
必须是一个或多个(由分隔'|')以下的恒定值:

ValueDescriptiontop放对象在其容器的顶部,不改变其大小。bottom放对象在其容器的底部,不改变其大小left其容器的z左边,不改放对象在变其大小right其容器的z右边,不改放对象在变其大小center_vertical放置在其容器的垂直中心对象,不改变其大小。fill_vertical如果需要的话,以便完全填满其容器生长的物体的垂直尺寸。center_horizontal放置在其容器的水平中心对象,不改变其大小fill_horizontal如果需要的话,以便完全填满其容器生长的物体的水平尺寸。center放置在其容器中的垂直和水平轴两个中心的对象,不改变其大小。fill如果需要的话,以便完全填满其容器成长对象的水平和垂直尺寸。这是默认的。clip_vertical可以设置附加的选项,使顶部和/或儿童的底部边缘夹在其容器边界。夹子基于垂直重力:顶部重力夹子的底部边缘,一个底重力夹子的顶部边缘,并且既不剪辑两边缘。clip_horizontal可以设置附加的选项,使左和/或儿童的右边缘夹在其容器边界。夹子基于水平比重:左重力剪辑的右边缘,右重力夹子的左边缘,并且既不剪辑两边缘。android:mipMap
布尔。启用或禁用的mipmap提示。见setHas MIPMAP()了解更多信息。默认值是假的。
android:tileMode
关键词。定义平铺模式。当启用瓦模式,该位图被重复。启用平铺模式时,重力会被忽略。
必须是以下常数值之一:

ValueDescriptiondisabled不平铺的位图。这是默认值。clamp复制边缘颜色,如果着色器绘制其原有的边界外repeat水平和垂直方向重复着色器的图像。mirror重复渲染图像的水平水平和垂直方向重复着色器的图像.ly和垂直方向上,交替镜像,使相邻图像总是缝。
例:

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@drawable/icon"    android:tileMode="repeat" />
也可以看看:
BitmapDrawable
创建别名资源
九宫格
九补丁是一个PNG图像,其中您可以定义当视图中含量超过正常范围的图像缩放的Andr​​oid伸缩区域。您通常指定此类型的图像为具有至少一个维度设定为“WRAP_CONTENT”视图的背景,并且当查看增长以适应内容,九宫图像也缩放以匹配视图的大小。一个例子利用九宫图像是Android的标准按钮组件所使用的背景,这必须拉伸以适应按钮中的文本(或图像)。
同与正常的位图,你可以直接或通过XML定义的资源引用九宫文件。
有关如何创建具有拉伸区域的九宫文件的完整讨论,请参阅2D图形文件。
九宫格文件
文件位置:
RES /绘制/ filename.9.png
文件名被用作资源ID。
编译的资源数据类型:
资源指针NinePatchDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
例:
随着保存在RES /绘制/ myninepatch.9.png的图像,这种布局XML应用九宫到View:

<Button    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:background="@drawable/myninepatch" />
也可以看看:
2D图形
NinePatchDrawable
XML Ninepatch
一个XML九宫格是XML指向一个九宫文件中定义的资源。该XML可以指定抖动的图像。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针NinePatchDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><nine-patch    xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@[package:]drawable/drawable_resource"    android:dither=["true" | "false"] />
内容:
<nine-patch>
定义九宫格来源和它的属性。
属性:


xmlns:android
串。 需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:src
绘制资源。 需要。参照九宫文件。
机器人:抖动
布尔。启用或禁用抖动位图,如果位图不具有相同的像素结构的屏幕(例如:一个ARGB8888位图与一个RGB565屏幕)。
例:

<?xml version="1.0" encoding="utf-8"?><nine-patch xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@drawable/myninepatch"    android:dither="false" />
层列表


一个LayerDrawable是管理其他可绘制阵列的可绘制对象。列表中的每个可绘在列表中的列表的最后一个可拉伸的顺序绘制绘制在顶部。


每个绘制由单个<层列表>元素中的<item>元素表示。


文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针LayerDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

?xml version="1.0" encoding="utf-8"?><layer-list    xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@[package:]drawable/drawable_resource"        android:id="@[+][package:]id/resource_name"        android:top="dimension"        android:right="dimension"        android:bottom="dimension"        android:left="dimension" /></layer-list>
ELEMENTS:
<layer-list>
需要。这必须是根元素。包含一个或多个<item>元素。
属性:


xmlns:android
串。需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
<item>
定义绘制的层绘制到另一个地方,在它的属性定义的位置。必须用<选择>元素的子元素。接受子<位图>元素。
属性:


android:drawable
绘制资源。需要。参照绘制资源。
android:id
资源ID。为此绘制一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:“@ + ID /名称”。加符号表示,本应作为一个新的ID来创建。您可以使用此标识来检索和修改与View.findViewById()或Activity.findViewById()的绘制。
android:top
整数。顶部的像素偏移。
android:right
整数。正确的像素偏移量。
android:bottom
整数。底部的像素偏移。
android:left
整数。在像素,左偏移。
所有的绘制项目缩放到适合包含视图的大小,默认情况下。因此,在不同位置放置图像在图层列表可能会增加视图的大小和一些图像缩放为宜。为了避免在列表中缩放项目,使用<位图>元素中的<item>元素中指定绘制并定义重力的东西,不结垢,如“中心”。例如,下面的<item>定义进行缩放以适应其容器查看一个项目:

<item android:drawable="@drawable/image" />
为了避免结垢,下面的示例使用<bitmap>元素为中心的比重:

<item>  <bitmap android:src="@drawable/image"          android:gravity="center" /></item>
例:
在保存RES /绘制/ layers.xml XML文件:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>      <bitmap android:src="@drawable/android_red"        android:gravity="center" />    </item>    <item android:top="10dp" android:left="10dp">      <bitmap android:src="@drawable/android_green"        android:gravity="center" />    </item>    <item android:top="20dp" android:left="20dp">      <bitmap android:src="@drawable/android_blue"        android:gravity="center" />    </item></layer-list>
请注意,此示例使用嵌套的<位图>元素与“中心”重力定义每个项目的绘制资源。这确保了没有图像被缩放到适合容器的大小,由于由偏移的图像调整大小而引起的。


这种布局XML应用绘制到View:

<ImageView    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:src="@drawable/layers" />
结果是日益偏移图像的堆栈:

也可以看看:
LayerDrawable
国家名单
一个StateListDrawable是使用几种不同的图像来表示相同的图形,根据对象的状态中的XML定义的可绘制的对象。例如,一个按钮控件可以在几种不同的状态(压,突出重点,或两者都不是),并使用状态列表绘制的一个存在,你可以为每个国家不同的背景图片。
你可以这样描述XML文件的状态列表。每个图形由单个<选择>元素中的<item>元素表示。每个<item>使用不同的属性来描述,其中它应该被用来作为图形为可绘制的状态。
在每个状态变化,所述状态列表遍历从上到下和相匹配的当前状态下使用,该选择并非基于所述的第一项“最佳匹配”,而仅仅是一个满足该状态的最低标准的第一项。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针StateListDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"    android:constantSize=["true" | "false"]    android:dither=["true" | "false"]    android:variablePadding=["true" | "false"] >    <item        android:drawable="@[package:]drawable/drawable_resource"        android:state_pressed=["true" | "false"]        android:state_focused=["true" | "false"]        android:state_hovered=["true" | "false"]        android:state_selected=["true" | "false"]        android:state_checkable=["true" | "false"]        android:state_checked=["true" | "false"]        android:state_enabled=["true" | "false"]        android:state_activated=["true" | "false"]        android:state_window_focused=["true" | "false"] /></selector>
ELEMENTS:
<selector>
需要。这必须是根元素。包含一个或多个<item>元素。
属性:


xmlns:android
串。需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:constantSize
布尔。 “真”,如果绘制的内部报告的大小仍然是状态的改变常数(尺寸是最大的所有国家的); “假”,如果尺寸变化基于当前的状态。默认为false。
android:dither
布尔。 “真”,以使该位图的抖动,如果位图不具有相同的像素结构的屏幕(例如,一个ARGB 8888位图与一个RGB 565屏幕); “假”以禁用抖动。默认值为true。
android:variablePadding
布尔。 “真”,如果绘制的填充应根据所选择的当前状态的变化; “假”如果填充应保持不变(基于所有国家的最大填充)。启用此功能,您需要处理时,这往往是不支持的状态变化,进行布局。默认为false。
<item>
定义绘制到在某些国家使用,正如它的属性描述。必须用<selector>元素的子元素。
属性:


android:drawable
绘制资源。需要。参照绘制资源。
android:state_pressed

布尔。 “真”,如果当按下对象应使用此产品的(当按钮被触摸诸如/点击); “假”,如果这个项目应该在默认使用,非按下状态。
android:state_focused
布尔。 “真”如果这个项目时,应使用对象具有输入焦点(当用户选择一个文本输入等); “假”,如果这个项目应该在默认使用,非聚焦状态。
android:state_hovered
布尔。 “真”,如果当物体被由光标悬停应该使用这个产品“假”,如果这个项目应该在默认使用,非悬停状态。通常,这可拉伸可以是用于“聚焦”状态的相同可拉伸。
介绍了API级别14。


android:state_selected
布尔。 “真”,如果当物体是用方向控制导航时(例如通过与一个d-垫列表导航时)当前用户选择这个项目应该使用; “假”如果在没有选择的对象应使用此项目。
选择状态时,使用对焦(安卓state_focused)是不够的(例如,当列表视图具有焦点,并在其中一个项目中选择了D-PAD)。


android:state_checkable
布尔。 “真”,如果当对象是可勾选应该使用这个产品, “假”,如果当对象不是可勾选此项目应该被使用。 (只有有用的,如果该对象可以可检查的和非可检查的小部件之间转换。)
android:state_checked
布尔。 “真”,如果当对象被选中应该使用这个产品, “假”,如果它应当对象是使用未检查。
android:state_enabled
布尔。 “真”,如果当物体被启用应使用此产品的(能够接收触摸/点击事件); “假”,如果当物体被禁用它应该被使用。
android:state_activated
布尔。 “真”,如果当对象被激活为持久的选择应该使用这个项目(如“亮点”以前选择列表中一个持久的导航视图的项目); “假”,如果当物体不激活它应该被使用。
介绍了API级别11。


android:state_window_focused
布尔。 “真”,如果当应用程序窗口已关注这个项目应该使用(应用程序是在前台),“假”,如果这个项目时,应使用应用程序窗口没有焦点(例如,如果通知栏是拉下来,或者出现一个对话框)。
注意:请记住,在Android的匹配对象的当前状态的状态列表适用中的第一项。因此,如果在该列表中的第一项包含任何状态的上述属性,那么它被施加每一次,这就是为什么预设值应始终是最后(如下面的示例所示)。


例:
在保存RES /绘制/ button.xml XML文件:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"          android:drawable="@drawable/button_pressed" /> <!-- pressed -->    <item android:state_focused="true"          android:drawable="@drawable/button_focused" /> <!-- focused -->    <item android:state_hovered="true"          android:drawable="@drawable/button_focused" /> <!-- hovered -->    <item android:drawable="@drawable/button_normal" /> <!-- default --></selector>
这种布局XML应用状态列表绘制到按钮:
<Button    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:background="@drawable/button" />
也可以看看:
StateListDrawable
级别列表
一个管理若干备用可绘制的绘制对象,每人分到的最大数值。设置与执行setLevel()的绘制的电平值加载在具有Android的水平列表中的绘制资源:最高等级大于或等于传递给方法的值。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针LevelListDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><level-list    xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@drawable/drawable_resource"        android:maxLevel="integer"        android:minLevel="integer" /></level-list>
android:insetLeft
<level-list>
这必须是根元素。包含一个或多个<item>元素。
属性:


xmlns:android
串。 需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
<item>
限定了可拉伸在一定的水平来使用。
属性:


android:drawable
绘制资源。 需要。参考可绘制资源被插图。
android:maxLevel
整数。最高级别允许为这个项目。
android:minLevel
整数。最低级别允许为这个项目。
例:

<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@drawable/status_off"        android:maxLevel="0" />    <item        android:drawable="@drawable/status_on"        android:maxLevel="1" /></level-list>
一旦这个被施加到一个视图,该电平可以与执行setLevel()或setImageLevel()来改变。
也可以看看:
LevelListDrawable
过渡绘制对象
一个TransitionDrawable是绘制对象,可以淡入淡出两个可绘制资源之间。
每个绘制由单个<过渡>元素中的<item>元素表示。没有两个以上的项目支持。过渡转接,呼叫转移启动()。为了向后过渡,叫反向转换()。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针TransitionDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:
<?xml version="1.0" encoding="utf-8"?><transitionxmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@[package:]drawable/drawable_resource"        android:id="@[+][package:]id/resource_name"        android:top="dimension"        android:right="dimension"        android:bottom="dimension"        android:left="dimension" /></transition>
ELEMENTS:
<transition>
需要。这必须是根元素。包含一个或多个<item>元素。
属性:


xmlns:android
串。需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
<item>
定义绘制的绘制转变的一部分使用。必须是一个<transition>元素的一个孩子。接受子<位图>元素。
属性:


android:drawable
绘制资源。需要。参照绘制资源。
android:id
资源ID。为此绘制一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:“@ + ID /名称”。加符号表示,本应作为一个新的ID来创建。您可以使用此标识来检索和修改与View.findViewById()或Activity.findViewById()的绘制。
android:top
整数。顶部的像素偏移。
android:right
整数。正确的像素偏移量。
android:bottom
整数。底部的像素偏移。
android:left
整数。在像素,左偏移。
例:
在保存RES /绘制/ transition.xml XML文件:
<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/on" />    <item android:drawable="@drawable/off" /></transition>
这种布局XML应用形状绘制到View

<ImageButton    android:id="@+id/button"    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:src="@drawable/transition" />
And the following code performs a 500ms transition from the first item to the second:

ImageButton button = (ImageButton) findViewById(R.id.button);TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();drawable.startTransition(500);
也可以看看:
TransitionDrawable
插图绘制对象


在XML的镶石按指定距离的另一绘制定义的绘制。当一个视图需要一个背景是不是查看实际边界较小,这非常有用。


文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针InsetDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><inset    xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/drawable_resource"    android:insetTop="dimension"    android:insetRight="dimension"    android:insetBottom="dimension"    android:insetLeft="dimension" />
ELEMENTS:
<inset>
定义插图绘制。这必须是根元素。
属性:


xmlns:android
串。 需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:drawable
绘制资源。 需要。参考可绘制资源被插图。
android:insetTop
尺寸。顶部插入,作为一个尺寸值或尺寸资源
android:insetRight
尺寸。右小图,作为一个尺寸值或尺寸资源
android:insetBottom
尺寸。底部插入,作为一个尺寸值或尺寸资源
android:insetLeft
尺寸。左小图,作为一个尺寸值或尺寸资源
例:

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/background"    android:insetTop="10dp"    android:insetLeft="10dp" />
也可以看看:
InsetDrawable
Clipdrawable
该剪辑另一个可绘制在此基础上定义在XML中可绘制可绘制目前的水平。可以控制多少子抽拉得到在宽度和高度根据该电平,以及一个重力来控制在其被放置在其整体容器裁剪。最常用的实现之类的进度条。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针ClipDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><clip    xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/drawable_resource"    android:clipOrientation=["horizontal" | "vertical"]    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |                     "fill_vertical" | "center_horizontal" | "fill_horizontal" |                     "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
ELEMENTS:
<clip>
定义剪辑绘制。这必须是根元素。
属性:


xmlns:android
串。 需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:drawable
绘制资源。 需要。参考可绘制资源进行裁剪。
android:clipOrientation
关键词。该方向的剪辑。
必须是以下常数值之一:

ValueDescriptionhorizontalClip the drawable horizontally.verticalClip the drawable vertically.
android:gravity
Keyword. Specifies where to clip within the drawable.

Must be one or more (separated by '|') of the following constant values:

ValueDescriptiontopPut the object at the top of its container, not changing its size. When clipOrientation is "vertical", clipping occurs at the bottom of the drawable.bottomPut the object at the bottom of its container, not changing its size. When clipOrientation is "vertical", clipping occurs at the top of the drawable.leftPut the object at the left edge of its container, not changing its size. This is the default. When clipOrientation is"horizontal", clipping occurs at the right side of the drawable. This is the default.rightPut the object at the right edge of its container, not changing its size. When clipOrientation is "horizontal", clipping occurs at the left side of the drawable.center_verticalPlace object in the vertical center of its container, not changing its size. Clipping behaves the same as when gravity is"center".fill_verticalGrow the vertical size of the object if needed so it completely fills its container. When clipOrientation is "vertical", no clipping occurs because the drawable fills the vertical space (unless the drawable level is 0, in which case it's not visible).center_horizontalPlace object in the horizontal center of its container, not changing its size. Clipping behaves the same as when gravity is"center".fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container. When clipOrientation is "horizontal", no clipping occurs because the drawable fills the horizontal space (unless the drawable level is 0, in which case it's not visible).centerPlace the object in the center of its container in both the vertical and horizontal axis, not changing its size. WhenclipOrientation is "horizontal", clipping occurs on the left and right. When clipOrientation is "vertical", clipping occurs on the top and bottom.fillGrow the horizontal and vertical size of the object if needed so it completely fills its container. No clipping occurs because the drawable fills the horizontal and vertical space (unless the drawable level is 0, in which case it's not visible).clip_verticalAdditional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges.clip_horizontalAdditional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges.
EXAMPLE:
XML file saved at res/drawable/clip.xml:
<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/android"    android:clipOrientation="horizontal"    android:gravity="left" />

The following layout XML applies the clip drawable to a View:

<ImageView    android:id="@+id/image"    android:background="@drawable/clip"    android:layout_height="wrap_content"    android:layout_width="wrap_content" />
以下代码获取被拉伸并增加以便剪裁逐步显示图像的量:

ImageView imageview = (ImageView) findViewById(R.id.image);ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();drawable.setLevel(drawable.getLevel() + 1000);
增加水平降低削波的量和慢慢揭示了图像。这是在7000的水平:


注意:默认级别为0,这是完全修剪,因此图像是不可见的。当电平是10,000时,图像不裁剪和完全可见。

也可以看看:
ClipDrawable
Scaledrawable
在XML改变基于当前水平的另一个绘制的大小定义的绘制。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针ScaleDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><scale    xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/drawable_resource"    android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]    android:scaleHeight="percentage"    android:scaleWidth="percentage" />
ELEMENTS:
<scale>
定义绘制规模。这必须是根元素。
属性:


xmlns:android

串。需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:drawable
绘制资源。需要。参照绘制资源。
android:scaleGravity
关键词。指定缩放后的重心位置。
必须是一个或多个(由分隔'|')以下的恒定值:


值说明
top把对象在其容器的顶部,不改变其大小。
bottom把对象在其容器的底部,不改变其大小。
left对象在其容器的左边缘,不改变其大小。这是默认的。
right把对象在其容器的右边缘,不改变其大小。
center_verticalcenter_vertical地点对象,不改变其大小。
fill_vertical 如果需要的话,以便完全填满其容器fill_vertical成长的对象的垂直尺寸。
 center_horizontal在它的容器的水平中心CENTER_HORIZONTAL地点对象,不改变其大小。
fill_horizontal如果需要的话,以便完全填满其容器fill_horizo​​ntal成长的对象的水平大小。
center中心放置在其容器的同时在垂直和水平轴,不改变其尺寸的中心的对象。
fill 填充,如果需要,以便完全填满其容器成长的对象的水平和垂直尺寸。
clip_vertical可以设置clip_vertical附加选项,使顶部和/或儿童的底部边缘夹在其容器的边界。夹子基于垂直重力:顶部重力夹子的底部边缘,一个clip_horizontal底重力夹子的顶部边缘,并且既不剪辑两边缘。
可以设置clip_horizo​​ntal附加选项,使左和/或儿童的右边缘夹在其容器的边界。夹子基于水平比重:左重力剪辑的右边缘,右重力夹子的左边缘,并且既不剪辑两边缘。
android:scaleHeight
百分比。规模高度,表示为可绘制的结合的百分比。该值的格式为XX%。例如:100%,12.5%等
android:scaleWidth
百分比。刻度宽度,表示为可绘制的结合的百分比。该值的格式为XX%。例如:100%,12.5%等
例:

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/logo"    android:scaleGravity="center_vertical|center_horizontal"    android:scaleHeight="80%"    android:scaleWidth="80%" />
也可以看看:
LayerDrawable
Shapedrawable
这是在XML中定义的通用形状。
文件位置:
RES /绘制/ filename.xml中
文件名被用作资源ID。
编译的资源数据类型:
资源指针GradientDrawable。
资源引用:
在Java:R.drawable.filename
在XML:@ [包:]绘制/文件名
句法:

<?xml version="1.0" encoding="utf-8"?><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:useLevel=["true" | "false"] />    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer" />    <size        android:width="integer"        android:height="integer" />    <solid        android:color="color" />    <stroke        android:width="integer"        android:color="color"        android:dashWidth="integer"        android:dashGap="integer" /></shape>
ELEMENTS:
<shape>
形状绘制。这必须是根元素。
属性:


xmlns:android
串。需要。定义了XML命名空间,它必须是“http://schemas.android.com/apk/res/android”。
android:shape
关键词。限定形状的类型。有效值为:
值应将描述
"rectangle",填补了包含查看的矩形。这是默认的形状。
"oval"”的椭圆形状,其适合的含视图的尺寸。
"line"跨越包含视图的宽度水平线。这种形状需要<冲程>元素来定义线的宽度。
"ring"”的环状。
下面的属性只用于机器人: android:shape="ring":


android:innerRadius
尺寸。用于环(中间孔)的内部的半径,作为尺寸值或尺寸的资源。
android:innerRadiusRatio
浮动。为环的内部的半径,表示为环的宽度的比率。例如,如果机器人:innerRadiusRatio =“5”,然后内径等于环的宽度由5分此值由机器人覆盖:innerRadius。默认值为9。
android:thickness
尺寸。环的厚度,作为尺寸值或尺寸的资源。
android:thicknessRatio
浮动。环的厚度,表示为环的宽度的比率。例如,如果机器人:thicknessRatio =“2”,则厚度等于环的宽度除以2。此值由机器人覆盖:innerRadius。默认值是3。
android:useLevel
布尔。如果此用作LevelListDrawable“真”。这通常应该是“假”还是你的形状可能不会出现。
<corners>
创建圆角的形状。仅适用于当形状为长方形。
属性:


android:radius
尺寸。所有边角半径,作为一个尺寸值或尺寸资源。这被覆盖由以下属性的每个角落。
android:topLeftRadius
尺寸。为左上角的半径,作为尺寸值或尺寸的资源。
android:topRightRadius
尺寸。为右上角的半径,作为尺寸值或尺寸的资源。
android:bottomLeftRadius
尺寸。为左下角的半径,作为尺寸值或尺寸的资源。
android:bottomRightRadius
尺寸。对于右下角的半径,作为尺寸值或尺寸的资源。
注:每一个角落,必须在开始阶段被超过1提供一个拐角半径大,否则没有边角圆润。如果你想具体的角落,不进行四舍五入,一个解决办法是使用Android:半径大于1设置默认的圆角半径大,但覆盖你真正想要的值每一个角落,提供零(“0dp” ),您不希望圆角。


<gradient>
指定形状的渐变色。
属性:


android:angle
整数。渐变的角度,以度。 0为从左到右,90是底部至顶部。它必须是45默认的倍数为0。
android:centerX
浮动。相对X位置为梯度(0 - 1.0)的中心。
android:centerY
浮动。相对Y位置的梯度(0 - 1.0)的中心。
android:centerColor
颜色。可选颜色自带的起点和终点之间的颜色,为十六进制值或颜色的资源。
android:ENDCOLOR
颜色。结束颜色,为十六进制值或颜色的资源。
android:gradientRadius
浮动。渐变的半径。只有当应用机器人:TYPE =“放射状”。
android:startColor
颜色。起始色,为十六进制值或颜色资源。
android:type
关键词。类型渐变图案的应用。有效值为:
值说明
"linear"”的线性渐变。这是默认的。
"radial"”径向渐变。起始颜色为中心的颜色。
"sweep"一个扫线梯度。
android:useLevel
布尔。如果此用作LevelListDrawable“真”。
<padding>
填充要应用到含有视图元件(此焊盘视图内容的位置,而不是形状)。
属性:


android:left
尺寸。左填充,作为一个尺寸值或尺寸资源。
android:top
尺寸。顶部填充,作为一个尺寸值或尺寸资源。
android:right
尺寸。右侧填充,作为一个尺寸值或尺寸资源。
android:bottom
尺寸。底部填充,作为一个尺寸值或尺寸资源。
<size>
形状的尺寸。
属性:


android:height
尺寸。形状的高度,作为尺寸值或尺寸的资源。
android:width
尺寸。形状的宽度,作为尺寸值或尺寸的资源。
注意:形状扩展到容器视图相称这里定义的尺寸的大小,在默认情况下。 scaleType到“中心”:当您在使用ImageView的形状,你可以通过设置在Android限制缩放。


<solid>
纯色填充的形状。
属性:


android:color
颜色。颜色应用到形状,以十六进制值或颜色资源。
<stroke>
中风线的形状。
属性:


android:width
尺寸。线的厚度,作为尺寸值或尺寸的资源。
android:color
颜色。线的颜色,如十六进制值或颜色资源。
android:dashGap
尺寸。线之间的距离破折号,作为尺寸值或尺寸的资源。只有有效的,如果机器人:dashWidth设置。
android:dashWidth
尺寸。各划线的大小,作为尺寸值或尺寸的资源。只有有效的,如果机器人:dashGap设置。
例:
在保存RES /绘制/ gradient_box.xml XML文件:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <gradient        android:startColor="#FFFF0000"        android:endColor="#80FF00FF"        android:angle="45"/>    <padding android:left="7dp"        android:top="7dp"        android:right="7dp"        android:bottom="7dp" />    <corners android:radius="8dp" /></shape>

这种布局XML应用形状绘制到View:

<TextView    android:background="@drawable/gradient_box"    android:layout_height="wrap_content"    android:layout_width="wrap_content" />

此应用程序代码获取形状绘制,并将其应用于一个观点

Resources res = getResources();Drawable shape = res. getDrawable(R.drawable.gradient_box);TextView tv = (TextView)findViewByID(R.id.textview);tv.setBackground(shape);


0 0
原创粉丝点击