4

I have a ImageSwitcher and 2 buttons..."Next" and "Previous" to slide images...But its animate my sliding only in one side from left to the right...How to fix that ? thanks...

Integer[] imageIDs = { R.drawable.image_one, R.drawable.image_two,
        R.drawable.image_tree };

private ImageSwitcher imageSwitcher;
private Button nextButton;
private Button previousButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Animation in = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_in_left);
    final Animation out = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);

    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
    imageSwitcher.setFactory(this);
    imageSwitcher.setInAnimation(in);
    imageSwitcher.setOutAnimation(out);
    imageSwitcher.setImageResource(imageIDs[0]);

    nextButton = (Button) findViewById(R.id.next);
    nextButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            imageSwitcher.setImageResource(imageIDs[1]);

        }
    });

    previousButton = (Button) findViewById(R.id.previous);
    previousButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            imageSwitcher.setImageResource(imageIDs[0]);
        }
    });

}

public View makeView() {
    ImageView imageView = new ImageView(this);
    imageView.setBackgroundColor(0xFF000000);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return imageView;
}
Jim
  • 8,874
  • 16
  • 68
  • 125

1 Answers1

2

change the in/out animation in the onClickListeners. try this: (not checked for syntax etc...)

@Override public void onCreate(Bundle savedInstanceState) {    
 super.onCreate(savedInstanceState);    
 setContentView(R.layout.main);    



 imageSwitcher.setImageResource(imageIDs[0]);     
 nextButton = (Button) findViewById(R.id.next);    
 nextButton.setOnClickListener(new OnClickListener() {       
   public void onClick(View v) {  
 Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);     
 Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);   
 imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);    
 imageSwitcher.setFactory(this); 
 imageSwitcher.setInAnimation(in);    
 imageSwitcher.setOutAnimation(out);        
    imageSwitcher.setImageResource(imageIDs[1]);     
     }    
 });    
  previousButton = (Button) findViewById(R.id.previous);     previousButton.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) {  
 Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_left);     
 Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_right);   
 imageSwitcher.setFactory(this); 
 imageSwitcher.setInAnimation(in);    
 imageSwitcher.setOutAnimation(out);  
           imageSwitcher.setImageResource(imageIDs[0]);     
    }   
  }); 
 } 

 public View makeView() {   
  ImageView imageView = new ImageView(this);   
  imageView.setBackgroundColor(0xFF000000);   
  imageView.setScaleType(ImageView.ScaleType.CENTER);  
   imageView.setLayoutParams(new ImageSwitcher.LayoutParams(             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    return imageView; } 
jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • That not works...I need two more animation...slide_out_right...and slide_in_left...But android don't have it ((( – Jim May 10 '11 at 13:53
  • 1
    see http://stackoverflow.com/questions/1274657/android-how-to-get-android-r-anim-slide-in-right for links to out_right and in_left. just create your own xml resources for them. – jkhouw1 May 10 '11 at 14:17