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

Java Android:加载片段时启动动画,java,android,animation,kotlin,Java,Android,Animation,Kotlin,我试图通过加载动画来实现自定义片段。同样的代码可以很好地处理活动,但不能处理片段 下面是我的片段布局代码: <FrameLayout 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" t

我试图通过加载动画来实现自定义片段。同样的代码可以很好地处理活动,但不能处理片段

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

<FrameLayout 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="pw.osin.musicexpert.fragments.BusyFragment">

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

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

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

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

</LinearLayout>
我需要在哪里调用我的开始动画方法

谢谢。

来自以下文件:

当片段对用户可见并正在运行时调用。这通常和包含活动生命周期的Activity.onResume相关联

您希望在动画首先可见时运行动画,因此我建议在片段的
onResume
中启动动画,或者在该片段所附加的活动中重写
onFragmentsResumed
方法。

来自以下文档:

当片段对用户可见并正在运行时调用。这通常和包含活动生命周期的Activity.onResume相关联


您希望在动画首先可见时运行动画,因此我建议在片段的
onResume
中启动动画,或者在该片段所附加的活动中重写
onfragmentsremed
方法。

谢谢您的回复。不,它不适合我。你能看一下我的代码吗?可能有什么问题吗?例如,片段的setAnimation需要是不同的,类似的东西。我在Kotlin上的代码,但它与Java没有太大区别。OnResume是一个完全可行的开始动画的地方。我认为您应该检查startAnimation方法是否真的像您所想的那样(android中有几种执行动画的方法,有时可能会很棘手)在片段中运行。onResume与活动的生命周期有关。因此,如果活动的onResume在附加片段时已被调用,则不会调用它。Thx for reply,在这种情况下我需要做什么?谢谢回复。不,它不适合我。你能看一下我的代码吗?可能有什么问题吗?例如,片段的setAnimation需要是不同的,类似的东西。我在Kotlin上的代码,但它与Java没有太大区别。OnResume是一个完全可行的开始动画的地方。我认为您应该检查startAnimation方法是否真的像您所想的那样(android中有几种执行动画的方法,有时可能会很棘手)在片段中运行。onResume与活动的生命周期有关。因此,如果活动的onResume在附加片段时已被调用,则不会调用它。Thx for reply,在这种情况下我需要做什么?您看到了什么?你的片段显示但没有动画?您的文本视图获得正确的文本吗?您好,是的。我的片段是显示但没有动画。你看到了什么?你的片段显示但没有动画?您的文本视图获得正确的文本吗?您好,是的。我的片段正在显示,但没有动画。
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:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="0"
        android:toDegrees="360" />
</set>
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()
}