如何以编程方式执行 RotateAnimations 的完整示例? [英] Full example of how to programmatically do RotateAnimations?

查看:19
本文介绍了如何以编程方式执行 RotateAnimations 的完整示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有各种步骤的动画,可以进行汽车的移动(平移)和旋转,例如直行、左转、直行……".

I want to have an animation with various steps that do moves (translations) and rotations like "straight on, turn left, straight on, ...." of a car.

我可以在 AnimationSet 中执行此操作,但是无法使用RELATIVE_TO_SELF"设置围绕我的汽车图像的中心旋转.我知道

I am able to do this in an AnimationSet, but fail at rotating around the center of my car image with the "RELATIVE_TO_SELF" setting. I know about

Animation a = new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5f,... )

为了这个目的.旋转仍然发生在屏幕(或画布?)的左上角.

for this purpose. Still the rotation occurs around the upper left corner of the screen (or canvas?).

目前我正在通过在每个动画步骤之后手动跟踪位置来解决这个问题,但这是次优的.

Currently I am solving this by manually keeping track of the position after each animation step, but this is suboptimal.

我怀疑我的初始布局设置是假的:

I suspect that my initial layout setup is bogus:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
>
    <FrameLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">

        <!-- view that draws some background -->
        <de.bsd.turtlecar.SampleView android:id="@+id/graph_view"
              android:layout_height="350px"
              android:layout_width="fill_parent"
              android:visibility="invisible"
              />

        <!-- the car -->
        <ImageView android:id="@+id/car_view"
                   android:src="@drawable/turtle_car"
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:visibility="invisible"/>


    </FrameLayout>

   <Button ... onClick="run" ... />
</LinearLayout>

这会在左上角显示汽车(应该显示在不同的地方——基本上是动画稍后开始的地方.它应该是稍后移动).

This shows the car in the upper left corner (should show up in a different place -- basically where the animation later starts. And it should be move later).

在我通过运行按钮触发的代码中:

In my code that is triggered through the run button I do:

    ImageView carView = (ImageView) findViewById(R.id.car_view);
    print(carView);
    AnimationSet animationSet = new AnimationSet(true);

    TranslateAnimation a = new TranslateAnimation(
            Animation.ABSOLUTE,200, Animation.ABSOLUTE,200,
            Animation.ABSOLUTE,200, Animation.ABSOLUTE,200);
    a.setDuration(1000);
    animationSet.addAnimation(a);

    RotateAnimation r = new RotateAnimation(0f, -90f,200,200); // HERE 
    r.setStartOffset(1000);
    r.setDuration(1000);
    animationSet.addAnimation(r);
    ...

所以在这一点上,轮换有效,但我必须跟踪.如果我旋转 RELATIVE_TO_SELF,旋转发生在屏幕的 (0,0) 附近.

So at the point with here, the rotation works, but I have to keep track. if I rotate RELATIVE_TO_SELF, the rotation happens around (0,0) of the screen.

附加问题:动画完成后,我可以做什么才能使汽车保持在屏幕上?

Additional question: what can I do in order to keep the car on screen after the animation has finished?

还是我完全走错了路?

推荐答案

尝试将 setFillAfter(true) 添加到您的动画中.这肯定会使汽车保持在最终位置,它也可以解决您的旋转点问题

Try adding setFillAfter(true) to your Animations. That will certainly keep the car in its final place and it may solve your rotation point problems too

 TranslateAnimation a = new TranslateAnimation(
        Animation.ABSOLUTE,200, Animation.ABSOLUTE,200,
        Animation.ABSOLUTE,200, Animation.ABSOLUTE,200);
a.setDuration(1000);
a.setFillAfter(true); //HERE
animationSet.addAnimation(a);

RotateAnimation r = new RotateAnimation(0f, -90f,200,200); // HERE 
r.setStartOffset(1000);
r.setDuration(1000);
r.setFillAfter(true); //HERE
animationSet.addAnimation(r);
...

这篇关于如何以编程方式执行 RotateAnimations 的完整示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆