2

I'm doing an image viewer that pass pictures to next or previous, by clicking right/left button.

I wanna to animate the transition between the pictures. I did it using ImageSwitcher, it is working, but I still have a problem.

The animation loaded is always the same, always sliding out right, making no difference if I click on right or left button.

QUESTION: How can I set which animation will run when I click my buttons?

I did my code based on this blog: http://saigeethamn.blogspot.com/2010/05/image-switcher-view-android-developer.html

Here same important parts of the code:

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

imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);

Now the click event (bm1 and bm2 are bitmaps):

    private OnClickListener mClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.buttonRight) {
            Drawable d =new BitmapDrawable(getResources(),bm1);
            imageSwitcher.setImageDrawable(d);
        } else
        if (v.getId()==R.id.buttonLeft) {
            Drawable d =new BitmapDrawable(getResources(),bm2);
            imageSwitcher.setImageDrawable(d);
        }
    }
};

How the ImageSwitcher know witch animation it will perform? If is In Left or Out Right?

EDITED FROM HERE ----------------------------------------------------

Solution:

private OnClickListener mClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.buttonRight) {
            imageSwitcher.setInAnimation(this, R.anim.slide_in_left); // added
            imageSwitcher.setOutAnimation(this, R.anim.slide_out_left); // added
            Drawable d =new BitmapDrawable(getResources(),bm1);
            imageSwitcher.setImageDrawable(d);
        } else
        if (v.getId()==R.id.buttonLeft) {
            imageSwitcher.setInAnimation(this, R.anim.slide_in_right); // added
            imageSwitcher.setOutAnimation(this, R.anim.slide_out_right); // added
            Drawable d =new BitmapDrawable(getResources(),bm2);
            imageSwitcher.setImageDrawable(d);
        }
    }
};

Based on the answer of Rui Gaspar. Need to create the xmls he shows.

Derzu
  • 7,011
  • 3
  • 57
  • 60

1 Answers1

4

You can implement your own animation

slide_in_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
               android:duration="350"/>
</set>

slide_in_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" 
               android:duration="350"/>
</set>

slide_out_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p" 
               android:duration="350"/>
</set>

slide_out_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p" 
               android:duration="350"/>
</set>

You can use it like this:

//Switch Left
Intent myIntent = new Intent(m_context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);

//Switch Right  
Intent myIntent = new Intent(m_context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
Rui Gaspar
  • 154
  • 8
  • Thanks for the answer. I create these 4 files on the res/anim folder, and put the overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); before and after my imageSwitcher.setImageDrawable(d); But this doesn't change my animation. What more should I set? – Derzu Feb 24 '12 at 16:00
  • I barely noticed the initial question and the code that I put it to animate between activities. To change the animation uses setInAnimation() and setOutAnimation() when you click on each button. – Rui Gaspar Feb 24 '12 at 16:37
  • Now I understood, it's works. I have to set both In and Out animation for each direction I wanna slide. Like that: if (direction == RIGHT) { imageSwitcher.setInAnimation(this, R.anim.slide_in_left); imageSwitcher.setOutAnimation(this, R.anim.slide_out_left); } else if (direction == LEFT) { imageSwitcher.setInAnimation(this, R.anim.slide_in_right); imageSwitcher.setOutAnimation(this, R.anim.slide_out_right); } – Derzu Feb 24 '12 at 19:00