Android 动画之AlphaAnimation应用详解

窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation。AlphaAnimation(0.01f, 1.0f); 从0.01f到1.0f渐变。学过flash的,应该对alpha值很了解,0.0是完全透明,1.0完全不透明。

 

 
public class MainActivity extends Activity { 
ImageView image; 
Button start; 
Button cancel; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
image = (ImageView) findViewById(R.id.main_img); 
start = (Button) findViewById(R.id.main_start); 
cancel = (Button) findViewById(R.id.main_cancel); 
/** 设置透明度渐变动画 */ 
final AlphaAnimation animation = new AlphaAnimation(1, 0); 
animation.setDuration(2000);//设置动画持续时间 
/** 常用方法 */ 
//animation.setRepeatCount(int repeatCount);//设置重复次数 
//animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态 
//animation.setStartOffset(long startOffset);//执行前的等待时间 
start.setOnClickListener(new OnClickListener() { 
public void onClick(View arg0) { 
image.setAnimation(animation); 
/** 开始动画 */ 
animation.startNow(); 
} 
}); 
cancel.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
/** 结束动画 */ 
animation.cancel(); 
} 
}); 
} 
}

 

 

 

一、所使用的技术:AlphaAnimation动画

1。官方描述:
An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of a Transformation
即:控制对象alpha水平的动画。这个动画可以通过改变alpha属性,达到渐进渐出的效果。

2。构造方法:AlphaAnimation(float fromAlpha, float toAlpha)
官方解释:Constructor to use when building an AlphaAnimation from code
即:使用代码实现渐变动画
如:AlphaAnimation(0.01f, 1.0f); 从0.01f到1.0f渐变。学过flash的,应该对alpha值很了解,0.0是完全透明,1.0完全不透明。

二、动画的实现
1。实例化对象
AlphaAnimation anim = new AlphaAnimation(0.01f, 1.0f);
2。设置动画持续时长(两秒)
anim.setDuration(2000);
3。添加事件监听
anim.setAnimationListener(new Animation.AnimationListener() {
            
    @Override
    public void onAnimationStart(Animation animation) {    
    }
            
    @Override
    public void onAnimationRepeat(Animation animation) {    
    }
            
    @Override
    public void onAnimationEnd(Animation animation) {
        //渐变动画结束后,执行此方法,跳转到主界面    
    }
});

4。为控件绑定动画效果
imageView.setAnimation(anim);

5。开始动画

anim.start();

 

看到上面两个地方开始动画的方法不一样,现在好像android里面已经有一个新的方法

 public void startAnimation(Animation animation) {
        animation.setStartTime(Animation.START_ON_FIRST_FRAME);
        setAnimation(animation);
        invalidateParentCaches();
        invalidate(true);
    }

posted @ 2016-02-14 13:50  阿冰的学习日记  阅读(1623)  评论(0编辑  收藏  举报