有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:加载片段时启动动画

我试图通过加载动画来实现自定义片段。同样的代码适用于活动,但不适用于片段

下面是我的片段布局代码:

<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="pw.osin.musicexpert.fragments.BusyFragment">

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_gravity="center"
    安卓:orientation="vertical">

    <ImageView
        安卓:id="@+id/fragment_animation_logo"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="center"
        安卓:layout_marginBottom="20dp"
        安卓:background="@drawable/vinyl" />

    <FrameLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="center"
        安卓:layout_margin="20dp"
        安卓:background="@color/colorPrimaryDark">

        <TextView
            安卓:id="@+id/fragment_animation_description"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_margin="16dp"
            安卓:gravity="center_horizontal"
            安卓:text="@string/message_data_is_loading"
            安卓:textColor="@color/colorPrimaryYellow"
            安卓:textSize="20dp"
            安卓:textStyle="bold" />
    </FrameLayout>

</LinearLayout>

我在片段中的OnCreate方法中调用的方法

private fun setAnimation() {
    Log.i(TAG, "Установка анимации")
    val drawable = AnimationUtils.loadAnimation(this.context, R.anim.rotate_anim)
    mAnimationDescription?.setText(R.string.message_data_is_loading);
    mAnimationImage?.startAnimation(drawable)
}

private fun initViewItems(view: View?) {
    Log.i(TAG, "Поиск элементов View")
    mAnimationDescription = view?.findView<TextView>(R.id.animation_description);
    mAnimationImage = view?.findView<ImageView>(R.id.animation_logo);
}

我的旋转动画代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
    <rotate
        安卓:duration="2000"
        安卓:fromDegrees="0"
        安卓:pivotX="50%"
        安卓:pivotY="50%"
        安卓:repeatCount="infinite"
        安卓:startOffset="0"
        安卓:toDegrees="360" />
</set>

在这里,我将动画片段附加到OnCreate方法中的活动

private var mTransaction: FragmentTransaction? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    attachFragment()
}

private fun attachFragment() {
    mTransaction = supportFragmentManager.beginTransaction()
    mTransaction?.add(R.id.main_activity_container, BusyFragment.newInstance("Получаем дату"))
    mTransaction?.commit()
}

我需要在哪里调用我的开始动画方法

谢谢


共 (1) 个答案

  1. # 1 楼答案

    ^{}的文档中:

    Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.

    您希望在动画一开始就可见时运行动画,因此我建议在片段的onResume中启动动画,或者在该片段所连接的活动中重写onFragmentsResumed方法