Ric*_*ton 17 java android android-layout
我正在开发一个应用程序,用于计算几段文本中的问号数量.
扫描完成后(根本没有时间)我希望在数字从0到TOTAL之后显示总数.所以,对于10:0,1,2,3,4,5,6,7,8,9 10然后停止.
我尝试了几种不同的技术:
TextView sentScore = (TextView) findViewById(R.id.sentScore);
long freezeTime = SystemClock.uptimeMillis();
for (int i = 0; i < sent; i++) {
if ((SystemClock.uptimeMillis() - freezeTime) > 500) {
sentScore.setText(sent.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
for (int i = 0; i < sent; i++) {
// try {
Thread.sleep(500);
} catch (InterruptedException ie) {
sentScore.setText(i.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我相信这些都是非常业余的尝试.任何帮助将非常感激.
谢谢,
理查德
mar*_*mor 57
我使用了更传统的Android风格动画:
ValueAnimator animator = new ValueAnimator();
animator.setObjectValues(0, count);
animator.addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
view.setText(String.valueOf(animation.getAnimatedValue()));
}
});
animator.setEvaluator(new TypeEvaluator<Integer>() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round(startValue + (endValue - startValue) * fraction);
}
});
animator.setDuration(1000);
animator.start();
Run Code Online (Sandbox Code Playgroud)
您可以使用0和count值来使计数器从任意数字转到任何数字,并使用它1000来设置整个动画的持续时间.
请注意,这支持Android API等级11及更高版本,但您可以使用超棒的nineoldandroids项目使其易于向后兼容.
Luk*_*rog 10
试试这个:
private int counter = 0;
private int total = 30; // the total number
//...
//when you want to start the counting start the thread bellow.
new Thread(new Runnable() {
public void run() {
while (counter < total) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.post(new Runnable() {
public void run() {
t.setText("" + counter);
}
});
counter++;
}
}
}).start();
Run Code Online (Sandbox Code Playgroud)