package com.saiermeng.annimation;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv_xfj);
}
public void alpha(View v){
// 创建透明度的动画对象
// fromAlpha 开始透明度的值 0~1.0 由完全透明到完全不透明
// toAlpha 结束透明度的值
AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
//设置动画重复播放的次数
aa.setRepeatCount(2);
//设置动画播放的顺序的模式:REVERSE 相反的顺序
aa.setRepeatMode(AlphaAnimation.REVERSE);
//设置动画播放的总时间长度
aa.setDuration(3000);
//在IMageView上播放动画
iv.startAnimation(aa);
}
public void rotate(View v) {
// 创建旋转的动画对象
// fromDegrees 旋转的开始角度 0~360
// toDegrees 旋转的结束角度
// pivotXType 在水平方向相对旋转的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
// pivotXValue 相对的位置,如自己宽度的0.5f
// pivotYType 在垂直方向相对选装的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
// pivotYValue 相对的位置,如自己高度的0.5f
RotateAnimation ra = new RotateAnimation(0, 360,
1, 0.5f,
1, 0.5f);
// 设置动画重复播放的次数
ra.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
ra.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
ra.setDuration(3000);
// 在imageview上播放动画
iv.startAnimation(ra);
}
public void scale(View v) {
// 创建缩放的动画对象
// fromX 开始缩放时宽度的比例 最小为0,放大一倍为1.0f
// toX 结束缩放时宽度的比例
// fromY 开始缩放时高度开始缩放时宽度的比例 最小为0,放大一倍为1.0f
// toY 结束缩放时高度的比例的比例 最小为0,放大一倍为1.0f
// pivotXType 在水平方向相对缩放的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
// pivotXValue 相对的位置,如自己宽度的0.5f
// pivotYType 在垂直方向相对缩放的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
// pivotYValue 相对的位置,如自己高度的0.5f
ScaleAnimation sa = new ScaleAnimation(0, 2.0f, 0, 2.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画重复播放的次数
sa.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
sa.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
sa.setDuration(3000);
// 在imageview上播放动画
iv.startAnimation(sa);
}
public void trans(View v) {
// 创建平移的动画对象
// fromXType 在水平方向平移时相对的类型,
// fromXValue 平移的开始位置
// toXType 在水平方向平移时相对的类型,
// toXValue 在水平方向平移倍数
// fromYType 在垂直方向平移时相对的类型,
// fromYValue 平移的开始位置
// toYType 在垂直方向平移时相对的类型,
// toYValue 在垂直方向平移倍数
TranslateAnimation ta = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 3.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 3.0f);
// 设置动画重复播放的次数
ta.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
ta.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
ta.setDuration(3000);
// 在imageview上播放动画
iv.startAnimation(ta);
}
public void set(View v) {
// 创建动画集合
// shareInterpolator 插筒, true 表示一个动画接一个的播放动画 false会把几种动画总到一起播放
AnimationSet set =new AnimationSet(false);
AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
// 设置动画重复播放的次数
aa.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
aa.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
aa.setDuration(3000);
set.addAnimation(aa);
RotateAnimation ra = new RotateAnimation(0, 360,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画重复播放的次数
ra.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
ra.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
ra.setDuration(3000);
set.addAnimation(ra);
ScaleAnimation sa = new ScaleAnimation(0, 2.0f, 0, 2.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画重复播放的次数
sa.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
sa.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
sa.setDuration(3000);
set.addAnimation(sa);
TranslateAnimation ta = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 3.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 3.0f);
// 设置动画重复播放的次数
ta.setRepeatCount(2);
// 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
ta.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复播放的总时间长度
ta.setDuration(3000);
set.addAnimation(ta);
iv.startAnimation(set);
}
}
简单的四种动画Demo
最新推荐文章于 2024-04-23 00:56:43 发布