【问题标题】:Unable to achieve animation effect in ViewAnimator when performing view switchingViewAnimator在进行视图切换时无法实现动画效果
【发布时间】:2017-04-22 17:52:47
【问题描述】:

ViewAnimator中进行视图切换时,我首先想到我可以很容易地实现动画效果,通过使用

viewAnimator.setDisplayedChild(1);

viewAnimator.showNext();

视图切换成功。但是,没有显示动画。

我希望会有滑入/滑出效果,因为我在初始化期间设置了动画。

Animation slideInLeftFast = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_in_left_fast);
Animation slideOutRightSlow = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_out_right_slow);
this.viewAnimator.setInAnimation(slideInLeftFast);
this.viewAnimator.setOutAnimation(slideOutRightSlow);
slideInLeftFast.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationEnd(Animation arg0) {
        android.util.Log.i("CHEOK", "animation end");
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
        android.util.Log.i("CHEOK", "animation repeat");
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
        android.util.Log.i("CHEOK", "animation start");
    }
});

我的完整布局文件如下。 ViewAnimator中有2个不同的视图

trading_sign_in_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_animator"
    android:animateFirstView="false"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/sign_in_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            android:layout_above="@+id/sign_in_bottom_nav_bar">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/username_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:id="@+id/username_edit_text"

                    android:inputType="textVisiblePassword|textNoSuggestions"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                app:passwordToggleEnabled="true"
                android:id="@+id/password_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:id="@+id/password_edit_text"

                    android:inputType="textPassword"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:id="@+id/forgot_password_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:enabled="true"
            android:textAllCaps="false"
            android:text="Forgot password"
            android:textSize="16sp"
            android:layout_above="@+id/sign_in_bottom_nav_bar"
            android:layout_centerHorizontal="true"

            android:layout_marginBottom="8dp"
            android:paddingLeft="32dp"
            android:paddingRight="32dp" />

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/sign_in_bottom_nav_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:background="?attr/selectableItemBackground"

                android:id="@+id/sign_in_button"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="1.0"
                android:layout_height="48dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:enabled="true"
                android:textAllCaps="true"
                android:text="Log in" />
        </LinearLayout>
    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/forgot_password_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            android:layout_above="@+id/forgot_password_bottom_nav_bar">

            <TextView
                android:id="@+id/primary_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="16sp" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/forgot_password_username_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:id="@+id/forgot_password_username_edit_text"

                    android:inputType="textVisiblePassword|textNoSuggestions"
                    android:imeOptions="actionNext|flagNoExtractUi" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/forgot_password_bottom_nav_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:background="?attr/selectableItemBackground"

                android:id="@+id/forgot_password_forgot_password_button"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="1.0"
                android:layout_height="48dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:enabled="true"
                android:textAllCaps="true"
                android:text="Forgot password" />
        </LinearLayout>
    </RelativeLayout>

</ViewAnimator>

我可以知道我的代码有什么问题吗?我错过了什么吗?完整的最小代码可以在https://github.com/yccheok/MyProject找到。

【问题讨论】:

    标签: java android android-layout android-animation android-view


    【解决方案1】:

    如果您只是在viewAnimator 初始化后指定初始显示的子级,则动画将按预期启动。

    viewAnimator = (ViewAnimator) v.findViewById(R.id.view_animator);
    viewAnimator.setDisplayedChild(0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-21
      • 2012-02-29
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多