动画仅在我触摸屏幕后才开始

时间:2013-04-21 16:46:23

标签: android animation fragment

我有一个顶部栏容器和内容容器的布局。单击顶部栏中的按钮时,将使用动画显示垂直菜单。我的minSdkVersion是9。

当我启动应用程序并且仍然没有单击菜单按钮(即内容片段没有更改)时这很有效,但是只要我单击一个选项(然后替换content_container中的片段) ,垂直菜单行为不规律。菜单btn的点击事件被正确触发,但并不总是显示垂直菜单(但有时它是......)。但是,当我单击按钮然后触摸屏幕时,动画(显示或隐藏菜单)开始。

我想它与重叠内容片段的垂直菜单有关,然后替换内容片段以某种方式修改它,但我找不到任何解决方案。

有人可以帮忙吗?

顶栏片段

    @Override
    public void onActivityCreated (Bundle savedInstanceState){

        super.onActivityCreated(savedInstanceState);

        toggleMenu(0);      

        Button btn_menu = (Button) getView().findViewById(R.id.btn_menu);
        btn_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVerticalMenu.setVisibility(View.VISIBLE);
                toggleMenu(1000);
            }
        });
    }

    private void toggleMenu(int duration){
        if(mMenuIsOpen){

            TranslateAnimation anim1 = new TranslateAnimation(0,0,0,-(mHeight-mMenuVerticalOffset));
            anim1.setFillAfter(true);
            anim1.setDuration(duration);
            mVerticalMenu.setAnimation(anim1);

            AlphaAnimation anim2 = new AlphaAnimation(0.7f, 0.0f);
            anim2.setFillAfter(true);
            anim2.setDuration(duration);            

            menu_option_01.setOnClickListener(null);
            menu_option_02.setOnClickListener(null);
            menu_option_03.setOnClickListener(null);

            mMenuIsOpen = false;
        }
        else{

            TranslateAnimation anim1 = new TranslateAnimation(0,0,-(mHeight-mMenuVerticalOffset),0);
            anim1.setFillAfter(true);
            anim1.setDuration(duration);
            mVerticalMenu.setAnimation(anim1);

            AlphaAnimation anim2 = new AlphaAnimation(0.0f, 0.7f);
            anim2.setFillAfter(true);
            anim2.setDuration(duration);

            menu_option_01.setOnClickListener(mButtonClickListener);
            menu_option_02.setOnClickListener(mButtonClickListener);
            menu_option_03.setOnClickListener(mButtonClickListener);

            mMenuIsOpen = true;
        }

    }



    private OnClickListener mButtonClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            toggleMenu(1000);

            if(!v.isSelected()){        


                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();

                switch(v.getId()){

                case R.id.menu_option_01:

                    // replace content_container by fragment 1

                    break;

                case R.id.btn_02:

                    // replace content_container by fragment 2

                    break;      

                case R.id.btn_03:

                    // replace content_container by fragment 3

                    break;      

                }
            }
        }

    };

    private OnClickListener mBgClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            toggleMenu(1000);           
        }
    };

主要布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="44dp" />

    <FrameLayout
        android:id="@+id/top_bar_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false" />

</RelativeLayout>

顶栏布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000" >

    <LinearLayout
        android:id="@+id/vertical_menu"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_marginTop="44dp"
        android:background="#ffffff"
        android:orientation="vertical"
        android:visibility="gone" >

      <!-- menu layout -->

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="#ffffff" >

        <Button
            android:id="@+id/btn_menu"
            android:layout_width="50dp"
            android:layout_height="44dp"
            android:background="@drawable/menubtn" />   

        <ImageView
            android:layout_width="130dp"
            android:layout_height="44dp"
            android:src="@drawable/logo"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:1)

在我的Toggle方法结束时,我使根视图无效:

rootView.invalidate();

现在它有效。不太清楚为什么我必须这样做......

答案 1 :(得分:1)

我知道已有一个已接受的答案,但我有类似的问题,答案没有帮助。

我有一个视图,我在布局结束时声明要保持其Z索引高于其兄弟姐妹。我不得不触摸页面以使动画有效。

所以我通过Java再次设置了Z索引并且工作正常。

view.bringToFront();
相关问题