简体   繁体   English

Android:加载片段时开始动画

[英]Android: Start animation when fragment is loaded

I've trying to implement custom fragment with loading animation. 我试图通过加载动画来实现自定义片段。 Same code works fine with activity, but not works with fragment. 相同的代码对活动有效,但对片段无效。

Here my fragment layout code: 这是我的片段布局代码:

<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>

My methods which I invokes in OnCreate method in fragment 我在片段中的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);
}

My rotate animation code: 我的旋转动画代码:

<?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>

Here I attach my animation fragment to activity in OnCreate method 在这里,我将动画片段附加到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()
}

Where I need to invoke my start animation method? 我需要在哪里调用我的开始动画方法?

Thanks. 谢谢。

From the documentation of onResume : onResume的文档中:

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. 这通常与包含Activity的生命周期的Activity.onResume绑定。

You want to run the animation when it can be visible in the first place, hence I suggest starting the animation in your fragment's onResume or override the onFragmentsResumed method in the activity that this fragment is attached to. 您首先希望在动画可见时运行动画,因此建议您在片段的onResume启动动画,或者在附加该片段的活动中覆盖onFragmentsResumed方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM