如何将加载过程从 0 动画到 100?

问题描述 投票:0回答:2

启动屏幕上需要有一个视图。

即加载,并以动画的形式显示加载从0%到100%。

我正在尝试按照以下方式进行操作,但是这种方法不太好,我该如何用小动画来做到这一点?

这是我现在的代码片段:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    for (i in 0..100) {
        binding.loadingView.setText("Loading $i %")
    }
}

还有我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data class="SplashScreenBinding">

        <import type="android.view.View" />

        <variable
            name="viewModel"
            type="com.mandarine.aco.splash.SplashViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main_background">

        <ImageView
            android:id="@+id/appImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:scaleType="center"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/loadingView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="66dp"
            android:gravity="center_horizontal"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Loading"
            android:textColor="@android:color/white"
            android:textSize="42px"
            tools:text="Loading 43 %" />
    </RelativeLayout>
</layout>

问:如何将加载过程从 0 动画化到 100?

android xml kotlin data-binding android-animation
2个回答
1
投票

因为现在您只需要一个持续设定持续时间并更新屏幕上 0 - 100 值的加载动画,您可以使用

ValueAnimator.ofInt

private var valueAnimator: ValueAnimator? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val targetTextView = // ...
    
    valueAnimator = ValueAnimator.ofInt(0, 100).apply {
        addUpdateListener {
            targetTextView.text = "Loading ${it.animatedValue}%"
        }
        interpolator = LinearInterpolator()
        duration = 2000
        start()
    }
}

// You need to end the animation in case the view is being
// destroyed before the animation has completed
override fun onDestroyView() {
    super.onDestroyView()
    valueAnimator?.end()
    valueAnimator = null
}

您还可以根据需要调用

pause()
上的
cancel()
end()
valueAnimator
来暂停/取消/结束动画。


0
投票
        lifecycleScope.launch(Dispatchers.IO) {
            while (currentTime < totalTime) {
                delay(abc)
                runOnUiThread {
                    binding.alphaBtn?.text =
                        "${((currentTime.toFloat() / totalTime) * 100).roundToInt()} % "
                //update your animation here
              }  
                currentTime += 1
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.