标签:
<?xml version="1.0" encoding="utf-8"?> <color xmlns:android="http://schemas.android.com/apk/res/android" android:color="#FF0000" />
当然也可以使用Java代码创建ColorDrawable,需要注意的是Android中使用一个int类型的数据表示颜色值,通常习惯使用十六进制格式的数据表示颜色值。一个int类型包含四个字节,分别代表颜色的4个组成部分:透明度(Alpha)、红(RED)、绿(GREEN)、蓝(BLUE),每个部分由一个字节(8个bit)表示,取值范围为0~255。在xml中使用颜色时可以省略透明度(Alpha)部分,如#ff0000表示红色。但是在代码中必须要明确指出透明度(Alpha)代表的数据,如果省略了就表示完全透明的颜色,例如0xFFFF0000表示红色,而0xFF0000虽然也表示红色,但它却是完全透明的,也就是说当绘制到画布上时,看不出有任何效果。
ColorDrawable drawable = new ColorDrawable(0xffff0000);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size /> //定义区域的大小
<gradient>//设置区域背景的渐变效果
<solid/>//设置区域的背景颜色,如果设置了solid会覆盖gradient的效果
<stroke />//设置区域的边框效果
<padding />//设置区域的内边距
</shape>
<!-- 线性渐变效果的椭圆-->
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:angle="90" />
<stroke
android:width="3dip"
android:color="#fff"
android:dashWidth="4dip"
android:dashGap="5dip" />
</shape>
<!-- 平铺渐变效果的圆环-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring" android:innerRadiusRatio="8"
android:thicknessRatio="3" android:useLevel="false">
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#ff0000" android:endColor="#0000ff" android:centerColor="#00ff00"/>
</shape>
<!-- 发散渐变效果的圆-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring" android:innerRadius="0dip"
android:thickness="70dip" android:useLevel="false">
<gradient android:type="radial" android:useLevel="false" android:gradientRadius="70"
android:startColor="#ff0000" android:endColor="#0000ff" android:centerColor="#00ff00"/>
</shape>
以下几幅图展示了上述三个渐变效果:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/png_icon_416"
android:tileMode="mirror"
android:antialias="true"
android:dither="true"
>
</bitmap>
也可以使用Java代码实现相同的效果,等价的Java代码如下:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.png_icon_416); BitmapDrawable mBitmapDrawable = new BitmapDrawable(mBitmap); mBitmapDrawable.setTileModeXY(TileMode.MIRROR, TileMode.MIRROR); mBitmapDrawable.setAntiAlias(true); mBitmapDrawable.setDither(true); mDrawable = mBitmapDrawable;
效果如下图所示:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/droid_logo"
android:dither="true" />
<?xml version="1.0" encoding="utf-8"?>
<bitamp
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/droid_logo"
android:dither="true" />
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/bitmap_bell"
android:insetLeft="20dp"
android:insetRight="20dp"
android:insetTop="20dp"
android:insetBottom="20dp"
>
</inset>
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:drawable="@drawable/bitmap_android"
android:gravity="left"
>
</clip>
如果没有android:drawable属性,必须要设置一个任意类型的drawable作为子节点,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="left"
>
<bitmap
android:src="@drawable/android_text"
android:gravity="center"
/>
</clip>
<!-- android:scaleGravity=""可以设置缩放的对齐方式 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/smiley_smile"
android:scaleWidth="100%"
android:scaleHeight="100%"
>
</scale>
图7-1 ScaleDrawable运行效果图
八、 RotateDrawable
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/smiley_smile"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>

图8-1 RotateDrawable运行效果图
<?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/level1"
android:duration="300"
/>
<item
android:drawable="@drawable/level2"
android:duration="300"
/>
<item
android:drawable="@drawable/level3"
android:duration="300"
/>
<item
android:drawable="@drawable/level4"
android:duration="300"
/>
<item
android:drawable="@drawable/level5"
android:duration="300"
/>
</animation-list>
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
((AnimationDrawable)mDrawable).start();
}
}, 1000);
十、LayerDrawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/layer1" />
<item android:drawable="@drawable/layer2" />
<item android:drawable="@drawable/layer3" />
</layer-list>
效果如下图所示:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:maxLevel="2000"
android:drawable="@drawable/level1" />
<item
android:maxLevel="4000"
android:drawable="@drawable/level2" />
<item
android:maxLevel="6000"
android:drawable="@drawable/level3" />
<item
android:maxLevel="8000"
android:drawable="@drawable/level4" />
<item
android:maxLevel="10000"
android:drawable="@drawable/level5" />
</level-list>
效果如下图所示:


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/gradient_normal"
/>
<item android:state_pressed="true"
android:drawable="@drawable/gradient_pressed"
/>
<item android:state_focused="true"
android:drawable="@drawable/gradient_focused"
/>
</selector>


图12-3 Pressed状态下StateListDrawable运行效果图<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/smiley_smile"/>
<item android:drawable="@drawable/smiley_smile_glasses"/>
</transition>
在使用AnimationDrawable的时,需要主动调用startTransition方法启动两个层之间的切换动画,也可以调用reverseTransition方法启动逆向切换动画,它们都可以接受一个毫秒数,作为动画的持续时间。代码如下:
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
((TransitionDrawable)mDrawable).startTransition(2000);
}
}, 1000);


标签:
原文地址:http://www.cnblogs.com/shanzei/p/4281397.html