【问题标题】:runs animation in translation between two activity在两个活动之间运行动画翻译
【发布时间】:2013-09-29 21:12:08
【问题描述】:

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

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

};

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

【问题讨论】:

    标签: android animation android-activity


    【解决方案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;
    }
    

    【讨论】:

    • 但我想当用户触摸图像时,动画和翻译运行为此我将 overridePendingTransition() 方法添加到 onTouchListener。
    • 谢谢。你的回答是真的。另一个问题:如果我想向上滑动图像(从下向上滑动)和第二个从下向上滑动的活动显示,我该如何为此编写此代码?
    • 我没有看到您的编辑答案。你从 XDelta 和 toXDelta 等填充了什么数据???
    • 这是完全相同的过程,除了您必须将值传递给下一个活动。价值的传递可以通过一个简单的 sharedPreference,请参阅我的回答:stackoverflow.com/questions/19048693/…。现在,当第二个活动开始时,它是 onCreate,它会根据这个变量选择正确的 overridePendingTransition 方法。
    • fromXDelta, toXDelta 由你根据你想要的动画来填写。
    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    相关资源
    最近更新 更多