Delay before slide show animation start

被刻印的时光 ゝ 提交于 2019-12-13 02:36:43

问题


My slide show Activity is one of my list view rows , when click the slideshow row it must open slide images with animation but there is a delay of around 25 second till first image appear , i try to change the time here:

int delay = 1000;

but that not fix it ,

any help will be appreciated , thanks

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; 
    }
}

回答1:


Replace this piece of code :

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);                  

}

Write instead this code :

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

This will lead to start image without delay ,

then you can control the appearance time and the time in between image as you want,

Hope help you .




回答2:


Try to use postdelay:

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

Handler mHandler = new Handler();

mHandler.postDelayed(mRunnable, mSampleDurationTime);

where mRunnable is your task:

private final Runnable mRunnable = new Runnable() {

 //...
public void run() {

   // do something here

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

 }
    ...
};


来源:https://stackoverflow.com/questions/18802641/delay-before-slide-show-animation-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!