Android 动画AlphaAnimation类方法

来源:互联网 发布:淘宝steam慈善包送大作 编辑:程序博客网 时间:2024/05/10 07:06

当我们打开应用时,出现在我们眼前的是一张渐变图片。此图可以是应用欢迎图片,也可以广告海报(服务可以推送广告),就是用到了动画AlphaAnimation完成的。


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);
    }


有点乱。

转自:http://www.apkbus.com/blog-134260-54359.html

2 0
原创粉丝点击