幻灯片放映动画开始前延迟

时间:2013-09-14 14:11:12

标签: android slideshow

我的幻灯片放映活动是我的列表视图行之一,当点击幻灯片显示行时,它必须打开带有动画的幻灯片图像,但是在第一个图像出现之前会有大约25秒的延迟, 我试着在这里改变时间:

int delay = 1000;

但是没有解决它,

任何帮助将不胜感激,谢谢

public class SlideShow extends Activity {

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

private int[] IMAGE_IDS = {
        R.drawable.day_one_1, R.drawable.day_one_2,R.drawable.day_one_3, 
        R.drawable.day_one_4, R.drawable.day_one_5,R.drawable.day_one_6,R.drawable.day_one_7,       
        R.drawable.day_one_8, R.drawable.day_one_9,R.drawable.day_one_10
        };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.slide); 
     if (customTitleSupported) { 
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
         TextView tv = (TextView) findViewById(R.id.title_tv1); 
         tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
         tv.setText("slide show");  
         }       
    final Handler mHandler = new Handler();
 // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();              
        }
    };

    int delay = 1000; 

    int period = 5000; 

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);
    }

    }, delay, period);                  
}        
private void AnimateandSlideShow() {

    SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    boolean animation_one = getPrefs.getBoolean("animation_one", true);             
    boolean animation_two = getPrefs.getBoolean("animation_two", false);
    boolean animation_three = getPrefs.getBoolean("animation_three", false);    
    boolean animation_four = getPrefs.getBoolean("animation_four", false);
    boolean animation_five = getPrefs.getBoolean("animation_five", false);                          

 if (animation_one == true) {   
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);  
    Animation rotateimage1 = AnimationUtils.loadAnimation(this, R.anim.bounce);            
      //
      AnimationSet s = new AnimationSet(false); 
      s.addAnimation(rotateimage);
      s.addAnimation(rotateimage1);
      slidingimage.startAnimation(s);

}else if(animation_two == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;        
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
      slidingimage.startAnimation(rotateimage);   

}else if(animation_three == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;        
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.move);        
      slidingimage.startAnimation(rotateimage);   

}else if(animation_four == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;        
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.sequential);        
      slidingimage.startAnimation(rotateimage);   

}else if (animation_five == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.slide_down);       
      slidingimage.startAnimation(rotateimage);  


}else if(animation_one == false && animation_two == false && animation_three == false
        && animation_four == false && animation_five == false){

    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);       
      slidingimage.startAnimation(rotateimage);       
    }
 }   
public boolean onCreateOptionsMenu(android.view.Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    getLayoutInflater().setFactory(new Factory() {
    public View onCreateView(String name, Context context,AttributeSet attrs) {

    if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
    try {
    LayoutInflater li = LayoutInflater.from(context);
    final View view = li.createView(name, null, attrs);

    new Handler().post(new Runnable() {
    public void run() {

    view .setBackgroundResource(R.drawable.border);

    ((TextView) view).setTextSize(30); 

    ((TextView) view).setTypeface(FontFactory.getBFantezy(getBaseContext()));

    ((TextView) view).setTextColor(Color.RED); 
    }
    });

    return view; 
    }
     catch (InflateException e) {}
      catch (ClassNotFoundException e) {}                                    
    }
    return null; 
    }
    });          
    return super.onCreateOptionsMenu(menu);
                                       }    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.slide_pref:
              Intent i = new Intent("com.test.demo.SETTING");
              startActivity(i);
        break;
            }
    return false; 
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用postdelay

private int mSampleDurationTime = 5000; // 1 sec
private boolean continueToNext = true;

Handler mHandler = new Handler();

mHandler.postDelayed(mRunnable, mSampleDurationTime);

mRunnable是你的任务:

private final Runnable mRunnable = new Runnable() {

 //...
public void run() {

   // do something here

   if(continueToNext == true){
    mHandler.postDelayed(mRunnable, mSampleDurationTime);
   }

 }
    ...
};

答案 1 :(得分:0)

替换这段代码:

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

        AnimateandSlideShow();              
    }
};

int delay = 1000; 

int period = 5000; 

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {

     mHandler.post(mUpdateResults);
}

}, delay, period);                  

}

改为编写此代码:

final Handler handler = new Handler();   
 runnable = new Runnable() {
     @Override
    public void run() {
        handler.postDelayed(runnable, 5000);
        AnimateandSlideShow();    
    }
};
handler.postDelayed(runnable, 500);
}

这将导致无延迟地开始图像,

然后您可以根据需要控制图像之间的出现时间和时间,

希望能帮助你。