public class HelloAnimationActivity extends Activity { /** Called when the activity is first created. */ Button b_alpha; Button b_scale; Button b_rotate; Button b_translate; ImageView iv; AnimationSet animationSet; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView)findViewById(R.id.image); b_alpha = (Button)findViewById(R.id.alpha); b_scale = (Button)findViewById(R.id.scale); b_rotate = (Button)findViewById(R.id.rotate); b_translate = (Button)findViewById(R.id.translate); b_alpha.setOnClickListener(new AlpahListener()); b_scale.setOnClickListener(new ScaleListener()); b_rotate.setOnClickListener(new RotateListener()); b_translate.setOnClickListener(new TranslateListener()); } class AlpahListener implements OnClickListener{ @Override public void onClick(View v) { //创建一个AnimationSet对象 AnimationSet animationSet = new AnimationSet(true); //创建一个AlphaAnimation对象 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //设置动画执行的时间(单位:毫秒) alphaAnimation.setDuration(1000); //将AlphaAnimation对象添加到AnimationSet当中 animationSet.addAnimation(alphaAnimation); //使用ImageView的startAnimation方法开始执行动画 iv.startAnimation(animationSet); } } class ScaleListener implements OnClickListener{ /** * 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f) */ @Override public void onClick(View arg0) { 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); iv.startAnimation(animationSet); } } class RotateListener implements OnClickListener{ /** * 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心 */ @Override public void onClick(View arg0) { // TODO Auto-generated method stub animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(5000); animationSet.addAnimation(rotateAnimation); iv.setAnimation(rotateAnimation); } } class TranslateListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub animationSet = new AnimationSet(true); 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); animationSet.addAnimation(translateAnimation); iv.setAnimation(animationSet); } } }
Android:Animation的简单学习
内容版权声明:除非注明,否则皆为本站原创文章。