什么是AnimationSet
1.AnimationSet是Animation的子类
2.一个AnimationSet包含了一系列的Animation
3.针对AnimationSet设置一些Animation的常见属性(startOffset,duration等等),可以被包含在AnimationSet中的Animation集成
2.Interpolator的使用方法
什么是Interpolator
Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator
AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator:在动画的以均匀的速度改变
这是同时实现两个动画的效果:
MainActivity.java
package com.yx.animations03;
import com.yx.animations03.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button button=null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new buttonListener());
imageView = (ImageView) findViewById(R.id.imageViewId);
}
class buttonListener implements OnClickListener{
@Override
public void onClick(View v) {
/*//多个动画效果,方法一
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.flishes);
imageView.startAnimation(animation);
*/
//方法二
AnimationSet animationSet = new AnimationSet(true);//这里的true表示共享一个Interpolator
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f
);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0f
);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000);
animationSet.setStartOffset(500);
imageView.startAnimation(animationSet);
}
}
}
flishes.xml 本文件在res下新建的anim中
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>
<!-- android:shareInterpolator="true":设置下面的控件(scale,alpha)共享一个Interpolator-->
<!-- android:interpolator="@android:anim/accelerate_interpolator":设置动画效果
AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator:在动画的以均匀的速度改变
-->
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
></scale>
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"></alpha>
</set>
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="测试动画效果"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="@+id/scaleButton"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50px"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>