|
如图第一幅图,点击按钮,,,显示第二幅画面,在点击的话,是菜单隐藏起来.
1.ActivityMain.java文件
TranslateAnimation showAction,hideAction;//
LinearLayout menu;
Button button;
boolean menuShowed;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得布局管理器的引用
menu = (LinearLayout)findViewById(R.id.menu);
button = (Button)findViewById(R.id.button);
//缩放动画
showAction = new ScaleAnimation(
1.0f, 1.0f, 0.0f,1.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f);
hideAction = new ScaleAnimation(
1.0f, 1.0f, 0.0f,1.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f);
//一开始菜单是隐藏的
hideAction.setDuration(500);//设置隐藏持续时间0.5秒
menuShowed = false;//隐藏
menu.setVisibility(View.GONE);
//点击按钮实现功能
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(menuShowed){//如果已经显示了,就隐藏。
menuShowed = false;
menu.setVisibility(View.GONE);
menu.startAnimation(hideAction);
}else{//隐藏时,实现显示。
menuShowed = true;
menu.setVisibility(View.VISIBLE);
menu.startAnimation(showAction);
}
}
});
2.main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="100sp"
android:background="#ffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am a menu !"
android:textSize="24sp"
android:gravity="center"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to show/hide menu"
android:textSize="24sp"
/>
</LinearLayout>
源码下载: TranslateAnimation_Menu.rar (41.42 KB, 下载次数: 3) |
|