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)
<?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为指定从哪里开始裁剪,这个可以通过或操作设置多个属性。
<?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>
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是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果。
<transition>节点下的每个<item>代表一个drawable资源。只能有两个<item>。先前转换调用startTransition()
。向后,调用 reverseTransition()
。
res/drawable/filename.xml
TransitionDrawable
的指针
R.drawable.filename
@[package:]drawable/filename
<?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>
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);