概述
ColorDrawable
public void setBackgroundColor(@ColorInt int color) {
if (mBackground instanceof ColorDrawable) {
((ColorDrawable) mBackground.mutate()).setColor(color);
computeOpaqueFlags();
mBackgroundResource = 0;
} else {
setBackground(new ColorDrawable(color));
}
}
GradientDrawable通常用来设置渐变色。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFF"
android:endColor="#000"
android:angle="45" />
</shape>
GradientDrawable的构造函数:public GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)
BitmapDrawable
NinePatchDrawable
InsetDrawable
ClipDrawable
<?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"] />
clip元素中只有android:drawable、android:clipOrientation和android:gravity三个属性,其中android:drawable为需要裁剪的原始图片,android:clipOrientation为裁剪的方向,可以按照垂直(vertical)或者水平(horizontal)方向进行裁剪,android:gravity为指定从哪里开始裁剪,这个可以通过或操作设置多个属性。AnimationDrawable
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/on_001"
android:duration="100"/>
<item
android:drawable="@drawable/on_002"
android:duration="100"/>
<item
android:drawable="@drawable/on_003"
android:duration="100"/>
<item
android:drawable="@drawable/on_004"
android:duration="100"/>
<item
android:drawable="@drawable/on_005"
android:duration="100"/>
<item
android:drawable="@drawable/on_006"
android:duration="100"/>
</animation-list>
ScaleDrawable
ScaleDrawable,RotateDrawable分别用来对图片进行拉伸和旋转。LayerDrawable
属性:xmlns:android 必须的。 定义xml文件的命名空间,必须是 "http://schemas.android.com/apk/res/android"。
<item>定义一个drawable放置在layer drawable中,具体的位置有它的。必须是<selector>的子元素(这个不太理解)。可接受<bitmap>做为子元素
属性:
android:drawable Drawable资源。必须的。引用的drawable资源
android:id 资源ID。一个为这个item定义的唯一的资源ID。 使用:"@+id/name".这样的方式。可以检索或修改这个drawable通过下面的方式:View.findViewById() orActivity.findViewById().
android:top Integer。与top的距离,单位像素
android:right Integer。与right的距离,单位像素
android:bottom Integer。与bottom的距离,单位像素
android:left Integer。与left的距离,单位像素
在默认的情况下,所有的drawable item都会缩放到合适的大小来适应视图。因此,在一个layer-list中定义不同的位置可能会增加视图的尺寸和被自动缩放。为了避免被缩放,可以再<item>节点里加上<bitmap>元素来指定一个drawable,并且定义一些不会被拉伸的gravity属性,例如center。
举个例子,下面在item里面定义一个drawable,图片就会自动缩放以适应视图的大小。
<item android:drawable="@drawable/image" />
为了避免缩放,可以使用<bitmap>的子元素来指定drawable资源
<item>
<bitmap android:src="@drawable/image"
android:gravity="center" />
</item>
StateListDrawable
LevelListDrawable
LevelListDrawable对应于<level-list标签>,跟selector一样,它也表示一个drawable集合。使用方法如下。
<?xml version="1.0" encoding="utf-8level_drawable"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_launcher"
android:maxLevel="0"/>
<item
android:drawable="@drawable/icon1"
android:maxLevel="1"/>
</level-list>
<ImageView
android:id="@+id/cv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="@drawable/level_drawable"/>
TransitionDrawable
一个TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果。
<transition>节点下的每个<item>代表一个drawable资源。只能有两个<item>。先前转换调用startTransition()。向后,调用 reverseTransition()。
-
文件位于:
-
res/drawable/filename.xml
文件名作为资源ID
编译资源类型:
-
指向
TransitionDrawable的指针
资源引用:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
语法:
-
XML文件保存为:
res/drawable/transition.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>在layout文件中使用:
<ImageButton android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" /> -
ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);
<?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>
例子:
9708



被折叠的 条评论
为什么被折叠?



