【问题标题】:set setInAnimation and setOutAnimation while starting activity在启动活动时设置 setInAnimation 和 setOutAnimation
【发布时间】:2011-02-13 19:32:53
【问题描述】:

我有以下情况

TabActivity>group activiyt>(A->B->C)

这里 A、B 和 C 是活动。我像这样加载

 setContentView(this,getLocalActivityManager().startActivity("zero",intent.addFlags(Intent.FLG_ACTIVITY_CLEAR_TOP)) .getDecorView());

当我将一个活动更改为另一个活动时,我需要设置动画(从左/右滑动)?
目前我在setContentView 之后使用了以下动画到新视图

public static Animation inFromRightAnimation() {

        Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromRight.setDuration(ANIMATIION_DURATION);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }  

但它只对新视图做动画。
我需要像当前左移一样同时从右移新
有什么方法可以在 startActivity 中设置 setInAnimationsetOutAnimation,比如视图翻转器?
谢谢你

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以在活动之间制作过渡动画。下面是一个在启动 (LaunchActivity) 屏幕和游戏主菜单屏幕之间 5 秒后进行自定义动画转换的示例:

                new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
    
                /* Create an intent that will start the main activity. */
                Intent mainIntent = new Intent(LaunchActivity.this,
                MainMenuActivity.class);
                LaunchActivity.this.startActivity(mainIntent);
    
                /* Finish splash activity so user cant go back to it. */
               LaunchActivity.this.finish();
    
               /* Apply our splash exit (fade out) and main
               entry (fade in) animation transitions. */
               overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
                }
        }, 5000);
    

    这里的动画是在 xml 文件中定义的,但是你可以在代码中创建它们,就像你已经在做一样。

    应该保存在 res 中的 anim 文件夹中的翻译 xml 文件可能如下所示:

        <?xml version="1.0" encoding="utf-8"?>
    <set 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:interpolator="@android:anim/accelerate_interpolator">
        <translate 
            android:fromXDelta="100%p"
            android:toXDelta="0"
            android:duration="1000" />
    </set>
    

    它将一个对象从屏幕右侧 100% 宽度移动到左侧...

    【讨论】:

    • 非常感谢。 overridePendingTransition 是我正在寻找的。但是现在默认动画(左->右)产生了问题。如何防止这种情况
    • overridePendingTransition 可以从 API 级别 5 开始访问。我想知道是否有可能在 API 级别 4 上进行转换?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多