Android - 从隐形到可见的动画

时间:2017-07-30 03:37:40

标签: android android-layout android-fragments animation

我试图使用以下代码为文本视图设置动画

布局/ fragment_testing.xml

<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"
    tools:context="com.onthego.OnTheGoFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Heading test"
        android:gravity="center"
        android:layout_marginTop="150dp"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="@dimen/heading_level_2"
        android:id="@+id/otg_main_title_tv"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/otg_main_menu_item_height"
        android:layout_marginTop="10dp"
        android:layout_marginStart="@dimen/otg_main_menu_item_magin_side"
        android:layout_marginEnd="@dimen/otg_main_menu_item_magin_side"
        android:id="@+id/otg_main_rapid_test_tv"
        android:text="Rapid Test"
        android:textSize="@dimen/text_size_1"
        android:textColor="@android:color/black"
        android:textAlignment="center"
        android:gravity="center"
        android:background="@drawable/otg_menu_item_bg"
        android:layout_below="@id/otg_main_title_tv"/>

    <ImageView
        android:layout_width="@dimen/otg_main_menu_item_height"
        android:layout_height="@dimen/otg_main_menu_item_height"
        android:layout_alignTop="@id/otg_main_rapid_test_tv"
        android:layout_alignStart="@id/otg_main_rapid_test_tv"
        android:background="@drawable/otg_menu_item_bg"
        android:padding="@dimen/otg_main_menu_item_icon_padding"
        android:src="@drawable/ic_chevron_right_black_48dp"/>

</RelativeLayout>

Testfragment.java

public class Testfragment extends Fragment {

    private TextView mTvOtgRapidTest;
    private Activity    mActivity;
    private Context     mContext;

    public Testfragment () {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(mContext == null) {
            mContext = context;
            mActivity = (MainActivity) getActivity();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_testing, container, false);
        mTvOtgRapidTest = (TextView) root.findViewById(R.id.otg_main_rapid_test_tv);

        new Handler().postDelayed(new Runnable() {  //To ensure if the view is visible on fragment attach
            @Override
            public void run() {
                setMenuAnimation();
            }
        }, 2000);

        return root;
    }


    private void setMenuAnimation() {
        Animation menuItemAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.menu_item_scale_x_up);
        mTvOtgRapidTest.startAnimation(menuItemAnimation);

        mTVOtgStamina.startAnimation(menuItemAnimation);
    }

}

这里我要做的是,视图在附加片段时应该是不可见的,在动画之后,它应该是可见的。

但是这里发生的事情是在附加片段时可以看到视图,并开始动画。

如何通过动画将文本视图从不可见状态变为可见?

2 个答案:

答案 0 :(得分:1)

xml中的

使其不可见

android:visibility="invisible"

每当你想要开始动画调用它并添加侦听器到动画时,在动画结束时它会使它可见

textView.startAnimation(anim);
anim..setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
           **textView.setVisibility(View.VISIBLE);**
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

答案 1 :(得分:1)

在元素的父活动中,您需要设置动画添加以下属性,动画将自动完成。

android:animatelayoutchanges="true"