我有一个带有动画gif的WebView。我也有一个倒数计时器,当它启动时,它会加载一个不同的动画gif。
当计时器运行时,gif没有动画。解决这个问题的最佳方法是什么?
public class Practice extends SherlockActivity implements
AnimationLayout.Listener {
public final static String TAG = "Practice";
protected ListView mList;
protected AnimationLayout mLayout;
protected String[] mStrings = { "Lesson 1", "Lesson 2", "Lesson 3",
"Lesson 4", "Lesson 5", "Lesson 6", "Lesson 7", "Lesson 8",
"Lesson 9", "Lesson 10" };
String[] lesson1;
String[] lesson2;
String[] lesson3;
String[] lesson4;
String[] lesson5;
String[] lesson6;
String[] lesson7;
String[] lesson8;
String[] lesson9;
String[] lesson10;
Button buttonA;
Button buttonB;
Button buttonC;
Button buttonD;
String[] prev;
int numCorrect;
int numWrong;
int i;
String answer;
static Random rnd;
Random r;
final String alphabet = "ABCD";
final int N = 4;
char letter = 'A';
String rand = " ";
int index;
WebView sign;
long mMilliseconds = 10000;
TextView score;
CountDownTimer mCountDownTimer = new CountDownTimer(mMilliseconds, 10){
@Override
public void onFinish() {
score.setText("0");
}
public void onTick(long millisUntilFinished){
score.setText(String.valueOf(millisUntilFinished/100));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
prev = new String[100];
rnd = new Random();
r = new Random();
score = (TextView) findViewById(R.id.textView1);
lesson1 = getResources().getStringArray(R.array.lesson_1);
lesson2 = getResources().getStringArray(R.array.lesson_2);
lesson3 = getResources().getStringArray(R.array.lesson_3);
lesson4 = getResources().getStringArray(R.array.lesson_4);
lesson5 = getResources().getStringArray(R.array.lesson_5);
lesson6 = getResources().getStringArray(R.array.lesson_6);
lesson7 = getResources().getStringArray(R.array.lesson_7);
lesson8 = getResources().getStringArray(R.array.lesson_8);
lesson9 = getResources().getStringArray(R.array.lesson_9);
lesson10 = getResources().getStringArray(R.array.lesson_10);
rand = lesson1[rnd.nextInt(lesson1.length)];
mLayout = (AnimationLayout) findViewById(R.id.animation_layout);
mLayout.setListener(this);
mList = (ListView) findViewById(R.id.sidebar_list);
mList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mStrings));
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mList.setItemChecked(0, true);
sign = new WebView(getApplicationContext());
sign = (WebView) findViewById(R.id.webView1);
buttonA = (Button) findViewById(R.id.option1_button);
buttonB = (Button) findViewById(R.id.option2_button);
buttonC = (Button) findViewById(R.id.option3_button);
buttonD = (Button) findViewById(R.id.option4_button);
setSign();
}
public void onClickContentButton(View v) {
mLayout.toggleSidebar();
}
public void onClickA(View v) {
if (answer == buttonA.getText().toString()) {
numCorrect += 1;
} else {
numWrong += 1;
}
setSign();
}
public void onClickB(View v) {
if (answer == buttonB.getText().toString()) {
numCorrect += 1;
} else {
numWrong += 1;
}
setSign();
}
public void onClickC(View v) {
if (answer == buttonC.getText().toString()) {
numCorrect += 1;
} else {
numWrong += 1;
}
setSign();
}
public void onClickD(View v) {
if (answer == buttonD.getText().toString()) {
numCorrect += 1;
} else {
numWrong += 1;
}
setSign();
}
public void onClick(View view) {
}
@Override
public void onBackPressed() {
if (mLayout.isOpening()) {
mLayout.closeSidebar();
} else {
finish();
}
}
@Override
public void onSidebarOpened() {
Log.d(TAG, "opened");
}
@Override
public void onSidebarClosed() {
Log.d(TAG, "opened");
}
@Override
public boolean onContentTouchedWhenOpening() {
Log.d(TAG, "going to close sidebar");
mLayout.closeSidebar();
return true;
}
public void setSign() {
answer = pickRandomAnswer(prev[i]);
letter = alphabet.charAt(r.nextInt(N));
sign.loadUrl("file:///android_asset/" + answer.toLowerCase() + ".html");
prev[i] = answer;
buttonA.setText(pickRandomAnswer(answer));
buttonB.setText(pickRandomAnswer(buttonA.getText().toString()));
buttonC.setText(pickRandomAnswer(buttonB.getText().toString()));
buttonD.setText(pickRandomAnswer(buttonC.getText().toString()));
if (letter == 'A')
buttonA.setText(answer);
if (letter == 'B')
buttonB.setText(answer);
if (letter == 'C')
buttonC.setText(answer);
if (letter == 'D')
buttonD.setText(answer);
if(i>= 95){
i = 0;
} else {
i += 1;
}
mCountDownTimer.start();
}
public String pickRandomAnswer(String str) {
do{
index = rnd.nextInt(10+1);
if(index == 1){
if(mList.isItemChecked(0)){
rand = lesson1[rnd.nextInt(lesson1.length)];
}
}
if(index == 2){
if(mList.isItemChecked(1)){
rand = lesson2[rnd.nextInt(lesson2.length)];
}
}
if(index == 3){
if(mList.isItemChecked(2)){
rand = lesson3[rnd.nextInt(lesson3.length)];
}
}
if(index == 4){
if(mList.isItemChecked(3)){
rand = lesson4[rnd.nextInt(lesson4.length)];
}
}
if(index == 5){
if(mList.isItemChecked(4)){
rand = lesson5[rnd.nextInt(lesson5.length)];
}
}
if(index == 6){
if(mList.isItemChecked(5)){
rand = lesson6[rnd.nextInt(lesson6.length)];
}
}
if(index == 7){
if(mList.isItemChecked(6)){
rand = lesson7[rnd.nextInt(lesson7.length)];
}
}
if(index == 8){
if(mList.isItemChecked(7)){
rand = lesson8[rnd.nextInt(lesson8.length)];
}
}
if(index == 9){
if(mList.isItemChecked(8)){
rand = lesson9[rnd.nextInt(lesson9.length)];
}
}
if(index == 10){
if(mList.isItemChecked(9)){
rand = lesson10[rnd.nextInt(lesson10.length)];
}
}
} while ( rand.contains("Grammar")| rand == str);
return rand;
}
static void shuffleArray(String[] words) {
for (int i = words.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
String a = words[index];
words[index] = words[i];
words[i] = a;
}
}
}
答案 0 :(得分:0)
在查看您的代码之后,问题的一个可能原因是,在TextView
的{{1}}方法的每次调用中,您都在更新用户界面中的onTick()
。 CountDownTimer
的文档只提到它是“定期调用”所以我不确定它被调用的次数,但在我看来,你不应该做每次更新UI的事情。 CountDownTimer
调用,尤其是当你有onTick()
动画gif(资源密集型任务)时。我建议您使用WebView
方法执行此操作:
onTick()
这样,您每隔public void onTick(long millisUntilFinished){
if (mLastMillisUntilFinished == 0 ||
mLastMillisUntilFinished - millisUntilFinished > UI_UPDATE_INTERVAL) {
score.setText(String.valueOf(millisUntilFinished/UI_UPDATE_INTERVAL));
mLastMillisUntilFinished = millisUntilFinished;
}
}
毫秒才更新UI(只需将其定义到您想要的间隔,例如100,在每10秒更新一次)。这样可以防止您经常不必要地更新UI,并允许UI_UPDATE_INTERVAL
执行此操作。
作为最后的手段,有一个更复杂(没有太复杂)的解决方案,可以让你获得你想要的性能。在我的一个github repos中看到这个WebView
类:
我在谷歌代码上的一个废弃项目中找到了这个类,并在一个需要我将动画GIF绘制到Canvas的项目中使用它。它需要认真的工作,因为它根本不是线程安全的,它会占用内存,但它确实有效。
您可以非常轻松地使用GifView,它应该比使用WebView动画gif更快。您还需要GifDecoder课程。
干杯。
答案 1 :(得分:0)
作为最后的手段我会说你也可以将你的GIF分成帧,然后使用drawable在android中渲染它们。 Drawables reference