2012-02-29 21 views
0

我正在做一个应用程序的Android应该显示图片(我已经获得),只有现在的应用程序啊改变了过程,图像应显示在不同的时间间隔,例如,第一个图像显示在开始活动时, 4秒后的第二秒,15秒后的第三秒等等......这是我的活动,它显示固定间隔内的图像;Android:如何在不同的时间间隔更改图像视图中的图像?

public class WCourse extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ 

//player 
private ImageButton buttonPlayPause; 
private SeekBar seekBarProgress; 
public EditText editTextSongURL; 


public static TextView TextViewTopTitle; 

//Slider 
public int currentimageindex=0; 
Timer timer; 
TimerTask task; 
ImageView slidingimage; 

//images ids 
private int[] IMAGE_IDS = { 
     R.drawable.c5_d1, R.drawable.c5_d2, R.drawable.c5_d3, R.drawable.c5_d4, R.drawable.c5_d5, 
     R.drawable.c5_d6, R.drawable.c5_d7, R.drawable.c5_d8, R.drawable.c5_d9, R.drawable.c5_d10, 
     R.drawable.c5_d11, R.drawable.c5_d12, R.drawable.c5_d13, R.drawable.c5_d14, R.drawable.c5_d15, 
     R.drawable.c5_d16, R.drawable.c5_d17, R.drawable.c5_d18, R.drawable.c5_d19, R.drawable.c5_d20, 
     R.drawable.c5_d21, R.drawable.c5_d22, R.drawable.c5_d23, R.drawable.c5_d24, R.drawable.c5_d25, 
     R.drawable.c5_d26, R.drawable.c5_d27, R.drawable.c5_d28, R.drawable.c5_d29, R.drawable.c5_d30, 
     R.drawable.c5_d31, R.drawable.c5_d32, R.drawable.c5_d33, R.drawable.c5_d34, R.drawable.c5_d35, 
     R.drawable.c5_d36, R.drawable.c5_d37, R.drawable.c5_d38, R.drawable.c5_d39, R.drawable.c5_d40, 
     R.drawable.c5_d41, R.drawable.c5_d42, R.drawable.c5_d43, R.drawable.c5_d44, R.drawable.c5_d45, 
     R.drawable.c5_d46 
    }; 

//player  
private MediaPlayer mediaPlayer; 
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class 

//slider handler 
private final Handler handler = new Handler(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.watch_course); 

    TextViewTopTitle = (TextView) findViewById(R.id.TextViewTopTitle); 
    Bundle bundle = getIntent().getExtras(); 
    TextViewTopTitle.setText(bundle.getString("RESULT2")+" "); 
    getText(bundle.getString("RESULT2")); 

    final Handler mHandler = new Handler();  
    initView(); 

// Create runnable for posting 
    final Runnable mUpdateResults = new Runnable() { 
     public void run() { 

      AnimateandSlideShow(); 

     } 
    }; 

    final int delay = 500; // delay for .5 sec.  

    final long period = 1000;   
    Timer timer = new Timer(); 

    timer.scheduleAtFixedRate(new TimerTask() { 

    public void run() {   
     mHandler.post(mUpdateResults); 
    } 

}, delay, period);} 

private void AnimateandSlideShow() { 
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left); 
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); 
currentimageindex++; 
    slidingimage.getAnimation();} 

如何以不同的时间间隔显示这些图像的任何建议或示例?真的很感激你的帮助。

+0

你考虑在每次运行时停止该定时器并重新设置时间间隔,然后开始新的间隔定时器? – 2012-02-29 07:01:52

+1

使用它来获得一个随机时间public static int randomNumber(int min,int max){ return min +(new Random())。nextInt(max - min); } – zeitue 2012-02-29 07:33:56

回答

1

使用下面的代码希望这将有助于

MyCount count=new MyCount(IMAGE_IDS.length * 1000,1000); 
public class MyCount extends CountDownTimer{ 

      public MyCount(long millisInFuture, long countDownInterval) { 
       super(millisInFuture, countDownInterval); 
       } 

       @Override 
       public void onFinish() { 
         start=0; 
        count.start(); 
        } 

       @Override 
       public void onTick(long millisUntilFinished) { 
         if(start<=IMAGE_IDS.length) 
{ 
        img.setImageResource(IMAGE_IDS[start]) 
        start+=1; 
} 

       } 
     } 
int start=0; 
count.start(); 
+0

倒计时器使用会很好,如果我打算适应seekbar所需的时间? – JLouis 2012-02-29 07:18:57

+0

没有得到你的问题,请解释一点 – Maneesh 2012-02-29 07:22:31

+0

当然,这个活动做三件事,首先使用mp3 seekbar,其次,样本图像必须与音频同步,然后我会做一个textviews(如一个字幕),也必须像图像与搜索栏一样同步,问题是是否使用countDownTimer,那么以后我会遇到seekbar imageview时间间隔的问题吗? – JLouis 2012-02-29 07:30:10

相关问题