Animation从总体来说可以分为两类:
Tweened Animations:该类提供了旋转,移动,伸展,淡入淡出等效果
Frame-By-Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定的事件间隔一个一个显示,和动画片差不多
一、Tweened Animations
Tweened Animations也有四种类型:
Alpha:淡入淡出效果
Scale:缩放效果
Rotate:旋转效果
Translate:移动效果
设置动画效果可以在XML文件中设置,也可以在Java代码中设置。我们先来讲解在Java代码中怎么设置这四种动画效果
Java代码中设置动画效果的步骤:
创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
根据需要创建相应的Animation对象
根据需要对Animation对象的各个属性进行设值
将Animation对象添加到AnimationSet对象中
使用控件的startAnimation()方法执行AnimationSet
Java代码中的通用属性:
setDuration(long durationMillis):设置动画持续事件(单位:毫秒)
setFillAfter(boolean fillAfter):如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
setFillBefore(boolean fillBefore):如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
setStartOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
setRepeatCount(int repeatCount):设置动画重复的次数
setInterpolator(Interpolator i):设置动画的变化速度
setInterpolator(new AccelerateDecelerateInterpolator()):先加速,后减速
setInterpolator(new AccelerateInterpolator()):加速
setInterpolator(new DecelerateInterpolator()):减速
setInterpolator(new CycleInterpolator()):动画循环播放特定次数,速率改变沿着正弦曲线
setInterpolator(new LinearInterpolator()):匀速 以及其他一些特定的动画效果
Java代码中设置动画效果的Demo(AnimationDemo01):
1 main.xml 2 3 <?xml version="1.0" encoding="utf-8"?> 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 android:orientation="vertical" > 8 9 <ImageView 10 android:id="@+id/imageView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_marginTop="100dip" 14 android:src="@drawable/ic_launcher" /> 15 16 <LinearLayout 17 android:layout_width="fill_parent" 18 android:layout_height="fill_parent" 19 android:gravity="bottom" 20 android:orientation="vertical" > 21 22 <Button 23 android:id="@+id/alphaButton" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:text="测试alpha动画效果" /> 27 28 <Button 29 android:id="@+id/scaleButton" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:text="测试scale动画效果" /> 33 34 <Button 35 android:id="@+id/rotateButton" 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:text="测试rotate动画效果" /> 39 40 <Button 41 android:id="@+id/translateButton" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:text="测试translate动画效果" /> 45 </LinearLayout> 46 47 </LinearLayout>