具有多个动画和Viewpager图像的幻灯片

时间:2013-08-18 14:52:02

标签: android android-viewpager slideshow

我有listactivity个app形成很多行,当你点击时,一行是幻灯片图片 显示ImageView的行打开活动,还有一个项目的选项菜单(幻灯片动画设置),当您单击它时,它会打开复选框首选项 动画屏幕有多个复选框,每个复选框都会对图像应用不同的动画 幻灯片放映,用户决定使用可用的许多动画来滑动图像 检查其复选框动画名称,或取消选中所有复选框以进行幻灯片放映活动 必须以viewpager模式显示图像。

android:defaultValue="true"用于fade_in动画的第一个动画。 但是:当您打开幻灯片活动时,它会以imagepager模式打开图像,忽略 android:defaultValue="true"以获取fade_in复选框,

然后转到偏好设置屏幕再选择另一个动画然后再回到幻灯片放映 活动,它不应用新动画,我必须多次按回按钮 直到完成所有在寻呼机中滚动的图像然后它应用下一个动画, 有时它会卡在图像寻呼机上并冻结,正常行为正在应用 下一个动画一旦按回按钮返回幻灯片。

当我处于图像viewpager模式并滚动它时,另一件事, 它滚动几个图像然后回到第一个图像然后我再次滚动图像 然后它突然回到第一张图像,依此类推。

整个项目可以从 here

下载

任何帮助将不胜感激。

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,
        R.drawable.day_one_10, R.drawable.day_one_11, R.drawable.day_one_12,
        R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
        R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18, 
        R.drawable.day_one_19,R.drawable.day_one_20  
        };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slide);
    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; 
}
   }

ImagePager.java

  public class ImagePager extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pager);

    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
    ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

     private int imageArra[] = { 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, R.drawable.day_one_11, R.drawable.day_one_12,
        R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
        R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18, 
        R.drawable.day_one_19,R.drawable.day_one_20  

    };

public class ImagePagerAdapter extends PagerAdapter {   
    Activity activity;
    int[] imageArray;

    public ImagePagerAdapter(Activity act, int[] imgArra) {
        imageArray = imgArra;
        activity = act;   
        }

    public int getCount() {
        return imageArray.length;   
        }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.custom_pager, null);   

        ImageView im=(ImageView) layout.findViewById(R.id.pager_imageView);

        im.setImageResource(imageArray[position]);

        ((ViewPager) collection).addView(layout, 0);
           return layout;   
           }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);   
        }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);   
        }

    @Override
    public Parcelable saveState() {
        return null; 
        }
    } 
@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 :(得分:0)

请尝试使用此

发布项目以找出合适的解决方案

首先,当您将ImageView slidingimage;声明为实例变量时,将Animation rotateimage声明为实例变量并将其用作

 rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);  

现在,一旦将动画添加到imageview,请调用if循环中的invalidate()方法

slidingimage.startAnimation(rotateimage)
slidingimage.invalidate();

在您的imagePager类中,将ViewPager myPager设为静态实例变量private static ViewPager myPager,并将此代码添加到类

public static void refreshPager(){
    if(myPager != null)
        myPager.invalidate();
}

settings课程中,在onBackPressed()事件

上调用此方法
@Override
public void onBackPressed() {
    ImagePager.refreshPager();
    super.onBackPressed();
}

修改

您只需要这样做,在Slide.java之后和setContentView(R.layout.main);

之前的final Handler mHandler = new Handler();文件中添加此代码
SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    if(getPrefs.getBoolean("Initialization", false) == false){
         SharedPreferences.Editor edit = getPrefs.edit();
         edit.putBoolean("animation_one", true);
         edit.putBoolean("Initialization", true);
         edit.commit();
    }

喜欢这个

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    if(getPrefs.getBoolean("Initialization", false) == false){
         SharedPreferences.Editor edit = getPrefs.edit();
         edit.putBoolean("animation_one", true);
         edit.putBoolean("Initialization", true);
         edit.commit();
    }
    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

    // Some more code............

仅创建偏好文件;)

编辑2 在你Slide.java文件中声明一个像这样的布尔值

public boolean loaded ;

并按照这样编码你的其他

else if(animation_one == false && animation_two == false && animation_three == false 
        && animation_four == false && animation_five == false){
    Intent intent = new Intent(Slide.this, ImagePager.class);  
    if(loaded)
    startActivity(intent);
    loaded = true;
}

Slide.java public static TimerTask任务中创建一个静态变量;并在ImagePager.java if(imageArra.length-1 == position)Slide.task.cancel();方法中添加此代码instantiateItem(),如下所示

    public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);   

    ImageView im=(ImageView) layout.findViewById(R.id.myimage);             
    im.setImageResource(imageArray[position]);

    ((ViewPager) collection).addView(layout, 0);
    if(imageArra.length-1 == position)
        Slide.task.cancel();
       return layout;   
       }

希望这有效