package com.example.android_1507e_animation_day16_demo03_java;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
image=(ImageView) findViewById(R.id.image);
}
//位移
public void translate(View v){
/**
* fromXType 开始X轴的类型. One of Animation.ABSOLUTE,绝对的 Animation.RELATIVE_TO_SELF,相对于自身 or Animation.RELATIVE_TO_PARENT. 相对于父窗体
* fromXValue X轴开始的位置.
* toXType 结束X轴的类型 One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
* toXValue X轴结束的位置
* fromYType 开始Y轴的类型
* fromYValue 开始Y轴的位置
* toYType Y轴结束的类型
* toYValue Y轴结束的位置
*/
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);
//设置执行的时间
translateAnimation.setDuration(3000);
//设置是否来回执行 RESTART 正常 REVERSE 来回执行
translateAnimation.setRepeatMode(Animation.REVERSE);
//设置执行的次数
translateAnimation.setRepeatCount(2);
//是否保存最后的位置
translateAnimation.setFillAfter(true);
//设置插值器
translateAnimation.setInterpolator(new AccelerateInterpolator());
//执行动画
//
image.setAnimation(translateAnimation);
image.startAnimation(translateAnimation);
}
//旋转
public void rotate(View v){
/**
*
* fromDegrees, 开始的角度
* toDegrees,
结束的角度
* pivotXType
X轴原点的类型
* pivotXValue, X原点的位置
* pivotYType, Y轴原点的类型
* pivotYValue Y原点的位置
*/
RotateAnimation rotateAnimation=new RotateAnimation(0, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//设置执行的时间
rotateAnimation.setDuration(3000);
//设置执行的次数
rotateAnimation.setRepeatCount(2);
//设置是否来回执行 RESTART 正常 REVERSE 来回执行
rotateAnimation.setRepeatMode(Animation.REVERSE);
image.startAnimation(rotateAnimation);
}
//透明
public void alpha(View v){
/**
* fromAlpha, 开始透明
* toAlpha 结束透明
*/
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(3000);
alphaAnimation.setRepeatCount(2);
image.startAnimation(alphaAnimation);
}
//缩放
public void scale(View v){
/**
* fromX, 开始X轴水平的位置
* toX, 结束X轴水平的位置
* fromY, 开始Y轴水平的位置
* toY,结束Y轴水平的位置
* pivotXType, 规定原点的类型以及位置
* pivotXValue,
* pivotYType,
* pivotYValue
*
*/
ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setRepeatCount(1);
//重复执行
scaleAnimation.setRepeatMode(Animation.REVERSE);
image.startAnimation(scaleAnimation);
}
//组合动画
public void set(View v){
//是否使用通用一个插值器
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f, 0.0f);
RotateAnimation rotateAnimation=new RotateAnimation(0, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
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.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(10000);
image.startAnimation(animationSet);
animationSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.i("TAG", "开始");
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Log.i("TAG", "重复");
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.i("TAG", "结束");
}
});
}
}