补间动画java代码实现,及资源文件配置

本文深入探讨了Android中补间动画的实现方法,包括Alpha、Scale、Rotate、Translate动画类的使用,以及如何通过XML资源文件配置动画,提供丰富的示例代码。

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

补间动画:
补间动画能完成一系列简单的变化(如位置、尺寸、透明度、和旋转等)。例如,在你的程序中有一个ImageView组件,我们通过补间动画可以使该视图组件实现放大、缩小、旋转、渐变等。补间动画类位于android.view.animation包中,该包中包含了一些常用的动画实现类。
Animation:动画抽象类,其他几个实现类继承该类。
ScaleAnimation:控制尺寸变化的动画类。
AlphaAnimation:控制透明度变化的动画类。
RotateAnimation:控制旋转变化的动画类。
TranslateAnimation:控制位置变化的动画类。
AnimationSet:定义动画属性集合类。
AnimationUtils:动画工具类。
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == alphaBtn) {
// 透明动画的类
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.1f);
// 设置动画持续时间
alphaAnimation.setDuration(3000);
// 保持动画后的状态
alphaAnimation.setFillAfter(true);
// 重复的次数
alphaAnimation.setRepeatCount(-1);
// 重复的类型 RESTART - 每次从头开始播放动画 REVERSE - 反向播放动画
alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// 让图片组件开始加载动画
imageView.startAnimation(alphaAnimation);


}
if (v == scaleBtn) {
// 缩放动画的类。从图片的中心位置开始缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
0.1f, Animation.RELATIVE_TO_SELF, 0.1f,
Animation.RELATIVE_TO_SELF, 0.1f);
// 设置动画持续时间
scaleAnimation.setDuration(3000);
// 保持动画后的状态
scaleAnimation.setFillAfter(true);
// 重复的次数
scaleAnimation.setRepeatCount(-1);
// 重复的类型 RESTART - 每次从头开始播放动画 REVERSE - 反向播放动画
scaleAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// 让图片组件开始加载动画
imageView.startAnimation(scaleAnimation);
}

if (v == translateBtn) {

//移动动画

TranslateAnimation translateAnimation = new TranslateAnimation(0,
150, 0, 150);
// 设置动画持续时间
translateAnimation.setDuration(3000);
// 保持动画后的状态
translateAnimation.setFillAfter(true);
// 重复的次数
translateAnimation.setRepeatCount(-1);
// 重复的类型 RESTART - 每次从头开始播放动画 REVERSE - 反向播放动画
translateAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// 让图片组件开始加载动画
imageView.startAnimation(translateAnimation);


}

if (v == rotateBtn) {

//旋转动画

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.8f,
Animation.RELATIVE_TO_SELF, 0.8f);
// 设置动画持续时间
rotateAnimation.setDuration(3000);
// 保持动画后的状态
rotateAnimation.setFillAfter(true);
// 重复的次数
rotateAnimation.setRepeatCount(-1);
// 重复的类型 RESTART - 每次从头开始播放动画 REVERSE - 反向播放动画
rotateAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 让图片组件开始加载动画
imageView.startAnimation(rotateAnimation);

}

使用资源文件配置动画
res/anim/目录下建立xml文件

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:fillAfter="true">

    <alpha

        android:duration="2000" android:fillAfter="true" android:fromAlpha="1" android:toAlpha="0.5"/>

    <scale android:duration="2000" android:fromXScale="1"android:fromYScale="1"

        android:pivotX="50%" android:pivotY="50%" android:toXScale="0.5" android:toYScale="0.5" >

    </scale>

    <translate android:duration="2000" android:fromXDelta="300" android:fromYDelta="300"

android:toXDelta="0" android:toYDelta="0" />

    <rotate android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%"

android:repeatCount="0" android:repeatMode="reverse" android:toDegrees="360"/>

</set>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值