今天写了个欢迎界面用到RotateAnimation 、ScaleAnimation 、AlphaAnimation 这三个动画: 先贴主要代码,下面在讲
public class WelcomeUI extends Activity { private static final long animation_duration = 1500; private View mRootView; //欢迎界面根视图ID @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); //初始化根视图ID mRootView = findViewById(R.id.welcome_root); //旋转动画 RotateAnimation rotateAnimation = new RotateAnimation(0f, //起始角度 360f, //终止角度 Animation.RELATIVE_TO_SELF, //可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT 0.5f, //x坐标的伸缩值 Animation.RELATIVE_TO_SELF, //可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT 0.5f); //y坐标的伸缩值 rotateAnimation.setDuration(animation_duration); //设置动画时长 rotateAnimation.setFillAfter(true); //设置动画结束后停留的位置 //缩放动画 ScaleAnimation scaleAnimation = new ScaleAnimation(0f, //动画起始时 X坐标上的伸缩尺寸 1f, //动画结束时 X坐标上的伸缩尺寸 0f, //动画起始时 y坐标上的伸缩尺寸 1f, //动画结束时 y坐标上的伸缩尺寸 Animation.RELATIVE_TO_SELF, //可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT 0.5f, //x坐标的伸缩值 Animation.RELATIVE_TO_SELF, //可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT 0.5f); //y坐标的伸缩值 scaleAnimation.setDuration(animation_duration); rotateAnimation.setFillAfter(true); //淡入动画 AlphaAnimation alphaAnimation = new AlphaAnimation(0,1); alphaAnimation.setDuration(animation_duration); rotateAnimation.setFillAfter(true); //动画集合 AnimationSet set = new AnimationSet(false); //是否加入Interpolator可以定义动画播放的速度,对所有对象都有效 set.addAnimation(rotateAnimation); set.addAnimation(scaleAnimation); set.addAnimation(alphaAnimation); //设置动画 mRootView.startAnimation(set); } }下面是布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/welcome_root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash_bg_newyear"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/splash_sheep_newyear" /> </RelativeLayout>Android的动画有4种,RatateAnimation旋转,ScaleAnimation 缩放,AlphaAnimation淡入,还有一个是TranslateAnimation移动。知道这四个动画的参数后基本就会了,前三个动画上面的代码已经写有了,并且有注释,这个例子没用到移动,照葫芦画瓢弄懂他的参数就好了这里贴一个连接,这个帖子写得很好: 这里写链接内容
当设置多个动画的时候,需要一个动画集合:
//动画集合 AnimationSet set = new AnimationSet(false); //是否加入Interpolator可以定义动画播放的速度,对所有对象都有效 set.addAnimation(rotateAnimation); set.addAnimation(scaleAnimation); set.addAnimation(alphaAnimation);有个主意的地方是动画集可以加入Interpolator可以定义动画播放的速度,对所有动画有效
总结:需要什么做动画就为他取个ID,然后用这个ID设置动画,多个动画的时候可用动画集。主要是弄清参数的意思! ss 初学者,可能写的不好,主要是帮助自己记忆