简体   繁体   English

在android中自定义重复翻译动画

[英]custom repeating of translation animation in android

I'm doing animation of spinning with different images (like horizontal roulette) one image at a time from left to right over the screen. 我正在用不同的图像(如水平轮盘)在屏幕上从左到右每次旋转一个动画。

At every animation cycle I need to change image to random from the list, so I found if I use repeating animation then setting new ImageResource on onAnimationRepeat of AnimationListener doesn't update my ImageView. 在每个动画周期中,我都需要从列表中将图像更改为随机图像,因此我发现如果我使用重复动画,那么在AnimationListener的onAnimationRepeat上设置新的ImageResource不会更新ImageView。 I've decided to use custom triggering of next animation from onAnimationEnd. 我决定使用onAnimationEnd的下一个动画的自定义触发。 Here a pice of code: 这里有一堆代码:

    ImageView image = (ImageView)(ImageView)findViewById(R.id.imageId);

    Animation aMid = AnimationUtils.loadAnimation(this, R.anim.plant_middle);  
    aMid.setFillEnabled(true);
    aMid.setFillAfter(true);
    aMid.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) { 
            setRandomImageResource();
        }

        @Override
        public void onAnimationRepeat(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation) {
            if(customCounter < SPIN_COUNT){
                customCounter++;
                image.startAnimation(aMid);
            }
        }
   });

And Animation ixm is pretty simple, it's about ImageView comes from left of the screen to it's right: 而动画ixm非常简单,它是关于ImageView从屏幕左侧到右边:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true">

    <translate
         android:interpolator="@android:anim/linear_interpolator"
         android:fromXDelta="-100%"
         android:toXDelta="100%"
         android:fromYDelta="0%"
         android:toYDelta="0%"
         android:duration="4000" /> 
</set>

The problem is in that between every animation cycle ImageView appears in the center of the screen for a very short moment, like it is flying from right back to left to start next animation cycle, or like it flickers in the middle where it's initially positioned in layout xml. 问题在于 ,在每个动画循环之间,ImageView会在屏幕中央出现很短的时间,就像从右到左飞行开始下一个动画周期一样,或者像它在最初位于中间的中间闪烁布局xml。

Setting it's visibility to GONE on onAnimationEnd and VISIBLE on onAnimationEnd doesn't work our the problem. 在onAnimationEnd上将其可见性设置为GONE,在onAnimationEnd上将其可见性设置为无法解决我们的问题。

What do? 做什么?

I used your code for my application and I'll tell you how I decide this problem. 我将你的代码用于我的应用程序,我会告诉你我是如何决定这个问题的。 First of all when you create an ImageView tag into the XML file you must not set a source for it like that: 首先,当您在XML文件中创建ImageView标记时,您不能像这样设置它的源:

<ImageView android:id="@+id/cloud_image"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_marginTop="20dip"
    android:adjustViewBounds="false" 
    android:scaleType="fitXY" />

After that you must set the ImageView source into onAnimationStart() function and must set empty image (empty and 100% transparent image) source into onAnimationEnd() function: 之后,您必须将ImageView源设置为onAnimationStart()函数,并且必须将空图像(空和100%透明图像)源设置为onAnimationEnd()函数:

// == Animation listener ====================================================
mCloudsAnimation.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation animation) {
            mCloudsImage.setBackgroundResource(R.drawable.clouds);
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            mCloudsImage.setBackgroundResource(R.drawable.clear_sky);
            mCloudsImage.startAnimation(mCloudsAnimation);
        }
});

It works on the emulator. 它适用于模拟器。 Now I'm going to try it on my device. 现在我要在我的设备上试一试。

将“setFillAfter(true)”添加到AnimationSet中(不仅仅是动画)它对我有用。

I managed to achieve needed by setting layout margin left to -1000 and back on onAnimationEnd/onAnimationStart instead of doing visibility. 我设法通过将布局边距设置为-1000并返回onAnimationEnd / onAnimationStart而不是执行可见性来实现所需。 However solution is ugly. 然而解决方案很难看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM