* 从开始状态到结束状态的一个过渡动画
* 平移
* 透明
* 旋转
* 缩放
package com.example.tween;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private AlphaAnimation a;
private TranslateAnimation t;
private RotateAnimation r;
private ScaleAnimation s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
/**
* 透明度
* 第一个参数fromAlpha:动画起始时的透明度
* 第二个参数toAlpha: 动画结束时的透明度
*/
public void alpha(View v) {
a = new AlphaAnimation(0, 1);
// 设置动画的时间
a.setDuration(500);
// 设置动画的播放模式
a.setRepeatMode(Animation.REVERSE);
// 设置动画的重复次数
a.setRepeatCount(4);
// 开始播放动画
iv.startAnimation(a);
}
/**
* 位移动画
* 参数1,参数3,参数5,参数7: 设置参照点的方式(相对自己)Animation.RELATIVE_TO_SELF
* 参数2:x轴起始移动的位置 (0表示原图位置左上角x轴的坐标)
* 参数4:x轴停止移动的位置(2表示移动原图宽度的两倍)
* 参数6:y轴起始移动的位置 (0表示原图位置左上角y轴的坐标)
* 参数8:y轴停止移动的位置(2表示移动原图高度的两倍)
* TranslateAnimation tras = new TranslateAnimation(
* Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,
* Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
*/
public void translate(View view) {
t = new TranslateAnimation(0, 100, 0, 100);
// 设置动画的时间
t.setDuration(500);
// 设置动画的播放模式
t.setRepeatMode(Animation.REVERSE);
// 设置动画的重复次数
t.setRepeatCount(4);
// 动画做完之后停在结束位置
t.setFillAfter(true);
// 开始播放动画
iv.startAnimation(t);
}
/**
* 旋转动画
*/
public void rotate(View view) {
// RotateAnimation r = new RotateAnimation(0, 270);
/*
* 参数1:旋转的起始角度
* 参数2:旋转的终止角度
* 参数3:旋转中心的x轴取值参照方式
* 参数4:中心点x轴的取值(0.5f表示相对与原图的0.5倍)
* 参数5:旋转中心的y轴取值参照方式
* 参数6:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
*/
r = new RotateAnimation(360, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
r.setDuration(300);
r.setRepeatCount(2);
r.setRepeatMode(Animation.REVERSE);
// 开始播放动画
iv.startAnimation(r);
}
/**
* 缩放
*/
public void scale(View view){
/*
* 参数1:x方向起始大小(1f表示原图大小)
* 参数2:x方向终止大小(0.2f表示原图的0.2倍)
* 参数3:y方向起始大小(1f表示原图大小)
* 参数4:y方向终止大小(0.2f表示原图的0.2倍)
* 参数5:缩放中心点x轴取值的参照方式
* 参数6: 中心点x轴的取值(0.5f表示相对与原图的0.5倍)
* 参数7:缩放中心点y轴取值参照方式
* 参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
*/
s = new ScaleAnimation(1,10, 1, 100f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
// 设置显示时间长度
s.setDuration(20);
// 设置重复次数
s.setRepeatCount(3);
// 设置动画重复的模式
s.setRepeatMode(Animation.REVERSE);
// 在ImageView上播放动画
iv.startAnimation(s);
}
/**
* 动画的合集
*/
public void set(View v){
AnimationSet set = new AnimationSet(false);
set.addAnimation(a);
set.addAnimation(r);
set.addAnimation(t);
set.addAnimation(s);
// 在ImageView上播放动画
iv.startAnimation(set);
}
}