如何以编程方式做RotateAnimations完整的例子吗? [英] Full example of how to programmatically do 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).
在我的code,它是通过运行按钮,我做的触发:
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?
或完全是我在错误的轨道上吗?
Or am I completely on the wrong track?
推荐答案
尝试添加 setFillAfter(真)以您的动画。这必将让这款车在其最后的地方,它可能会解决您的旋转点的问题太
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屋!