public class MainActivity extends Activity implements OnClickListener
{
/**
* 定义四个按钮和一张图片
*/
private ImageView imageView = null;
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
/**
* 初始化界面
*/
public void initView()
{
imageView = (ImageView) findViewById(R.id.imageViewId);
rotateButton = (Button) findViewById(R.id.rotateButtonId);
translateButton = (Button) findViewById(R.id.translateButtonId);
scaleButton = (Button) findViewById(R.id.scaleButtonId);
alphaButton = (Button) findViewById(R.id.alphaButtonId);
rotateButton.setOnClickListener(this);
scaleButton.setOnClickListener(this);
alphaButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
int switchID = v.getId();
switch (switchID)
{
case R.id.alphaButtonId:
{
AnimationSet animationSet = new AnimationSet(true);//创建一个AnimationSet对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//创建一个AlphaAnimation对象
alphaAnimation.setDuration(1000);//设置动画执行的时间(单位:毫秒)
animationSet.addAnimation(alphaAnimation);//将AlphaAnimation对象添加到AnimationSet当中
imageView.startAnimation(animationSet);//使用ImageView的startAnimation方法开始执行动画
break;
}
case R.id.rotateButtonId:
{
AnimationSet animationSet = new AnimationSet(true);
/**
* 前两个参数定义旋转的起始和结束的度数,后两个参数定义圆心的位置
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
break;
}
case R.id.scaleButtonId:
{
AnimationSet animationSet = new AnimationSet(true);
/**
* 围绕一个点伸缩
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
break;
}
case R.id.translateButtonId:
{
AnimationSet animationSet = new AnimationSet(true);
/**
* x和y轴的起始和结束位置
*/
TranslateAnimation translateAnimation = new TranslateAnimation
(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f
);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
break;
}
}
}
}
Animation主要有四大属性,分别是淡入淡出,绕轴旋转,变化大小,位移变化,如图:
ANIMATION TYPE | ATTRIBUTE | VALID VALUES |
Alpha | fromAlpha / toAlpha | Float from 0 to 1 |
Scale | fromXScale toXScale | Float from 0 to 1 |
fromYScale toYScale | Float from 0 to 1 | |
pivotX pivotY | String of the percentage of graphic width height from 0% to 100% | |
Translate | FromX toX | Float from 0 to 1 |
fromY toY | Float from 0 to 1 | |
Rotate | fromDegrees toDegrees | Float from 0 to 360 |
pivotX pivotY | String of the percentage of graphic width height from 0% to 100% |
这些属性还有一些共同的方法:
1.setDuration(long durationMills)
设置动画持续时间(单位:毫秒)
2. setFillAfter(boolean fillAfter)
如果fillAfter的值为true,则动画执行后,控件将停留在控件结束的状态。
3. setFillBefore(Boolean fillBefore)
如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态。
4.setStartOffSet(long startOffSet)
设置动画执行之前的等待时间。
5.setRepeatCount(int repeatCount)
设置动画重复执行的次数。