package com.anim;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
ImageView imageView1;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
ImageView imageView5;
Handler handler;
Animation translateAnimation;
Animation alphaAnimation;
Animation scaleAnimation;
Animation rotateAnimation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
imageView1 = (ImageView)findViewById(R.id.imageview1);
imageView2 = (ImageView)findViewById(R.id.imageview2);
imageView3 = (ImageView)findViewById(R.id.imageview3);
imageView4 = (ImageView)findViewById(R.id.imageview4);
imageView5 = (ImageView)findViewById(R.id.imageview5);
handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
// 平移动画
// 1.代码实现
// Animation translateAnimation = new TranslateAnimation(0, 50.0f, 0, 100.0f);
// translateAnimation.setDuration(2000);
// 2.加载xml文件
translateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_translate);
// 添加动画播放的加速/减速,即缓冲效果
// 1.线性播放,即匀速播放,这个是动画的默认效果
// LinearInterpolator linearInterpolator = new LinearInterpolator();
// translateAnimation.setInterpolator(linearInterpolator);// 添加到动画中
// 2.加速播放,参数值越大,加速效果越明显,即开始时慢,结束时快
Interpolator accelerateInterpolator = new AccelerateInterpolator(2.0f);
translateAnimation.setInterpolator(accelerateInterpolator);
// 3.减速播放,与上类似
// Interpolator decelerateInterpolator = new DecelerateInterpolator(0.8f);
// translateAnimation.setInterpolator(decelerateInterpolator);
// 4.开始和结束较慢,在动画播放中间时段会先加速后减速
// Interpolator accelerateDecelerateInterpolator = new AccelerateDecelerateInterpolator();
// translateAnimation.setInterpolator(accelerateDecelerateInterpolator);
// 5.实现循环播放的动画效果
// Interpolator cycleInterpolator = new CycleInterpolator(2.0f);
// translateAnimation.setInterpolator(cycleInterpolator);
imageView1.startAnimation(translateAnimation);
handler.sendEmptyMessageDelayed(2, 3000);
break;
case 2:
// 渐变动画
// 1.代码实现(实现效果是从完全不透明变成完全透明,在1秒钟内完成)
// Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
// animation2.setDuration(1000);
// 2.加载xml文件
alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_alpha);
imageView2.startAnimation(alphaAnimation);
handler.sendEmptyMessageDelayed(3, 2000);
break;
case 3:
// 缩放动画
// 1.代码实现
// Animation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.ABSOLUTE,10.0f,Animation.ABSOLUTE, 10.0f);
// scaleAnimation.setDuration(1000);
// 2.加载xml文件
scaleAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_scale);
imageView4.startAnimation(scaleAnimation);
handler.sendEmptyMessageDelayed(4, 2000);
break;
case 4:
// 旋转动画(实现效果是以(10.0f,10.0f)为旋转轴心点,将组件从0度旋转到270度)
// 1.代码实现 (正数表示顺时针旋转)
// Animation rotateAnimation = new RotateAnimation(0.0f, 270.0f, Animation.ABSOLUTE, 10.0f, Animation.ABSOLUTE, 10.0f);
// rotateAnimation.setDuration(3000);
// 2.加载xml文件
rotateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_rotate);
rotateAnimation.setRepeatCount(Animation.INFINITE);// 设置播放次数:传入一个负数即可实现无限次播放
rotateAnimation.setRepeatMode(Animation.REVERSE);// 设置播放模式:这里设置为播放完毕后,从后向前反过来播放,还有一个是RESTART,即从头播放
rotateAnimation.setStartTime(AnimationUtils.currentAnimationTimeMillis()+2000);// 设置播放的开始时间:这里通过工具类获取当前系统时间,然后再1000,即延迟1秒,如果传入的值为负数,则立即播放,可以用系统的变量:Animation.START_ON_FIRST_FRAME(-1)
rotateAnimation.setStartOffset(3000);// 设置播放偏移时间,即动画开始播放之前延时多少毫秒,加上上面的2秒,则动画首次播放被延迟了5秒,之后重复播放过程中,每次播放都会被延迟3秒
imageView4.startAnimation(rotateAnimation);
handler.sendEmptyMessageDelayed(5, 6000);
/**
* 另外这里讲解一下其他几个方法:
* 1.setFillBefore(boolean fillBefore);当设置为true时,那么在动画开始播放之前就会进行动画变换操作,默认为true
* 2.setFillAfter(boolean fillAfter);当设置为true时,那么在动画结束之后(只有一帧)仍会进行动画变换操作,默认为false
* 3.setFillEnabled(boolean fillEnabled);只有将其设置为true,上述的两个属性才有意义,否则无论是开始还是结束都会进行动画变换操作,默认为false
*/
break;
case 5:
// 将各种动画效果集合到一块,应用到组件上
// 1.代码实现
// AnimationSet animationSet = new AnimationSet(false);// 参数说明:true表示应用AnimationSet的Interpolator效果,false表示应用各个动画对象自己的Interpolator效果
// animationSet.addAnimation(new TranslateAnimation(0, 50.0f, 0, 50.0f));
// animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
// animationSet.setDuration(3000);
// animationSet.setRepeatCount(Animation.INFINITE);
// animationSet.setRepeatMode(Animation.RESTART);
// 2.加载xml文件
Animation animationSet = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_set);
imageView3.setAnimation(animationSet);
// handler.sendEmptyMessageDelayed(6, 1000);
// 设置背景
MainActivity.this.getWindow().setBackgroundDrawableResource(R.drawable.blue);
break;
case 6:
Animation animation3D = new My3DAnimation(0.0f, 360.0f, 100, 180, 100.0f, true);
animation3D.setDuration(3000);
imageView5.startAnimation(animation3D);
if (m == -1) {
handler.sendEmptyMessageDelayed(1, 3000);
m++;
}
handler.sendEmptyMessageDelayed(6, 3000);
break;
}
};
};
handler.sendEmptyMessageDelayed(6, 1000);
}
private int m = -1;
}