TextView 顯示類似進度條的動態文字效果(如下載進度、倒計時)。
| 方案 | 優點 | 缺點 |
|---|---|---|
| SeekBar/ProgressBar + TextView 疊加 | 原生組件、穩定、無需自定義繪製 | 佈局稍複雜 |
| 自定義 View 繪製文字進度 | 高度定制化、單一組件 | 需處理文字測量、動畫、狀態保存 |
| ObjectAnimator 動畫 TextView 文本 | 代碼簡潔、Material 風格 | 僅適用數字變化 |
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/progress_bg"
android:thumb="@null"/>
<TextView
android:id="@+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0%"
android:textColor="#FFF"
android:textSize="14sp"/>
</FrameLayout>
SeekBar seekBar = findViewById(R.id.seekBar);
TextView tv = findViewById(R.id.tvProgress);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
tv.setText(progress + "%");
}
@Override public void onStartTrackingTouch(SeekBar sb) {}
@Override public void onStopTrackingTouch(SeekBar sb) {}
});
ObjectAnimator.ofInt(0, 100).setDuration(1000).addUpdateListener(anim -> {
tv.setText(anim.getAnimatedValue() + "%");
}).start();