android中Drawable总结
资源类型:Drawable
抽象概念,即屏幕上画。
i. Drawable类型:Bitmap
a) 资源在代码中的类型:BitmapDrawable
b) 资源存储:存储在res/ drawable /目录下,会被appt工具进行压缩处理,如果你不想被压缩处理,则建议提供在/res/raw/目录下。
c) Android只支持三种格式的位图:png (preferred), .jpg (acceptable), .gif (discouraged)。
d) 在代码中使用
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
e) 除了提供png、jpg和gif外,还可以提供xml。
其中xml仍然引用png、jpg或gif,只是这样就可以添加属性了。
xml中的内容举例如下:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src=\'#\'" /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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
ii. Drawable类型:Nine-Patch
a) 资源在代码中的类型:NinePatchDrawable
b) 资源存储:res/drawable/filename.9.png。
c) 这种一种可拉伸的PNG资源,当一个组件在空间上使用了wrap_content时,通常会使用Nine-Patch作为背景。例如:Button。
d) 除了提供。9.png外,还可以提供xml。
其中xml仍然引用9.png,只是这样就可以添加属性了。
iii. Drawable类型:LayerList
a) 资源在代码中的类型:LayerDrawable。
b) 资源存储:res/drawable/filename.xml
c) LayerList管理着多个Drawable的容器,它依次绘制Drawable,后面绘制的Drawable会在前面绘制的上面。
d) 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>
通常<item></item>之间会使用<Bitmap>,好布局,避免拉伸的影响。
e) 效果例子:
iv. Drawable类型:StateListDrawable
a) 资源在代码中的类型:StateListDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它也是Drawable的容器,功能就是当UI Widget处于不同的状态下,显示不同的Drawable。和ColorStateList相似,本质上它们都是selector。
d) 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_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
e) d)中状态的匹配是先匹配原则。
v. Drawable类型:LevelListDrawable
a) 源在代码中的类型:LevelListDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它也是Drawable的容器,每个Drawable都定义了minLevel和maxLevel。
当程序员调用setLevel(int level)时,所有maxLevel≥level的Drawable都将被显示。
d) xml文件中的格式:
<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>
vi. Drawable类型:Transition Drawable
a) 资源在代码中的类型:TransitionDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它也是Drawable的容器,但它只容纳两个Drawable。它提供一个Drawable淡入,另一个Drawable淡出的功能。
d) xml文件中的格式:
<?xml version="1.0" encoding="utf-8"?>
<transition 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" />
</transition>
e) 使用代码
ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);
vii. Drawable类型:Inset Drawable
a) 资源在代码中的类型:InsetDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它的功能是将Inset Drawable按指定的空间插入到另一个Drawable中去。但SDK中没有说明如何指定另一个Drawable。
d) 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" />
viii. Drawable类型:Clip Drawable
a) 资源在代码中的类型:ClipDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它的功能是实现随意被剪切的Drawable。常用于进度条。
d) 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"] />
e) android:gravity属性:指明了剪切的开始位置。
例如:top,则从Drawable下部开始剪切。left,则从Drawable右部开始剪切。
f) 使用代码:
ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);
其中setLevel的值从0到10000。0表示100%剪切,10000表示%0剪切。
g) 效果
ix. Drawable类型:Scale Drawable
a) 资源在代码中的类型:ClipDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它的功能是对另一个Drawable进行大小调整。常用于进度条。
d) 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" />
e) 暂时不理解,需要看到代码和效果才能了解它的功能。
x. Drawable类型:Shape Drawable
a) 资源在代码中的类型:ShapDrawable。
b) 资源存储:res/drawable/filename.xml
c) 它的功能在Drawable上画一个图形,而且不是在代码中,而是在xml中。
d) xml文件中的格式:
e) <?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners />
<gradient />
<padding />
<size />
<solid />
<stroke />
</shape>
4) 资源类型:Layout
i. 资源在代码中的类型:View
ii. 资源存储:res/layout/filename.xml
iii.xml内容格式:
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup>
<View>
<requestFocus/>
</View>
<ViewGroup >
<View />
</ViewGroup>
<include layout="@layout/layout_resource"/>
</ViewGroup>
其中根元素不仅仅是<ViewGroup>,还可以是<merge>和<View>
iv. <ViewGroup>元素
a) View的容器,并不是所有的ViewGroup都是View的容器,例如;实现于AdapterView的ViewGroup。
b) 它的属性中主要注意的是android:id,使用该属性在代码中引用ViewGroup。而不是android:name属性。
v. <View>元素
a) 常引用widget,例如TextView、Button等。
b) 它的属性中主要注意的是android:id,使用该属性在代码中引用View。而不是android:name属性。
vi. <requestFocus>元素
a) 指定某个View得到焦点,一个文件只能有一个<requestFocus>
vii. <include>
a) 功能是包含一个layout资源
b) layout属性指定了layout资源
c) 指定android:id属性,意味着重命名layout资源中的根元素。
d) 还可指定layout资源中的根元素所拥有的属性
e) android:layout_height与android:layout_width需要同时指定,否则无效。
f) 介绍一种View,实现<include>相同的功能,它是ViewStub。注意它是View。
viii. <ViewStub>
a) xml中的格式:
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
android:layout_width="120dip"
android:layout_height="40dip" />
b) 代码:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
转自:http://hi.baidu.com/xmjmcai/item/be0722cf4c66b82ea1b50a30
更多相关文章
- Android(安卓)常见错误处理
- Android中Rect和RectF的区别与联系
- android开玩笑之创建xml文件
- Android(安卓)网络:使用URL访问网络资源,ImageView,下载到本地
- android so壳入口浅析
- AndroidStudio中导入module详细介绍
- android 区分按键长按及短按
- 【Android】如何方便地将代码抛到主线程执行
- 下载 编译 Android源代码 和 Android(安卓)kernel源代码