0

我想在两个带有动画的活动之间进行翻译。我希望当用户触摸页面顶部的图像时,图像转换到屏幕底部(向下滑动)并且第二个活动的视图从上到下移动(向下滑动),就像拖曳移动同时运行一样。我不知道我该如何实现这个?我使用这个代码。

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>

面:

 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;
    }

};

当我运行此代码并触摸图像时,屏幕变黑,然后第二个活动开始,然后动画运行。但是我想要第一个活动关闭时的动画,第二个活动在第一个活动结束时开始

4

1 回答 1

1

你走在正确的道路上。

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

必须在您的活动的 onCreate 中定义,并定义该活动在进入和退出时的行为方式。

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%" />

编辑:

您只想要视图的动画,然后切换到另一个活动,对吗?

@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;
}
于 2013-09-29T12:50:21.697 回答