我有一个充当菜单的图像视图。当用户单击它时,一个视图组(包括 5 个其他图像视图)将从左向右滑动。当用户再次单击菜单时,从右到左查看组幻灯片。
我可以模拟这种行为,但是在从右向左滑动后,我希望看不到视图组,但是视图组会放在它的位置上。我尝试使用LinearLayout.setVisibiliy(View.Invisible)
,但在那一刻我看不到从右向左滑动的动画。
这是我的代码,任何建议将不胜感激。
menu_open.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="10%"
android:animation="@anim/slide_out" />
幻灯片输出.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
menu_close.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="10%"
android:animation="@anim/slide_in" />
幻灯片输入.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ivMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_01" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/ivMenu" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_03" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_04" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_05" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_06" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/CD"
android:src="@drawable/ic_menu_07" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
private ImageView ivMenu;
private Animation animate;
private LinearLayout groupLayout;
private boolean menuState = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
groupLayout = (LinearLayout) findViewById(R.id.linearLayout);
groupLayout.setVisibility(View.INVISIBLE);
ivMenu = (ImageView) findViewById(R.id.ivMenu);
animate = AnimationUtils.loadAnimation(this, R.anim.animate);
ivMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ivMenu.startAnimation(animate);
runFadeOutAnimationOn(MainActivity.this, ivMenu);
if(!menuState) {
groupLayout.setVisibility(View.VISIBLE);
runExpandMenuAnimation(groupLayout, MainActivity.this);
} else {
runCollapseMenuAnimation(groupLayout, MainActivity.this);
// groupLayout.setVisibility(View.INVISIBLE);
}
menuState = !menuState;
Log.i("Menu state", "" + menuState);
}
});
}
public static void runExpandMenuAnimation(ViewGroup panel, Context ctx) {
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(ctx, R.anim.menu_open);
panel.setLayoutAnimation(controller);
}
public static void runCollapseMenuAnimation(ViewGroup panel, Context ctx) {
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(ctx, R.anim.menu_close);
panel.setLayoutAnimation(controller);
}
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx, android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
}