例如,如何在动画停止后显示一条随机线?目前我已经实现了这样的:
@Override
public void onAnimationEnd(Animation animation) {
//вывод текст здесь
String[] texts = {};
Set<Integer> generated = new HashSet<Integer>();
Random random = new Random ();
int pos = random.nextInt(texts.length);
((TextView) findViewById(R.id.myTextView)).setText(texts[pos]);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
但问题是,一来有String[] texts = {раз, два, ......, тысяча};
一千多个值,二来,我想把所有的东西都存储在字符串中,因为未来计划针对不同语言进行本地化。
@Override
public void onAnimationEnd(Animation animation) {
//вывод текст здесь
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Set<Integer> generated = new HashSet<Integer>();
Random random = new Random ();
int pos = random.nextInt(R.array.planets_array);
((TextView) findViewById(R.id.myTextView)).setText(planets[pos]);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
CatLog 输出以下内容:
-09 23:03:08.846 3579-3579/ru.infernal93.butilochka E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.infernal93.butilochka, PID: 3579
java.lang.ArrayIndexOutOfBoundsException: length=4; index=2122146214
at ru.infernal93.butilochka.ActivityGame$2.onAnimationEnd(ActivityGame.java:81)
at android.view.animation.Animation$3.run(Animation.java:376)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
10-09 23:03:08.848 3579-3579/ru.infernal93.butilochka E/MQSEventManagerDelegate: failed to get MQSService.
资源可以存储数组