我正在開發一個android遊戲應用程序。我對記分卡實施感到震驚。 我想在文本視圖中顯示分數,因爲數字會持續計算移動。 例如:假設用戶穿越一個障礙物,並且該障礙物的得分是200.那麼記分卡中的數字應該從0到200順利計數,然後停在200. 我在papiJump的記分卡中看到了這種類型的動畫。在textview中計數動畫的數字
請指導我。
我正在開發一個android遊戲應用程序。我對記分卡實施感到震驚。 我想在文本視圖中顯示分數,因爲數字會持續計算移動。 例如:假設用戶穿越一個障礙物,並且該障礙物的得分是200.那麼記分卡中的數字應該從0到200順利計數,然後停在200. 我在papiJump的記分卡中看到了這種類型的動畫。在textview中計數動畫的數字
請指導我。
public void animateTextView(int initialValue, int finalValue, final TextView textview)
{
DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(0.8f);
int start = Math.min(initialValue, finalValue);
int end = Math.max(initialValue, finalValue);
int difference = Math.abs(finalValue - initialValue);
Handler handler = new Handler();
for (int count = start; count <= end; count++) {
int time = Math.round(decelerateInterpolator.getInterpolation((((float) count)/difference)) * 100) * count;
final int finalCount = ((initialValue > finalValue) ? initialValue - count : count);
handler.postDelayed(new Runnable() {
@Override
public void run() {
textview.setText(finalCount + "");
}
}, time);
}
}
雖然這段代碼可以解決這個問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – jmattheis 2017-04-22 12:20:33
初始設置初始值= 0最終值取決於設置的初始值=上次賦值後的移動,並且可以根據移動位置更改最終值 – 2017-04-22 16:46:06
ValueAnimator animator = ValueAnimator.ofInt(int start, int end);
animator.setDuration(2000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
textView.setText(animation.getAnimatedValue().toString());
}
});
animator.start();
添加一些說明以及它將如何應用或如何應用於textview。 – androidnoobdev 2017-07-26 10:26:57
這個函數使用起來非常簡單,它會將textView上的整數值從0增加到動畫的某個值。 設置您的開始和結束範圍,例如0到300,並設置持續時間值,例如2000。因此,值增量過程將在2秒內生效。 瞭解更多信息https://developer.android.com/reference/android/animation/ValueAnimator.html – 2017-07-26 11:23:20
閱讀jmattheis關於答案的評論。 – androidnoobdev 2017-07-26 12:29:37
我想用Handler來從1數到200,並在TextView中顯示出來的。我的主意是否正確? – 2012-04-12 14:05:08
要小心一件事:如果你在UI線程上持續太久,你會被ANRed。所以一個AsyncTask可能更合適 – njzk2 2012-04-12 14:06:49