Android动画隐藏/可见

时间:2016-05-11 13:56:52

标签: android animation layout

我想在<LinearLayout>变为可见或消失时(GONE)为VISIBLE设置动画。当我点击{{1}时,LinearLayout会变为GONE / FloatingActionButton }}。这是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coma"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        android:visibility="gone"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar2"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"/>

            <!--This is the LinearLayout that I want to show
              Even tho is empty, the content is not needed.-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:paddingTop="48dp"
                android:visibility="gone"
                android:id="@+id/ll"
                android:background="@color/colorPrimary"
                android:orientation="horizontal">
            </LinearLayout>

        </android.support.design.widget.AppBarLayout>

        <!--For now on I have more code, but it's useless for the answer.-->
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

在这里你有打印,所以你可以看到我想要的动画类型,但基本上我想要显示/隐藏时向上/向下拖动动画:GoneVisible

我已经尝试过了:

ll.animate()
                            .translationY(0)
                            .alpha(0.0f)
                            .setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    super.onAnimationEnd(animation);
                                    ll.setVisibility(View.GONE);
                                }
                            });
  

修改

此logcat适用于MalteseFalcon的回答:

05-11 14:21:05.879 2591-2591/com.example.sdilab.pap E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.NullPointerException
                                                                          at com.example.sdilab.pap.Activitys.AgendWorkoutActivity$1$override.onClick(AgendWorkoutActivity.java:83)
                                                                          at com.example.sdilab.pap.Activitys.AgendWorkoutActivity$1$override.access$dispatch(AgendWorkoutActivity.java)
                                                                          at com.example.sdilab.pap.Activitys.AgendWorkoutActivity$1.onClick(AgendWorkoutActivity.java:0)
                                                                          at android.view.View.performClick(View.java:4204)
                                                                          at android.view.View$PerformClick.run(View.java:17355)
                                                                          at android.os.Handler.handleCallback(Handler.java:725)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                          at dalvik.system.NativeStart.main(Native Method)
  

EDIT2   MalteseFalcon的代码由我实现:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_workout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);

        TabLayout tabs = (TabLayout) findViewById(R.id.tabs2);
        tabs.setVisibility(View.GONE);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.coma);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fb);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ll.getVisibility() == View.GONE){
                ll.setVisibility(View.VISIBLE);}
                else {
                ll.setVisibility(View.GONE);
                LayoutTransition transition;
                transition = ll.getLayoutTransition();
                transition.enableTransitionType(LayoutTransition.CHANGING);
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

android:animateLayoutChanges="true"添加到布局xml。

你也应该尝试:

LayoutTransition transition;
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);

transition = layout.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);