【问题标题】:Slideshow with multiple animation and Viewpager images带有多个动画和 Viewpager 图像的幻灯片
【发布时间】:2013-08-29 20:55:48
【问题描述】:

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

android:defaultValue="true" 用于第一个动画,即淡入动画。 但是:当您打开幻灯片活动时,它会以 imagepager 模式打开图像,并且 忽略 android:defaultValue="true" 用于淡入淡出复选框,

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

当我在图像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;
}
   } 

【问题讨论】:

  • 如果我理解正确,请告诉我,您的应用以ImagePager 开头,然后用户点击设置并将动画设置更改为true,您希望动画在按下时开始播放设置活动中的后退按钮(返回到ImagePager)?

标签: android android-viewpager slideshow


【解决方案1】:

请在尝试使用此项目时发布项目以制定适当的解决方案

首先,您已将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 代码

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

希望这可行

【讨论】:

  • 亲爱的,我添加了你的代码,但仍然存在问题,当点击设置菜单更改动画时,它会出现变化对话框,我发布了整个项目的链接,你可以下载它并检查它 。问题:1-应用程序必须以淡入淡出动画开始,因为它设置了 android:defaultValue="true" 但应用程序忽略它并以 viewpager 开始。
  • 2- 在从第一张图像开始的几张图像后重新启动寻呼模式中的滑动图像。 3-在进入偏好屏幕选择另一个动画然后返回幻灯片活动后,它不应用新动画,我必须多次按下返回按钮直到完成在寻呼机中滚动的所有图像然后它应用下一个动画,谢谢很多。
  • @androidqq6 : 好的,让我检查一下项目 & 在视图寻呼机中,仅在一定次数的滑动后,下一页就会膨胀并被破坏
  • 另外 2 个问题呢,亲爱的,有什么建议吗,谢谢
  • 第 1 和第 3 解决了对吗?在第 2 中你需要停止无限循环或什么?你能解释一下
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 2015-01-24
  • 2012-02-27
  • 1970-01-01
  • 2018-05-28
相关资源
最近更新 更多