Android - 从不可见到可见的动画

沙菲克

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

布局/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>

测试片段.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);
    }

}

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

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

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

埃利亚斯·法泽尔

在 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) {

    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章