Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件。
包括:旋转,缩放,淡入淡出
分类:分两大类
1.Tweened Animations
该类Animations提供了旋转,移动,伸展和淡出等等效果
2.Frame-by-Frame Animations
这一列Animations可以创建一个Drawable序列,这些Drawable可以按照指定的
时间间歇一个一个的显示。(像放电影一样)
程序功能:
程序中有四个按钮(Alpha, Scale, Rotate, Translate)和一个图片:
分别点击四个按钮,则图片和对应的按钮分别执行:淡入淡出、缩放、旋转、移动动画效果。
代码如下:
main.xml同上一篇
AppMain.java
package lxy.litsoft;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class AppMain extends Activity {
//声明对象
private ImageView imageView;
private Button btAlpha;
private Button btScale;
private Button btRotate;
private Button btTranslate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化对象
imageView = (ImageView)findViewById(R.id.imageView01);
btAlpha = (Button)findViewById(R.id.button01);
btAlpha.setOnClickListener(new ButtonListener());
btScale = (Button)findViewById(R.id.button02);
btScale.setOnClickListener(new ButtonListener());
btRotate = (Button)findViewById(R.id.button03);
btRotate.setOnClickListener(new ButtonListener());
btTranslate = (Button)findViewById(R.id.button04);
btTranslate.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
if(v.getId() == R.id.button01){ //Alpha:淡入淡出效果
Animation animation = AnimationUtils.loadAnimation(AppMain.this, R.anim.alpha);
imageView.startAnimation(animation);
btAlpha.startAnimation(animation);
} else if(v.getId() == R.id.button02){ //Scale:缩放效果
Animation animation = AnimationUtils.loadAnimation(AppMain.this, R.anim.scale);
imageView.startAnimation(animation);
btScale.startAnimation(animation);
} else if(v.getId() == R.id.button03){ //Rotate:旋转效果
Animation animation = AnimationUtils.loadAnimation(AppMain.this, R.anim.rotate);
imageView.startAnimation(animation);
btRotate.startAnimation(animation);
}else if(v.getId() == R.id.button04){ //Translate:移动效果
Animation animation = AnimationUtils.loadAnimation(AppMain.this, R.anim.translate);
imageView.startAnimation(animation);
btTranslate.startAnimation(animation);
}
}
}
}
res/anim/alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1000" />
</set>
res/anim/scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="1.5"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
/>
</set>
res/anim/rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" />
</set>
res/anim/translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="50%"
android:toXDelta="0%"
android:fromYDelta="50%"
android:toYDelta="0%"
android:duration="1000" />
</set>
其他属性:
android:repeatCount="1" //动画重复次数
android:repeatMode="reverse" //重复执行的模式为反向执行
android:repeatMode="restart" //重复执行的模式为跟第一次一样
android:startOffset="0" //开始执行动画的时间偏移量(延时)
android:fillBefore="true" //动画结束时停到开始的状态
android:fillAfter="true" //动画结束时停到结束的状态