Animation主要有四大属性,分别是淡入淡出,绕轴旋转,变化大小,位移变化,如图:
这些属性还有一些共同的方法:
下面是一个实例代码:
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; } } } }
ok!