动画的分类
- 帧动画
- 补间动画(淡入淡出、缩放、旋转、平移)
补间动画(Animation)
- Alpha(淡入淡出)
fromAlpha:起始不透明度,取值为0~1.0,0表示完全透明,1.0表示完全不透明
toAlpha:目标不透明度,取值同上
duration:动画持续时长,单位:毫秒数 - Scale(缩放)
fromXScale:起始宽度,取值例如:0、100%
fromYScale:起始高度,取值同上
toXScale:目标宽度,取值同上
toYScale:目标高度,取值同上
pivotX:缩放的中心点X轴坐标,取值例如:0、50%、100%
pivotY:缩放的中心点Y轴坐标,取值同上
duration:动画持续时长,单位:毫秒数 - Rotate(旋转)
fromDegrees:起始角度,取值例如:0、360、720
toDegrees:目标角度,取值同上
pivotX:缩放的中心点X轴坐标,取值例如:0、50%、100%
pivotY:缩放的中心点Y轴坐标,取值同上
duration:动画持续时长,单位:毫秒数 - Translate(平移)
fromXDelta:起始X轴位置,取值例如:0、100%
fromYDelta:起始Y轴位置,取值同上
toXDelta:目标X轴位置,取值例如:0、100%
toYDelta:目标Y轴位置,取值同上
duration:动画持续时长,单位:毫秒数
Ps. 其它属性
fillAfter:是否停留在动画的目标效果,如果取值为true,则表示停留,否则,会闪回控件的起始效果,该属性应该配置在根节点上,配置在子级的动画效果节点上是无效的
repeatCount:重复次数,取值为整型数,在设置该值时,不应该把默认的第1次运行动画计为重复次数
repeatMode:重复模式,即第2次以后如何执行动画效果,取值为restart时表示重新执行动画,取值为reverse时表示后续的每次动画效果都是前一次的逆向执行效果
interpolator:动画加速度,取值为@android:anim/accelerate_decelerate_interpolator时,表示先加速再减速,该值为默认值,取值为@android:anim/accelerate_interpolator表示始终加速,取值为@android:anim/decelerate_interpolator表示始终减速
startOffset:动画起始时间的偏移量,即:过多少毫秒后再开始该节点的动画
关于补间动画的创建
- 通过资源文件创建,并且通过AnimationUtils的loadAnimation()方法加载资源文件,从而得到Animation对象
- 创建AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation的对象,从而得到Animation对象,切记,在使用这种方式时,需要通过setDuration()方法配置动画的持续时长
示例:
activity:
package com.yulipeng.animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView1);
}
//淡入淡出
public void doAlpha(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
//使用代码完成动画
// animation = new AlphaAnimation(0f, 1f);
// animation.setDuration(3000);
// Animation animation = new AnimationUtils.loadInterpolator(this,
// R.anim.alpha);
image.startAnimation(animation);
}
//平移动画
public void doTranslate(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(animation);
}
//缩放动画
public void doScale(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
image.startAnimation(animation);
}
//旋转动画
public void doRotate(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(animation);
}
//动画集合set
public void doAnimtaion(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation);
image.startAnimation(animation);
}
}
anim
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="5000"
>
</alpha>
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
>
</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="100%"
android:fillAfter="true"
android:repeatCount="2"
android:duration="5000"
>
</translate>
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:duration="5000"
>
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1"
android:repeatCount="3"
android:repeatMode="restart"
android:startOffset="6000"
android:duration="3000"/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3000"
android:duration="3000"/>
</set>
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:onClick="doAlpha"
android:text="alpha" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:onClick="doTranslate"
android:text="translate" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:onClick="doScale"
android:text="scale" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:onClick="doRotate"
android:text="rotate" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/num22" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doAnimtaion"
android:text="Animation" />
</LinearLayout>