Android Animation的一些简单用法

时间:2023-01-30 08:36:53

View Animation
startoffset:动画执行的时间。
pivotX:缩放(旋转)的中轴点X坐标,距离自身左边缘的位置.
pivoty:缩放(旋转)的中轴点y坐标,距离自身上边缘的位置.
interpolator:速率。

XML代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:startOffset="1000"
android:toAlpha="1.0" />

<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="1500"
android:toDegrees="-90" />

<scale
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="100%"
android:pivotY="50%"
android:startOffset="2000"
android:toXScale="2.0"
android:toYScale="2.0" />

<translate
android:duration="2000"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:startOffset="2500"
android:toXDelta="100%p"
android:toYDelta="-100%p" />

</set>

java代码:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

Drawable Animation

XML:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">

<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

JAVA:

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

ObjectAnimation

最简单的用法:

ObjectAnimator// 
.ofFloat(view, "rotationX", 0.0F, 360.0F)//
.setDuration(500)//
.start();

其他详情参考鸿洋大神的:
Android 属性动画(Property Animation) 完全解析 (上)
Android 属性动画(Property Animation) 完全解析 (下)