Android Is it possible to use concurrent interpolators?

こ雲淡風輕ζ 提交于 2019-12-07 06:49:30

问题


I have a set of two animations, both animations run together using the overshoot interpolator

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/overshoot_interpolator" >

    <translate
        android:duration="6000"
        android:fromXDelta="100%" android:toXDelta="0%" />

    <scale
        android:duration="6000"
        android:fromXScale="1.0" android:toXScale="0.6"
        android:pivotX="0"
        android:fromYScale="1.0" android:toYScale="1.0"
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

I want the translate animation to overshoot, and the scale animation to accelerate.
I tried to do this, but it does not work:

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

    <translate
        android:interpolator="@android:anim/overshoot_interpolator"
        android:duration="6000"
        android:fromXDelta="100%" android:toXDelta="0%" />

    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="6000"
        android:fromXScale="1.0" android:toXScale="0.6"
        android:pivotX="0"
        android:fromYScale="1.0" android:toYScale="1.0"
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

It seems as if only one interpolator can be active at a given time for all animations being performed on a single object.


回答1:


This is only guess work. I remember that one of AnimationSet's constructors can take an argument, namely shareInterpolator.

Judging by the name of the parameter, this probably should be set to false in you case. Right now, it should be using the default "value". This default value is most probably true because your animations are not having different interpolators although you specified a different one for each.

To confirm, the source code of AnimationSet has the following line to assign a value for shareInterpolator (from xml):

setFlag(PROPERTY_SHARE_INTERPOLATOR_MASK, 
        a.getBoolean(com.android.internal.R.styleable.AnimationSet_shareInterpolator
             , true));

This clearly means that if this boolean is not specified, it will default to true.


Solution

To fix your problem, I suppose you should specify it using this "R.styleable.AnimationSet_shareInterpolator". This merely means adding android:shareInterpolator="false" to your <set> elements




回答2:


I put split the animations and put one on an ImageView and the other on a RelativeLayout that was containing the image view.

Translator

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
            android:interpolator="@android:anim/overshoot_interpolator"
            android:duration="6000"
            android:fromXDelta="100%" android:toXDelta="0%" />

</set>

Scalor

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

    <scale
            android:interpolator="@android:anim/accelerate_interpolator"
            android:duration="6000"
            android:fromXScale="1.0" android:toXScale="0.6"
            android:pivotX="0"
            android:fromYScale="1.0" android:toYScale="1.0"
            android:repeatCount="1"
            android:repeatMode="reverse" />
</set>

xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/previousItem"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:onClick="goToPreviousItem"
                    android:layout_margin="@dimen/float_from_edges"
                    android:layout_width="wrap_content"
            >
        <ImageView android:id="@+id/previousItemArrow"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@drawable/arrow_left"
                />
    </RelativeLayout>

And the source code to instigate it:

RelativeLayout previousItemButton = (RelativeLayout)findViewById(R.id.previousItem);
    ImageView myButton = (ImageView)findViewById(R.id.previousItemArrow);
    Animation translator =
            AnimationUtils.loadAnimation(this, R.anim.translator);
    myButton.startAnimation(translator);

    Animation scalor =
            AnimationUtils.loadAnimation(this, R.anim.scalor);
    previousItemButton.startAnimation(scalor);

I thought it looked pretty good. I'm not sure what effect you were hoping for.




回答3:


This xml works with both interpolators check this


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

    <translate
        android:interpolator="@android:anim/overshoot_interpolator"
        android:fromXDelta="100%" android:toXDelta="0%"
        android:duration="500" />

    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="1.0" android:toXScale="0.6"
        android:pivotX="0"
        android:fromYScale="1.0" android:toYScale="1.0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:startOffset="500"
        android:duration="1000" />
</set>


来源:https://stackoverflow.com/questions/15311290/android-is-it-possible-to-use-concurrent-interpolators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!