I have an image view which acts as menu. When user click it a view group (including 5 other image view) will slide from left to right. When user click on menu again view group slides from right to left.
I can simulate this behavior but after sliding right to left I expect to not see view group however view group will put on its place. I tried to use LinearLayout.setVisibiliy(View.Invisible)
but at that moment i couldn't see sliding right to left animation.
This is my code, any suggestion would be appreciated.
menu_open.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="10%"
android:animation="@anim/slide_out" />
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" />
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;
}
}
Finally I found the solution. In this case we should use aViewGroup.setLayoutAnimationListener(new AnimationListener()...)
and override its methods.
Therefore I added:
groupLayout.setLayoutAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
if(menuState)
groupLayout.setVisibility(View.INVISIBLE);
menuState = !menuState;
Log.i("Menu state", "" + menuState);
}
});
来源:https://stackoverflow.com/questions/12328779/android-how-to-set-animation-listener-for-view-group