package com.bwei.day_06_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 imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
/**
* 透明
*
* @param v
*/
public void alpha(View v) {
// 得到AlphaAnimation对象,参数一:开始的透明度,参数二:结束的透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
// 持续时间
alphaAnimation.setDuration(2000);
// 动画结束时是否保持状态
alphaAnimation.setFillAfter(true);
// 动画重复次数
alphaAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
imageView.startAnimation(alphaAnimation);
}
/**
* 放缩
*
* @param v
*/
public void scale(View v) {
/*
* ScaleAnimation scaleAnimation=new ScaleAnimation(fromX, toX, fromY,
* toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
*/
// 参数一:开始的x坐标,参数二:动画结束的x坐标,参数三:开始的y坐标,参数四:动画结束的y坐标,
// 参数五:放缩时的中心点的x坐标位置的相对类型(有绝对,相对于父亲,相对于自己),参数六:中心点(放缩的中心点)x轴的位置,
// 参数七:放缩时的中心点的Y坐标位置的相对类型(有绝对,相对于父亲,相对于自己),参数八:中心点(放缩的中心点)Y轴的位置,
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0.0f, 1.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 持续时间
scaleAnimation.setDuration(2000);
// 动画结束时是否保持状态
scaleAnimation.setFillAfter(true);
// 动画重复次数
scaleAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
scaleAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(scaleAnimation);
}
/**
* 位移
*
* @param v
*/
public void translate(View v) {
// TranslateAnimation translateAnimation=new
// TranslateAnimation(fromXType, fromXValue, toXType, toXValue,
// fromYType, fromYValue, toYType, toYValue)
// 参数一:x轴起始点的相对类型,参数二:x轴的起始位置,参数三:x轴停止点的相对类型,参数四:x轴最终移动的距离(与起始点的相对距离)
// //参数五:Y轴起始点的相对类型,参数六:Y轴的起始位置,参数七:Y轴停止点的相对类型,参数八:Y轴最终移动的距离(与起始点的相对距离)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 1.0f);
// 持续时间
translateAnimation.setDuration(2000);
// 动画结束时是否保持状态
translateAnimation.setFillAfter(true);
// 动画重复次数
translateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
translateAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(translateAnimation);
}
/**
* 旋转
*
* @param v
*/
public void rotate(View v) {
// RotateAnimation rotateAnimation=new RotateAnimation(fromDegrees,
// toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
// 参数一:起始角度,参数二:旋转角度,
// 参数三:圆心x轴坐标的相对位置类型,参数四:圆心x轴的位置,到起始点的距离
// 参数五:圆心Y轴坐标的相对位置类型,参数六:圆心y轴的位置,到起始点的距离
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
1.0f);
// 持续时间
rotateAnimation.setDuration(2000);
// 动画结束时是否保持状态
rotateAnimation.setFillAfter(true);
// 动画重复次数
rotateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
rotateAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(rotateAnimation);
}
/*
* 动画合集
*/
public void set(View v) {
AnimationSet animationSet=new AnimationSet(false);
//
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
// 持续时间
alphaAnimation.setDuration(2000);
// 动画结束时是否保持状态
alphaAnimation.setFillAfter(true);
// 动画重复次数
alphaAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
//===============================================================================
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0.0f, 1.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 持续时间
scaleAnimation.setDuration(2000);
// 动画结束时是否保持状态
scaleAnimation.setFillAfter(true);
// 动画重复次数
scaleAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
scaleAnimation.setRepeatMode(Animation.RESTART);
//======================================================================
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
1.0f);
// 持续时间
rotateAnimation.setDuration(2000);
// 动画结束时是否保持状态
rotateAnimation.setFillAfter(true);
// 动画重复次数
rotateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
rotateAnimation.setRepeatMode(Animation.RESTART);
/
//往动画集合里添加动画
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.setDuration(5000);
animationSet.setRepeatCount(3);
imageView.startAnimation(animationSet);
}
}
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 imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
/**
* 透明
*
* @param v
*/
public void alpha(View v) {
// 得到AlphaAnimation对象,参数一:开始的透明度,参数二:结束的透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
// 持续时间
alphaAnimation.setDuration(2000);
// 动画结束时是否保持状态
alphaAnimation.setFillAfter(true);
// 动画重复次数
alphaAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
imageView.startAnimation(alphaAnimation);
}
/**
* 放缩
*
* @param v
*/
public void scale(View v) {
/*
* ScaleAnimation scaleAnimation=new ScaleAnimation(fromX, toX, fromY,
* toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
*/
// 参数一:开始的x坐标,参数二:动画结束的x坐标,参数三:开始的y坐标,参数四:动画结束的y坐标,
// 参数五:放缩时的中心点的x坐标位置的相对类型(有绝对,相对于父亲,相对于自己),参数六:中心点(放缩的中心点)x轴的位置,
// 参数七:放缩时的中心点的Y坐标位置的相对类型(有绝对,相对于父亲,相对于自己),参数八:中心点(放缩的中心点)Y轴的位置,
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0.0f, 1.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 持续时间
scaleAnimation.setDuration(2000);
// 动画结束时是否保持状态
scaleAnimation.setFillAfter(true);
// 动画重复次数
scaleAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
scaleAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(scaleAnimation);
}
/**
* 位移
*
* @param v
*/
public void translate(View v) {
// TranslateAnimation translateAnimation=new
// TranslateAnimation(fromXType, fromXValue, toXType, toXValue,
// fromYType, fromYValue, toYType, toYValue)
// 参数一:x轴起始点的相对类型,参数二:x轴的起始位置,参数三:x轴停止点的相对类型,参数四:x轴最终移动的距离(与起始点的相对距离)
// //参数五:Y轴起始点的相对类型,参数六:Y轴的起始位置,参数七:Y轴停止点的相对类型,参数八:Y轴最终移动的距离(与起始点的相对距离)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 1.0f);
// 持续时间
translateAnimation.setDuration(2000);
// 动画结束时是否保持状态
translateAnimation.setFillAfter(true);
// 动画重复次数
translateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
translateAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(translateAnimation);
}
/**
* 旋转
*
* @param v
*/
public void rotate(View v) {
// RotateAnimation rotateAnimation=new RotateAnimation(fromDegrees,
// toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
// 参数一:起始角度,参数二:旋转角度,
// 参数三:圆心x轴坐标的相对位置类型,参数四:圆心x轴的位置,到起始点的距离
// 参数五:圆心Y轴坐标的相对位置类型,参数六:圆心y轴的位置,到起始点的距离
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
1.0f);
// 持续时间
rotateAnimation.setDuration(2000);
// 动画结束时是否保持状态
rotateAnimation.setFillAfter(true);
// 动画重复次数
rotateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
rotateAnimation.setRepeatMode(Animation.RESTART);
imageView.startAnimation(rotateAnimation);
}
/*
* 动画合集
*/
public void set(View v) {
AnimationSet animationSet=new AnimationSet(false);
//
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
// 持续时间
alphaAnimation.setDuration(2000);
// 动画结束时是否保持状态
alphaAnimation.setFillAfter(true);
// 动画重复次数
alphaAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
//===============================================================================
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0.0f, 1.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 持续时间
scaleAnimation.setDuration(2000);
// 动画结束时是否保持状态
scaleAnimation.setFillAfter(true);
// 动画重复次数
scaleAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
scaleAnimation.setRepeatMode(Animation.RESTART);
//======================================================================
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
1.0f);
// 持续时间
rotateAnimation.setDuration(2000);
// 动画结束时是否保持状态
rotateAnimation.setFillAfter(true);
// 动画重复次数
rotateAnimation.setRepeatCount(2);
// 设置动画重复的模式(从头开始)(从尾开始)
rotateAnimation.setRepeatMode(Animation.RESTART);
/
//往动画集合里添加动画
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.setDuration(5000);
animationSet.setRepeatCount(3);
imageView.startAnimation(animationSet);
}
}