如何更改计时器的周期

时间:2014-04-10 05:03:08

标签: android timer timertask

我想用不同时期的SD卡制作幻灯片图片。但我不能改变计时器的周期。 谢谢

private String SrcPathConfigfileImage = "/sdcard/data/pic.txt";
private String lineImage;    
private int imageIndex = 0;
private ArrayList<String> imageList;
private ImageView slidingImage;
private int period;
private Handler mHandler;
private Timer timer;
private TimerTask timerTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    slidingImage = (ImageView)findViewById(R.id.imageView1);

    imageList = new ArrayList<String>();
    mHandler = new Handler();
    timer = new Timer();

    try {
        File file = new File(SrcPathConfigfileImage);

        @SuppressWarnings("resource")
        BufferedReader bufferRead = new BufferedReader(new FileReader(file));

        while ((lineImage = bufferRead.readLine()) != null) {
            String SrcPathImage = "/sdcard/data/pic/"+lineImage;
            imageList.add(SrcPathImage);
        }   

        doTask(4000);

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(MainActivity.this, "Failed! Image = " + e.getMessage(),Toast.LENGTH_LONG).show();
    }

    Log.d("period Main is ", String.valueOf(period));

}

public void doTask(int nPeriod) {
    timerTask = new TimerTask() {

        @Override
        public void run() {

            mHandler.post(new Runnable() {

                @Override
                public void run() {

                    playImage();

                }
            }); 
        }
    };
    timer.schedule(timerTask, 0, nPeriod);  // I cannot change nPeriod

}


public void playImage() {
    String srcTimePeriod = imageList.get(imageIndex % imageList.size());
String[] splitTimePeriod = srcTimePeriod.split(",");
String srcPartPicture = splitTimePeriod[0];
period = Integer.parseInt(splitTimePeriod[1]);

Log.d("value period", String.valueOf(period));
Bitmap bm = BitmapFactory.decodeFile(srcPartPicture);
slidingImage.setImageBitmap(bm);
imageIndex++;

Animation rotateimage = AnimationUtils.loadAnimation(this,R.anim.custom_anim);      
slidingImage.startAnimation(rotateimage);       }

我想用不同时期的SD卡制作幻灯片图片。但我不能改变计时器的周期。 谢谢

0 个答案:

没有答案