Android: ImageSwitcher

I have an ImageSwitcher and 2 buttons ... Next and Previous for slide images ... But its animation only slides me from one side from left to right ... How can I fix this? 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; } 
+4
source share
1 answer

change the in / out animation in 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; } 
+2
source

All Articles