android开发中,xml文件可以定义形状、动画等,这里对xml的各种使用方法进行详细介绍:
1、xml定义形状
先上源码,放在drawable目录下:
android:centerColor="#ff0000"
android:angle="45"
android:endColor="#000000"/>
android:height="200dip"/>
android:dashGap="3dip"
android:color="#00ff00"
android:width="3dip"/>
corners 定义形状的圆角的角度:android:radius 定义四个角的角度,如果想要分别定义每个角的角度可以使用 android:topLeftRadius、android:topRightRadius、android:bottomLeftRadius、android:bottomRightRadius,下图是设置四个角度分别为10dip,20dip,40dip,50dip效果:
;
gradient 定义渐变:android:startColor 、endColor、centerColor分别定义渐变的开始颜色、结束颜色和中间颜色;android:type 渐变的方向,默认是linear,还可以指定sweep渐变和radial渐变,当type为radial时需要设置半径android:gradientRadius,三种方式的效果如图:
android:angle定义渐变的角度,只有0,45,90三个值,分别是水平、45度倾斜方向和竖直方向渐变,使用的渐变颜色是代码中的颜色效果如图:
solid填充颜色,和渐变不能同时使用;
size形状大小;
stroke描边:android:width定义描边线的宽度 ,android:dashWidth,一段描边线的宽度,android:dashGap间隔,和dashWidth一起使用到达虚线的效果,代码中的效果如上图所示,在设置android:width为10dip时 效果如图
2.xml定义动画
xml定义动画可以分为帧动画、补间动画(Tween)以及自定义动画。
(1)帧动画xml源码放在drawable目录下,:
设置imageVIew的src使用上面的动画,在activity中启动动画,代码如下:
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/animlist"
/>
ImageView imageView = (ImageView) findViewById(R.id.image);
Button button = (Button) findViewById(R.id.button1);
final AnimationDrawable aDrawable = (AnimationDrawable) imageView.getDrawable();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
aDrawable.start();
}
});(2)补间动画:可以通过设置旋转、缩放、位移、透明度等,
android:interpolator="@android:anim/linear_interpolator"
android:duration="5000">
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
android:toAlpha="0.3"
android:duration="3000"/>
android:toDegrees="1800"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
android:toXDelta="20"
/>
android:interpolator 定义动画是匀速变化还是匀加速匀减速等。在activity加载动画后,启动动画代码如下
ImageView imageView = (ImageView) findViewById(R.id.image);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.test);
imageView.startAnimation(animation);