android圖片漸隱漸現動畫效果
最近專案中需要用到一個過渡動畫,就是一個圖片漸隱漸現,然後重複五次後過渡動畫消失,首先說下我的思路,一個timer,重複執行,然後執行的時候肯定要不斷呼叫顯示、隱藏動畫。其中定時器每次重複的時間是動畫顯示(或隱藏)的時間
public class MainActivity extends ActionBarActivity {
staticint num = 0;//定義一個num來記錄次數,就是來記錄過渡動畫的次數
Timer timer;//定時器,重複執行動畫
private AlphaAnimation mHideAnimation= null;
private
static SeekBar seek; //顯示和隱藏的圖片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar)findViewById(R.id.timeline_new);
seek.setProgress(20);
seek.setOnSeekBarChangeListener(seekListener);
seek.bringToFront();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message=new Message();
message.what
mHandler.sendMessage(message);
}
}, 50, 2000);
}
//涉及到ui操作,返回到主執行緒中操作
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 0){
if( num % 2 != 0 ){
setHideAnimation(seek,2000);
}else{
setShowAnimation(seek,2000);
}
num++;
if(num>10){//重複次數夠了以後,將num置回初始值,關閉定時器
num=0;
timer.cancel();
}
}
}
};
//漸漸隱藏動畫
private void setHideAnimation( View view, int duration ){
if( null == view || duration < 0 ){
return;
}
if( null != mHideAnimation ){
mHideAnimation.cancel( );
}
mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
mHideAnimation.setDuration( duration );
mHideAnimation.setFillAfter( true );
view.startAnimation( mHideAnimation );
}
//漸漸顯示動畫
private void setShowAnimation( View view, int duration ){
if( null == view || duration < 0 ){
return;
}
if( null != mShowAnimation ){
mShowAnimation.cancel( );
}
mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
mShowAnimation.setDuration( duration );
mShowAnimation.setFillAfter( true );
view.startAnimation( mShowAnimation );
}