gpt4 book ai didi

android - Android中的旋转动画序列

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

我想在 Android 项目中制作简单的动画。我的 Activity 中有一张图片:

<ImageView
android:id="@+id/pointer_png"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:src="@drawable/pointer_400" />

这是我在 Activity 类中的 onClick 方法:

public void onStartButtonClick(View view){
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.setFillAfter(true);

RotateAnimation anim = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(4000);
animationSet.addAnimation(anim);

RotateAnimation anim2 = new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim2.setDuration(4000);
animationSet.addAnimation(anim2);

RotateAnimation anim3 = new RotateAnimation(0.0f, -135.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim3.setDuration(4000);
animationSet.addAnimation(anim3);

RotateAnimation anim4 = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim4.setDuration(4000);
animationSet.addAnimation(anim4);

final ImageView pointer = (ImageView) findViewById(R.id.pointer_png);
pointer.startAnimation(animationSet);
}

不幸的是,效果出乎意料。我想按以下顺序旋转图像:

  1. 在 4 秒内旋转 180 度。
  2. 在接下来的 4 秒内旋转 -135 度。
  3. 在接下来的 4 秒内旋转 90 度。
  4. 在最后 4 秒内旋转 -45 度。

但使用此代码动画绝对短于 16 秒,并且它仅由一个部分组成 - 从 0 点开始 90 度,然后结束。可能 AnimationSet 检查所有动画并计算我序列中的最后一个位置。我尝试设置 AnimationSet(false) 并向每个 RotateAnimation 添加单独的 LinearInterpolator,但它不起作用。

我应该怎么做才能使我的动画更长并且所有旋转都分开(4 步,每步 4 秒)?

最佳答案

根据我的经验,AnimationSet 并不总是按预期工作,并且可能会令人头疼。我会尝试使用 ViewPropertyAnimator .

这是一个关于如何使用它的例子。您可以像这样设置 startDelay :

pointer.animate()
.rotation(...) // <- enter rotation values here
.setStartDelay(4000)
.setInterpolator(new LinearInterpolator())
.setDuration(4000);

或设置 AnimationListener并在onAnimationEnd()开始下一个动画当前一个完成时。

自发地,如果我必须这样做,我会编写自己的方法,像这样(未测试):

private void rotate(View v, float rotation, int startDelay) {
v.animate()
.rotation(rotation) // or rotationBy(rotation) whichever suits you better
.setStartDelay(startDelay)
.setInterpolator(new LinearInterpolator())
.setDuration(4000);
}

然后像这样调用它四次:

rotate(pointer, -45, 0);
rotate(pointer, 90, 4000);
rotate(pointer, -135, 8000);
rotate(pointer, 180, 12000);

关于android - Android中的旋转动画序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954563/

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