初始螢屏上需要一個視圖。

即加載并以影片的形式顯示從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?
uj5u.com熱心網友回復:
因為現在您只需要一個持續設定持續時間并在螢屏上更新 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()on來暫停/取消/結束影片valueAnimator。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/406221.html
標籤:
