效果图
二、布局main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:id="@+id/btn" android:text="按下显示动画"/> <ImageView android:layout_width="fill_parent" android:layout_height="200dp" android:layout_alignParentBottom="true" android:id="@+id/img" android:src="@drawable/bg"/> </RelativeLayout>
三、mainactivity
package com.ct.hide; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { /** Called when the activity is first created. */ private ImageView img; private Button btn; private boolean flag = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.img); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub flag = !flag; if(flag){ img.setAnimation(new MenuHideShow().hide()); img.setVisibility(View.GONE); }else { img.setAnimation(new MenuHideShow().show()); img.setVisibility(View.VISIBLE); } } }); } public class MenuHideShow{ Animation showMenu,hideMenu; public Animation show(){ showMenu = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0); showMenu.setDuration(1000); return showMenu; } public Animation hide(){ hideMenu = new ScaleAnimation(1, 0, 1, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1); hideMenu.setDuration(1000); Log.v("menuAnimation", "hide"); return hideMenu; } } }
(在F:\java\MyHideShow)