<span style="font-family:SimSun;">AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); alphaAnimation.setDuration(1000); animationSet.addAnimation(alphaAnimation); image.startAnimation(animationSet);</span>
<span style="font-family:SimSun;">AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1000); animationSet.addAnimation(scaleAnimation); image.startAnimation(animationSet);</span>
<span style="font-family:SimSun;">AnimationSet animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0, 360); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); image.startAnimation(animationSet);</span>
<span style="font-family:SimSun;">AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); image.startAnimation(animationSet);</span></span>
<span style="font-family:SimSun;">setDuration(long durationMills); // 设置动画的时长 setFillAfter(Boolean fillAfter); // 执行动画结束之后,停留在执行结束后的状态 setFillBefore(Boolean fillBefore); // 执行动画结束之后,停留在执行之前的状态 setStartOffset(long startOffset); // 在执行动画之前,等待多少时间 setRepeatCount(int repeatCount); // 动画执行多少次</span>
<span style="font-family:SimSun;">repeatMode(Boolean mode): // 顺序重复/倒序重复 </span>
<span style="font-family:SimSun;">package com.example.testdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
/**
* 动画演示
* @author zengtao 2015年6月10日 下午4:02:13
*
*/
public class MainActivity extends Activity {
private ImageView image;
private Button alpha, scale, rotate, translate, start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initLinstener();
}
private void initLinstener() {
alpha.setOnClickListener(onClickListener);
scale.setOnClickListener(onClickListener);
rotate.setOnClickListener(onClickListener);
translate.setOnClickListener(onClickListener);
start.setOnClickListener(onClickListener);
}
private void initView() {
image = (ImageView) findViewById(R.id.image);
alpha = (Button) findViewById(R.id.alpha);
scale = (Button) findViewById(R.id.scale);
rotate = (Button) findViewById(R.id.rotate);
translate = (Button) findViewById(R.id.trans);
start = (Button) findViewById(R.id.start);
}
private void alpha() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
private void scale() {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
private void rotate() {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
private void translate() {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
private void start() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v == alpha) {
alpha();
} else if (v == scale) {
scale();
} else if (v == rotate) {
rotate();
} else if (v == translate) {
translate();
} else if (v == start) {
start();
}
}
};
}
</span><span style="font-family:SimSun;"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--
起始x轴坐标
止x轴坐标
始y轴坐标
止y轴坐标
轴的坐标
轴的坐标
-->
<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="1000"/>
</set></span><span style="font-family:SimSun;"> Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.scale);
image.startAnimation(animation);</span>
1.AnimationSet是Animation的子类;
2.一个AnimationSet包含了一系列的Animation;
3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;
例:一个AnimationSet中有两个Animation,效果叠加
在上面的代码中,其实,我们也使用到了AnimationSet但是,我们都是只添加了一个动画,所以,多个动画效果的添加,也是非常容易的。<span style="font-family:SimSun;">private void start() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}</span>
Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种
InterpolatorAccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速LinearInterpolator:动画以均匀的速率改变
<span style="font-family:SimSun;">AnimationSet animationSet = newAnimationSet(true); animationSet.setInterpolator(new AccelerateInterpolator());</span>ps:这样呢,就能够将Interpolator插值器放入动画中了,在开启动画的过程中,就会添加相应想动画速度效果。Google给我们准备的还是非常的充分的,你能想到的,他基本都想到的,你想不到的,或许他都实现了,只是没有发现用法而已。
原文地址:http://blog.csdn.net/u011546655/article/details/46483271