Android Animation positioning Problems
I'm trying to make two translate animations in android and having problems with them:
- I'm trying to display an image entering from one side, and exiting the other, and to do repeatedly for a few minutes. I can see a moving, but every time the animation begins - i see a blink of the image for a millisecond.
This is my layout xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:background="@drawable/bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/anim_image" android:src="@drawable/movimage"
android:layout_width="wrap_content" android:layout_heigh开发者_如何学运维t="wrap_content"
android:visibility="invisible" android:layout_marginTop="30dp"
/>
</FrameLayout>
This is my anim xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="40" android:toDegrees="-40" android:toYScale="0.0"
android:pivotX="40%" android:pivotY="30%" android:duration="4000" />
<set>
<translate
android:fromXDelta="200%p" android:toXDelta="-150%p" android:fromYDelta="20%"
android:toYDelta="10%" android:duration="4000" android:zAdjustment="bottom" />
</set>
</set>
And in my java class i've created a countdowntimer which every onTick calls: image.startAnimation(anim);
I also tried to listen to the animation: anim.setAnimationListener(this);
And add to the onAnimationEnd: image.setVisibility(View.GONE);
But it didn't help me. Does anyone know what can i do to fix it?
--
- I'm trying to show a "falling star" image - i've created a small png file (30px*30px) and im trying to position it in one edge of the screen and let it move to the other and dissapear. But every code i write in the anim XML doesn't doing it! Can anyone help me with writing the animation correctly?
I've wrote: translate android:fromXDelta="100%p" android:toXDelta="-100%p" android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="500"
And it didn't work..
edit: I've found a solution:
I've found what was wrong. I've wrote a code with CountDownTimer for repeating, instead of using the animation's repeat option. I've changed my XML to be:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:startOffset="1000"
>
<rotate
android:repeatCount="infinite" android:repeatMode="restart" android:fillAfter="false"
android:fromDegrees="40" android:toDegrees="-40" android:toYScale="0.0"
android:pivotX="40%" android:pivotY="30%" android:duration="4000" />
<set>
<translate
android:repeatCount="infinite" android:repeatMode="restart" android:fillAfter="false"
android:fromXDelta="100%p" android:toXDelta="-100%p" android:fromYDelta="20%"
android:toYDelta="10%" android:duration="4000" android:zAdjustment="bottom" />
</set>
</set>
and now it works without those blinks in the screen. Also for the falling star, i've changed the XML to this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:repeatCount="infinite" android:repeatMode="restart" android:fillAfter="false"
android:fromXDelta="100%p" android:toXDelta="-100%p" android:fromYDelta="0%p"
android:toYDelta="100%p" android:duration="2500" android:zAdjustment="bottom" />
<set>
<alpha
android:repeatCount="infinite" android:repeatMode="restart" android:fillAfter="false"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2500" />
</set>
</set>
and now it works perfectly!
I think that the imageView comes back to its original position after the animation is completed. You can try having android:fillAfter="true|false"
(try one of it) in the xml of the translate animation. or try setFillAfter(boolean)
on the animation object if you are loading it using the AnimationUtils
.
精彩评论