Search code examples
androidandroid-animationviewflipper

Android - inAnimation works for ViewFlipper but outAnimation doesn't


I've been trying to implement ViewFlipper and its translate animation. There are many pages I can refer to, but no matter how much I tried, I can't see outAnimation ever happen. Below is my project, and currently no translate animation is seen but IF I COMMENT OUT the vf.setOutAnimation... part, inAnimation animation suddenly starts to work. ...why?

package com.example.testview;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class TestViewActivity extends Activity implements OnTouchListener {

    private ViewFlipper vf;
    private float firstTouchX;
    private float leaveTouchX;

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    vf = (ViewFlipper) findViewById(R.id.viewFlipper1);
    vf.setOnTouchListener(this);
}

    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                    firstTouchX = event.getX();
                    break;

            case MotionEvent.ACTION_UP:
                    leaveTouchX = event.getX();

                if (this.firstTouchX - 50f > leaveTouchX) {

                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
                    vf.showNext();
                }
                if (this.firstTouchX + 50f < leaveTouchX) {

                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
                    vf.showPrevious();
                }
                break;
        }
        return true;
    }
}

res/anim/left_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350" 
    android:fromXDelta="-100%p" 
    android:toXDelta="0%p"  
    android:fromYDelta="0%p"
    android:toYDelta="0%p">
</translate>

res/anim/left_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"
    android:fromYDelta="0%p"
    android:toYDelta="0%p">
</translate>

res/anim/right_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350"
    android:fromXDelta="100%p"
    android:toXDelta="0%p"
    android:fromYDelta="0%p"
    android:toYDelta="0%p">
</translate>

res/anim/right_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"
    android:fromYDelta="0%p"
    android:toYDelta="0%p">
</translate>

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
    <ViewFlipper
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewFlipper1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

         <include
            android:id="@+id/first"
            layout="@layout/first" />

        <include
            android:id="@+id/second"
            layout="@layout/second" />

        <include
            android:id="@+id/third"
            layout="@layout/third" />

    </ViewFlipper>

first.xml / second.xml / third.xml (only their own picture id is different)

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/img">
</ImageView>

BIG EDIT: I just build a smaller project based on my post and it seems to work... perhaps through the process of omitting some codes that I thought unnecessary to start a discussion caused my problem.... I'm really sorry if anybody made an attempt to answer. I'll do a more survey and hope I can post breakdowns that can be of use for later references, unless mods delete this question.

BIG EDIT2: Today I tried once again, and it DIDN'T WORK, presumably because I used Android ARM 2.2 Emulator. Once I switch into ARM/Inter 2.3.3, it works.


Solution

  • Try this, replace outAnimation into inAnimation like,

                if (this.firstTouchX - 50f > leaveTouchX) {
    
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
                    vf.showNext();
                }
                if (this.firstTouchX + 50f < leaveTouchX) {
    
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
                    vf.showPrevious();
                }
                break;
    

    And then try It might be work!