android菜单动画

来源:互联网 发布:澳门航空怎么样 知乎 编辑:程序博客网 时间:2024/06/04 18:03
今天学习了一个挺有意思的动画效果,这里的菜单并不是按机器上的 MENU出现在那种菜单,而是基于Android SDK 提供的android.view.animation.TranslateAnimation(extendsandroid.view.animation.Animation)类实例后附加到一个 Layout上使之产生的有动画出现和隐藏效果的菜单。
原理:Layout(菜单)从屏幕内(挨着屏幕边沿,其实并非一定,视需要的初态和末态而定)动态的移动到屏幕外(在外面可以挨着边沿,也可以离远点,这个无所谓了),这样就可以达到动态菜单的效果了。当 Layout(菜单)显示的时候,设置android:visibility="visible",当 Layout(菜单)隐藏的时 候,设置android:visibility="gone",这里 android:visibility 可以有 3 个值,"visible"为可见,"invisible"为不可见但占空间,"gone"为不可见且不占空间。
布局文件:
android菜单动画
代码文件:
public class MainActivity extends Activity {

private Animation showAciton, hideAction;
private LinearLayout menu;
private Button btn;
private boolean isMenuShowed;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

menu = (LinearLayout) findViewById(R.id.menu);
btn = (Button) findViewById(R.id.button);

// showAciton = newTranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
// Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF,
// -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

showAciton = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF,
0.0f);
hideAction = newTranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
hideAction.setDuration(2000);
showAciton.setDuration(2000);
isMenuShowed = false;
menu.setVisibility(View.GONE);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (isMenuShowed) {
isMenuShowed = false;
menu.startAnimation(hideAction);
menu.setVisibility(View.GONE);
} else {
isMenuShowed = true;
menu.startAnimation(showAciton);
menu.setVisibility(View.VISIBLE);
}

}
});
}
}
运行结果也就是模拟一个菜单缓缓展开,逐渐收上去的过程,感觉这效果不错啊,说不定哪天的项目就用到这效果了,由此来记录一下这简单的实现。
0 0