package com.example.unit5_animation_demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
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 img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
}
public void rotatetest(View v){
//旋转-需要确定中心点
RotateAnimation anim=new RotateAnimation(0, -3600,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
anim.setDuration(2000);
img.startAnimation(anim);
}
public void scaletest(View v){
//缩放-确定中心点位置
ScaleAnimation anim=new ScaleAnimation(1, 3, 1, 3,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 1f);
anim.setDuration(2000);
img.startAnimation(anim);
}
public void translatetest(View v){
//所有设定的值,都是偏移值
TranslateAnimation anim=new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0f,
Animation.RELATIVE_TO_PARENT, -0.51f,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_PARENT, -0.51f);
anim.setDuration(5000);
img.startAnimation(anim);
}
public void alphatest(View v){
//创建透明度变化的动画效果 1:完成不透明 0:完全透明
AlphaAnimation anim=new AlphaAnimation(1, 0);
//设置动画执行时间
anim.setDuration(2000);
//让动画停留在最后状态
anim.setFillAfter(true);
//设置重复的次数
anim.setRepeatCount(1);
//设置重复的模式 REVERSE:反向 restart:重复
anim.setRepeatMode(Animation.REVERSE);
//启动动画
img.startAnimation(anim);
}
}