Android的,如何设置动画侦听器视图组? [英] Android, How to set animation listener for view group?

查看:148
本文介绍了Android的,如何设置动画侦听器视图组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充当菜单的图像视图。当用户点击视图组(含5其他图像显示)将滑动由左到右。当在菜单中用户再次点击查看组幻灯片从右到左。

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.

我可以模拟这种行为,但滑动从右到左我希望看不到视图组但鉴于集团将放在它的位置后。我试图用 LinearLayout.setVisibiliy(View.Invisible),但在那一刻,我看不出滑动从右到左的动画。

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.

这是我的code,任何的建议是AP preciated。

This is my code, any suggestion would be appreciated.

menu_open.xml

menu_open.xml

<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="10%"
    android:animation="@anim/slide_out" />

slide_out.xml

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

menu_close.xml

<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="10%"
    android:animation="@anim/slide_in" />

slide_in.xml

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

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

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

推荐答案

最后,我找到了解决办法。在这种情况下,我们应该用 aViewGroup.setLayoutAnimationListener(新AnimationListener()...)并覆盖它的方法。

Finally I found the solution. In this case we should use aViewGroup.setLayoutAnimationListener(new AnimationListener()...) and override its methods.

所以我说:

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

这篇关于Android的,如何设置动画侦听器视图组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆