1

我的listactivity应用程序形成了很多行,一行是图像幻灯片,当您单击该行打开活动显示ImageView时,还有一个项目的选项菜单(幻灯片动画设置),当您单击它时,它会打开复选框首选项动画屏幕,其中包含多个复选框每个都将不同的动画应用于图像幻灯片,用户可以通过选中其复选框动画名称或取消选中所有复选框来确定幻灯片具有许多可用动画的图像,因此幻灯片活动必须以 viewpager 模式显示图像。

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

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

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

整个项目可以从 这里下载

任何帮助将不胜感激。

幻灯片放映.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;
}
   } 
4

1 回答 1

0

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

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

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

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

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

希望这有效

于 2013-08-28T06:16:29.043 回答