Android 动画AlphaAnimation类方法
當我們打開應用時,出現在我們眼前的是一張漸變圖片。此圖可以是應用歡迎圖片,也可以廣告海報(服務可以推送廣告),就是用到了動畫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
總結
以上是生活随笔為你收集整理的Android 动画AlphaAnimation类方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android inflate方法与 f
- 下一篇: Android Intent机制详解