package com.example.tweenanimdemo01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
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.image);
}
/**
* 位移动画
* fromXType 指定开始时x轴上坐标的类型. Animation.ABSOLUTE 绝对的, Animation.RELATIVE_TO_SELF相对于自身, or Animation.RELATIVE_TO_PARENT相对于父控件.
* fromXValue开始动画时x轴上坐标的值. 如果类型是Animation.ABSOLUTE,可以是一个绝对的数值;其他情况是一个百分比
* toXType 指定结束动画位置x轴上坐标的类型
* toXValue 结束位置x轴上坐标值
* fromYType
* fromYValue
* toYType
* toYValue
* @param view
*/
public void tranlate(View view) {
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
//设置动画持续的时间
animation.setDuration(3000);
//设置动画重复的次数
animation.setRepeatCount(Animation.INFINITE);
/**
* 动画到达最后位置的时候重复的模式
* 默认的是RESTART---重新开始
*
* 只有在重复次数大于0或者INFINITE.
*/
animation.setRepeatMode(Animation.REVERSE);
//动画执行等待的时间
animation.setStartOffset(1000);
//设置插值器(指定的动画效果)
animation.setInterpolator(new CycleInterpolator(30));
imageView.setAnimation(animation);
}
/**
* 透明度动画
*
* fromAlpha 开始动画时的透明度 0.0代表完全透明 1.0代表没有透明度
* toAlpha 动画结束时的透明度.
*
* @param view
*/
public void alpha(View view) {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 缩放动画
* float fromX,
* float toX,
* float fromY,
* float toY,
* int pivotXType, 指定缩放x轴上的类型
* float pivotXValue, 0.5f就代表x轴上中心点
* int pivotYType,
* float pivotYValue
* @param view
*/
public void scale(View view) {
ScaleAnimation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 旋转动画
* fromDegrees, 起始的角度
* toDegrees,终止的角度值
* pivotXType, pivotXValue, pivotYType, pivotYValue 指定类型和数值来确定中心点
* @param view
*/
public void rotate(View view) {
RotateAnimation animation = new RotateAnimation(0f, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 组合动画
* @param view
*/
public void zuhe(View view) {
RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.8f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.8f);
//组合动画是否使用同一个插值器
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(3000);
animationSet.setFillAfter(true);//动画结束以后 控件是否保持最后的状态 默认不保持
imageView.setAnimation(animationSet);
}
/**
* xml布局实现补间动画
* @param view
*/
public void xmlMethod(View view) {
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.se);
imageView.setAnimation(loadAnimation);
loadAnimation.setAnimationListener(new AnimationListener() {
//开始的
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, "开始", 0).show();
}
//重复的
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "重复", 0).show();
}
//结束的
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "结束", 0).show();
}
});
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
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.image);
}
/**
* 位移动画
* fromXType 指定开始时x轴上坐标的类型. Animation.ABSOLUTE 绝对的, Animation.RELATIVE_TO_SELF相对于自身, or Animation.RELATIVE_TO_PARENT相对于父控件.
* fromXValue开始动画时x轴上坐标的值. 如果类型是Animation.ABSOLUTE,可以是一个绝对的数值;其他情况是一个百分比
* toXType 指定结束动画位置x轴上坐标的类型
* toXValue 结束位置x轴上坐标值
* fromYType
* fromYValue
* toYType
* toYValue
* @param view
*/
public void tranlate(View view) {
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
//设置动画持续的时间
animation.setDuration(3000);
//设置动画重复的次数
animation.setRepeatCount(Animation.INFINITE);
/**
* 动画到达最后位置的时候重复的模式
* 默认的是RESTART---重新开始
*
* 只有在重复次数大于0或者INFINITE.
*/
animation.setRepeatMode(Animation.REVERSE);
//动画执行等待的时间
animation.setStartOffset(1000);
//设置插值器(指定的动画效果)
animation.setInterpolator(new CycleInterpolator(30));
imageView.setAnimation(animation);
}
/**
* 透明度动画
*
* fromAlpha 开始动画时的透明度 0.0代表完全透明 1.0代表没有透明度
* toAlpha 动画结束时的透明度.
*
* @param view
*/
public void alpha(View view) {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 缩放动画
* float fromX,
* float toX,
* float fromY,
* float toY,
* int pivotXType, 指定缩放x轴上的类型
* float pivotXValue, 0.5f就代表x轴上中心点
* int pivotYType,
* float pivotYValue
* @param view
*/
public void scale(View view) {
ScaleAnimation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 旋转动画
* fromDegrees, 起始的角度
* toDegrees,终止的角度值
* pivotXType, pivotXValue, pivotYType, pivotYValue 指定类型和数值来确定中心点
* @param view
*/
public void rotate(View view) {
RotateAnimation animation = new RotateAnimation(0f, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(animation);
}
/**
* 组合动画
* @param view
*/
public void zuhe(View view) {
RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.8f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.8f);
//组合动画是否使用同一个插值器
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(3000);
animationSet.setFillAfter(true);//动画结束以后 控件是否保持最后的状态 默认不保持
imageView.setAnimation(animationSet);
}
/**
* xml布局实现补间动画
* @param view
*/
public void xmlMethod(View view) {
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.se);
imageView.setAnimation(loadAnimation);
loadAnimation.setAnimationListener(new AnimationListener() {
//开始的
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, "开始", 0).show();
}
//重复的
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "重复", 0).show();
}
//结束的
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "结束", 0).show();
}
});
}
}