Android编程重复运行图像

时间:2014-08-11 04:45:48

标签: java android

嗨我有这些代码和我想要的是,当过程完成后,它将在第一张图像上再次重复。我怎么能这样做?

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fingerspellingout);
    gotWord = getIntent().getExtras();
    get = gotWord.getString("key");
    img = (ImageView)findViewById(R.id.image_alpha);
    //ttsbtn = (Button)findViewById(R.id.btn_speak);

    getArray= get.toCharArray();

    charIndex = 0;
    Timer timer = new Timer();
    TimerTask timerTask = new ImageTimerTask();
    timer.schedule(timerTask, 0, 2000);

}
public class ImageTimerTask extends TimerTask{
    ImageTimerTask imageTimerTask = this;

    @Override
    public void run() {
            System.out.println("charIndex: " + charIndex);
            System.out.println("this: " + getArray[charIndex]);
            System.out.println("get length: " + get.length());


            runOnUiThread(new Runnable(){
                @Override
                public void run() {

                    System.out.println("run UI thread!");
                    InputStream is;
                    try {

                        if(getArray[charIndex]==' ')
                        {
                            charIndex++;
                        }

                        is = getResources().getAssets().open(getArray[charIndex] + ".png");
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        img.setImageBitmap(bitmap);

                        charIndex++;

                        if(charIndex == get.length()){
                            System.out.println("cancel!");
                            imageTimerTask.cancel();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });
    }
}

这是我的代码......

1 个答案:

答案 0 :(得分:0)

尝试使用scheduleAtFixedRate(TimerTask task, long delay, long period)代替schedule

并且charIndex应该重置为0

if(charIndex == get.length()){
   charIndex=0;
}

或者您可以使用AnimationDrawable重复显示动画