Android實現動畫效果之淡入淡出
阿新 • • 發佈:2019-02-19
QQ群:372135639
View漸隱動畫效果
/**
* View漸隱動畫效果
*/
public 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);
}
View漸現動畫效果
/**
* View漸現動畫效果
*/
public 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);
}
最好在監聽動畫開始與結束, 對於showView, 是先呼叫showView.setVisibility(View.VISIBLE)設定為可見, 然後才呼叫showView.animate()
對於hideView, 是先呼叫 hideView.animate(),最後在onAnimationEnd事件中呼叫hideView.setVisibility(View.GONE);設定為不可見
監聽處理:View漸隱動畫效果
/**
* View漸隱動畫效果
*/
public void setHideAnimation( final 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);
mHideAnimation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
}
@Override
public void onAnimationRepeat(Animation arg0)
{
}
@Override
public void onAnimationEnd(Animation arg0)
{
view.setVisibility(View.GONE);
}
});
view.startAnimation(mHideAnimation);
}
監聽處理:View漸現動畫效果
/**
* View漸現動畫效果
*/
public void setShowAnimation( final 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);
mShowAnimation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
view.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation arg0)
{
}
@Override
public void onAnimationEnd(Animation arg0)
{
}
});
view.startAnimation(mShowAnimation);
}
需要注意: 我發現再設定View控制元件漸隱,且Gone(完全不可見),這兩個屬性後。 當點選控制元件位置的時候,依舊會出發該View的點選事件。 但是單獨設定View Gone的時候,點選View,就不會有反應。 這讓我很費解。 正好專案裡面,對於這個View的點選要進行處理, 所以我在給View設定 Gone的時候,會再次設定Clickable為false。