Android,如何为视图组设置动画侦听器?

时间:2012-09-08 07:06:20

标签: android animation listener viewgroup

我有一个用作菜单的图像视图。当用户单击它时,视图组(包括其他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" />

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;
    }
}

enter image description here

1 个答案:

答案 0 :(得分:5)

最后我找到了解决方案。在这种情况下,我们应该使用aViewGroup.setLayoutAnimationListener(new AnimationListener()...)并覆盖其方法。

因此我补充说:

 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);
            }
        });