【问题标题】:how to set method and call it for many slideshow animation如何设置方法并为许多幻灯片动画调用它
【发布时间】:2013-08-18 10:22:39
【问题描述】:

我的应用程序活动之一是带有许多动画的幻灯片,取决于用户的选择,如果用户取消选中所有它将打开寻呼机活动,下面的代码可以完美运行,但我必须多次使用 (if and 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; 
}
   }

【问题讨论】:

    标签: android methods slideshow


    【解决方案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;
    }
    

    【讨论】:

    • imagepager 仅在取消选中所有复选框并启动新类显示 imagespager 但所有动画应用于幻灯片活动中的图像时运行,我想将所有动画和寻呼机类应用于一个类中的相同图像和一种方法,谢谢
    • 对不起,我不太明白你的问题。我的代码和你的一样,只是多余的。那是你的问题,不是吗?
    • 我试了一下,它使用图像寻呼机打开,然后当您检查任何动画并返回图像时,它不会应用动画并堆叠在一张图像上,我也无法滚动它
    • 抱歉,我刚刚在我的代码中看到了一些非常愚蠢的东西,请尝试使用编辑后的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多