gpt4 book ai didi

android - 改变 Activity 时使用动画

转载 作者:行者123 更新时间:2023-11-29 16:01:52 26 4
gpt4 key购买 nike

我想在两个 Activity 之间转换时使用从右到左的滑动动画。到目前为止,我已经设置了传递给下一个 Activity 的命令。

public void advancenext (View view) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}

但是,我无法将动画合并到这段代码中。这是我目前为止的translation 动画

Animation set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);

LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);

我将如何将动画信息与 Activity 变化相结合?

提前致谢

最佳答案

要设置 Activity 入口动画,您必须创建一个 tween animation (涉及 alpha、scale、translate 等)在 res\anim 文件夹中,然后调用 overridePendingTransition()在调用 startActivity() 之后。

例如,您可以使用这些动画文件获得“Activity 从右侧进入并将前一个 Activity 推出”效果(如果我理解正确的话,这就是您所需要的):

push_left_exit.xml

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

push_left_enter.xml

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

建议在activity结束时设置一个“倒车”动画,这样按下后退按钮时的效果和进入是一致的。

关于android - 改变 Activity 时使用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com