android使用xml文件,android开发中xml文件使用总结

本文详述了在Android开发中XML文件的应用,包括如何使用XML定义形状,如设置渐变、圆角及描边效果,并介绍了XML动画的创建,如帧动画、补间动画的实现及启动方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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效果:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

gradient 定义渐变:android:startColor 、endColor、centerColor分别定义渐变的开始颜色、结束颜色和中间颜色;android:type 渐变的方向,默认是linear,还可以指定sweep渐变和radial渐变,当type为radial时需要设置半径android:gradientRadius,三种方式的效果如图:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

android:angle定义渐变的角度,只有0,45,90三个值,分别是水平、45度倾斜方向和竖直方向渐变,使用的渐变颜色是代码中的颜色效果如图:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

solid填充颜色,和渐变不能同时使用;

size形状大小;

stroke描边:android:width定义描边线的宽度 ,android:dashWidth,一段描边线的宽度,android:dashGap间隔,和dashWidth一起使用到达虚线的效果,代码中的效果如上图所示,在设置android:width为10dip时 效果如图

0818b9ca8b590ca3270a3433284dd417.png

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);

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值