Android动画XML问题 [英] Android animation XML issues
问题描述
我正在尝试使用Android的动画框架以菱形图案移动ImageView。这是我的animation.xml:
I'm trying to use Android's animation framework to have my ImageView move in a diamond pattern. Here's my animation.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true">
<translate
android:fromXDelta="40%p" android:toXDelta="90%p"
android:fromYDelta="10%p" android:toYDelta="40%p"
android:duration="500" android:startOffset="0"/>
<translate
android:fromXDelta="90%p" android:toXDelta="40%p"
android:fromYDelta="40%p" android:toYDelta="90%p"
android:duration="500" android:startOffset="500"/>
<translate
android:fromXDelta="40%p" android:toXDelta="10%p"
android:fromYDelta="90%p" android:toYDelta="40%p"
android:duration="500" android:startOffset="1000"/>
<translate
android:fromXDelta="10%p" android:toXDelta="40%p"
android:fromYDelta="40%p" android:toYDelta="10%p"
android:duration="500" android:startOffset="1500"/>
</set>
我的布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
</FrameLayout>
我的onStart:
protected void onStart() {
super.onStart();
ImageView img = (ImageView)findViewById(R.id.img);
Animation a = AnimationUtils.loadAnimation(this, R.anim.diamond);
img.startAnimation(a);
}
启动应用程序时,我看到的只是空白屏幕2秒钟,然后我的图像弹出到屏幕的左上角。如果我删除其中一个动画之外的所有动画,我将看到图像沿对角线移动。
When I start my application all I see is a blank screen for 2 seconds then my image pops into the upper-left corner of the screen. If I remove all but one of the translate animations I will see the image move in a diagonal line.
我更喜欢使用XML来定义动画,而不是Java。
I would prefer to use XML to define the animation and not Java.
有人对我如何看到整个动画有任何见识吗?
Does anyone have any insight into how I can see the entire animation?
-丹
推荐答案
动画属性是相对于它们的位置的是什么时候开始的。
The animation attributes are relative to where they are when they start. This is probably a lot closer to what you want:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true">
<translate
android:fromXDelta="40%p" android:toXDelta="90%p"
android:fromYDelta="10%p" android:toYDelta="40%p"
android:duration="500" android:startOffset="0"/>
<translate
android:fromXDelta="0%p" android:toXDelta="-50%p"
android:fromYDelta="0%p" android:toYDelta="50%p"
android:duration="500" android:startOffset="500"/>
<translate
android:fromXDelta="0%p" android:toXDelta="-30%p"
android:fromYDelta="0%p" android:toYDelta="-50%p"
android:duration="500" android:startOffset="1000"/>
<translate
android:fromXDelta="0%p" android:toXDelta="30%p"
android:fromYDelta="0%p" android:toYDelta="-30%p"
android:duration="500" android:startOffset="1500"/>
</set>
这篇关于Android动画XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!