0

I want to translation between two activity with animation. I want when user touches the image at top of page, the image translate to bottom of screen(slide down) and View of second activity move of top to bottom(slide down) and this like that tow move runs in same time. I dont know How can I implemented this? I use this code .

slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">

<scale
    android:duration="500"
    android:fromXScale="1.0"
    android:fromYScale="0.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:toXScale="1.0"
    android:toYScale="1.0" />

</set>

mian:

 private OnTouchListener onTouchListener=new OnTouchListener(){

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Intent intent=new Intent(MainActivity.this,Test.class);
        //overridePendingTransition(R.anim.slide_down, R.anim.slide_down);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_down, R.anim.slide_down);
        return false;
    }

};

When I run this code and touch image, the screen becomes black and then second activity starts and then the animation runs. But I want the animation when the first activity closes, second activity starts over the end of first activity

nathanchere
  • 8,008
  • 15
  • 65
  • 86
SensorS
  • 383
  • 3
  • 8
  • 20

1 Answers1

1

You are on the right path.

overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);

Must be defined in onCreate of your activity and defines how that activity behaves on enter and exit.

slide_in_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="200"
    android:fromYDelta="-100%"
    android:toYDelta="0%" />

slide_out_bottom.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="200"
    android:fromYDelta="0%"
    android:toYDelta="100%" />

Edit:

You want the animation for the view only, and then switch to another activity, right?

@Override
public boolean onTouch(View v, MotionEvent event) {
   // first animate the view
   TranslateAnimation anim = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
   anim.setDuration(duration);
   v.startAnimation(anim);

   new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // wait for the duration of the animation before switching acitivity
            // remember to apply the overridePendingTransition to them 
            // if you want a transition animation on this too

            // overridePendingTransition added to both onCreate of Test and MainActivity
            Intent intent=new Intent(MainActivity.this,Test.class); 
            startActivity(intent);

        }
    }, duration); // <-- notice the wait for animation to complete
    return false;
}
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • but i want when user touches the image, animation and translation runs for this i add overridePendingTransition() method to onTouchListener. – SensorS Sep 29 '13 at 12:54
  • Thanks. Your answer is true. another question: if i want to up the image(of bottom to up like slide up) and second activity shows of bottom to up like slide up, How can i write this code for this? – SensorS Sep 29 '13 at 13:18
  • I don't see your edit answer. with what data u fill fromXDelta and toXDelta and etc??? – SensorS Sep 29 '13 at 13:19
  • That is exactly the same procedure, except you will have to pass a value to the next activity. The pass of value could be through a simple sharedPreference, see my answer to: http://stackoverflow.com/questions/19048693/android-save-view-pager-state/19048917#comment28201203_19048917. Now when second activity starts it's onCreate it picks the right overridePendingTransition method depending on this variable. – cYrixmorten Sep 29 '13 at 13:22
  • fromXDelta, toXDelta is up to you to fill in depending on what animation you would like. – cYrixmorten Sep 29 '13 at 13:24
  • cYrixmorten, I read your answer in this link, but i can't understand why should i use this for my problem? Thank you if u explain for me :) – SensorS Oct 04 '13 at 07:32
  • I dont think you need to anyway, after thinking about it, nevermind :) – cYrixmorten Oct 04 '13 at 09:56