如何设置方法并为许多幻灯片动画调用它

时间:2013-08-18 10:22:40

标签: android methods slideshow

我的一个应用程序活动是幻灯片放映,许多动画取决于用户选择,如果用户取消选中它将打开寻呼机活动,下面的代码工作完美,但我必须用(if和if else)方法重复代码很多次对于每个冗余的动画,有没有办法设置一个方法并为每个动画调用它,如果未选中,则所有动画都将打开寻呼机活动,

任何帮助将不胜感激。

SlideShow.java

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Handler mHandler = new Handler();
 // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();              
        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 8000; // repeat every 4 sec.

    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 = getPrefs.getBoolean("animation", true);             
    boolean animation_one = getPrefs.getBoolean("animation_one", false);
    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 == 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);               
      slidingimage.startAnimation(rotateimage);   

}else 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.fade_in);        
      slidingimage.startAnimation(rotateimage);   

}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_out);       
      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.bounce);       
      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.fade_in_2);        
      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.flip);       
      slidingimage.startAnimation(rotateimage);  


}else if(animation == false && animation_one == false && animation_two == false){
    Intent intent = new Intent(SlideShow.this, ImagePager.class);                                     
    startActivity(intent);

}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

   @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {

    case R.id.action_settings:
        Intent p = new Intent("com.test.test.SETTING");
        startActivity(p);
    break;
    }

return false; 
}
   }

1 个答案:

答案 0 :(得分:-1)

我会尝试这样的事情......

{
    ...
    boolean noPager = true;
    noPager = doAnim(R.anim.custom_anim, animation, noPager);
    noPager = doAnim(R.anim.fade_in, animation_one, noPager);
    noPager = doAnim(R.anim.fade_out, animation_two, noPager);
    noPager = doAnim(R.anim.bounce, animation_three, noPager);
    noPager = doAnim(R.anim.fade_in_2, animation_four, noPager);
    noPager = doAnim(R.anim.flip, animation_five, noPager);
    if(!noPager){
        Intent intent = new Intent(SlideShow.this, ImagePager.class);                                     
        startActivity(intent);
    }
}

private boolean doAnim(int anim, boolean yes_no, boolean notDone){
    if(!yes_no || !notDone){
        return true;
    }
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, anim);       
    slidingimage.startAnimation(rotateimage);
    return false;
}