安卓:使用ObjectAnimator翻译与景观的维度分数值视图维度、视图、数值、景观

2023-09-12 21:45:27 作者:余.

看来,旧的观点动画(翻译比例等)都不再被接受在 AnimationInflater ,至少ICS的。我在4.0.4读取其code,并明确预计只有XML元素设置 objectAnimator 动画

It appears that the old view animations (translate, scale, and so on) are no longer accepted by the AnimationInflater, at least as of ICS. I read its code in 4.0.4, and it explicitly expects only the XML elements set, objectAnimator, animator.

尽管在http://developer.android.com/guide/topics/resources/animation-resource.html继续为包​​括动画来看,他们似乎是德precated。试图用它们的结果,比如,误差了java.lang.RuntimeException:未知动画名称:翻译

Even though the documentation at http://developer.android.com/guide/topics/resources/animation-resource.html continues to include the view animations, they appear to be deprecated. Trying to use them results in, for instance, the error java.lang.RuntimeException: Unknown animator name: translate.

因此​​,它使用Android的 ObjectAnimator 成为必要。但是,它不接受其自身或其母公司的关联维度的分数值(宽度 translationX ,例如)为旧观点的动画做的形式75%P

As such, it becomes necessary to use Android's ObjectAnimator. However, it does not accept fractional values of the associated dimension of itself or its parent (width for translationX, for example) as the old view animations did in the form "75%p".

构建 ObjectAnimator 在运行时手动,通过编程方式获取该片段的大小,是不可行的,因为 FragmentTransaction 只接受通过残油指定的声明动画。

Constructing the ObjectAnimator manually at runtime, by programmatically fetching the size of the Fragment, isn't feasible because the FragmentTransaction only accepts declarative animations specified by a resid.

我的目标是把屏幕外被填满整个活动(我基本上做两个片段之间的位移转换)的片段。这是现有的 TranslationAnimation 执行( slide_in_right.xml ,它随着其对应 slide_out_left。 XML 由于某种原因在 android.R.anim 不外露,因此,我必须重复他们在我的codeBase的):

My goal is to translate offscreen a Fragment that is filling up an entire Activity (I'm basically doing a shift transition between two fragments). This is the existing TranslationAnimation implementation (slide_in_right.xml, which along with its counterpart slide_out_left.xml is for some reason not exposed in android.R.anim, and I therefore have to duplicate them in my codebase):

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:duration="@android:integer/config_mediumAnimTime"/>
</set>

我的API级别设置为14。

My API level is set to 14.

谢谢!

推荐答案

其实反对漫画家接受的分数值。但也许你不明白的objectAnimator的基本概念或更一般的值动画。值的动画师将动画相关的属性值(比如你想要颜色,在屏幕上)的位置(X,Y,α参数或其他)。要创建这样一个属性(在你的情况xFraction和yFraction),你需要建立自己的getter和与该属性名setter方法​​。比方说,你想翻译的FrameLayout从0%到你的整个屏幕大小的25%。然后,你需要建立一个自定义视图封装了的FrameLayout对象,写你的getter和setter。

Actually object animators accept fractional values. But maybe you didn't understand the underlying concept of an objectAnimator or more generally a value animator. A value animator will animate a value related to a property (such as a color, a position on screen (X,Y), an alpha parameter or whatever you want). To create such a property (in your case xFraction and yFraction) you need to build your own getters and setters associated to this property name. Lets say you want to translate a FrameLayout from 0% to 25% of the size of your whole screen. Then you need to build a custom View that wraps the FrameLayout objects and write your getters and setters.

public class SlidingFrameLayout extends FrameLayout
{
    private static final String TAG = SlidingFrameLayout.class.getName();

    public SlidingFrameLayout(Context context) {
        super(context);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public float getXFraction()
    {
        int width = getWindowManager().getDefaultDisplay().getWidth();
        return (width == 0) ? 0 : getX() / (float) width;
    }

    public void setXFraction(float xFraction) {
        int width = getWindowManager().getDefaultDisplay().getWidth();
        setX((width > 0) ? (xFraction * width) : 0);
    }
}

然后您可以使用XML的方式来声明对象动画将xFraction属性XML属性下,用AnimatorInflater

Then You can use the xml way to declare the object animator putting xFraction under the property xml attribute and inflate it with a AnimatorInflater

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="xFraction" 
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="0.25" 
android:duration="500"/>

或者你可以使用java线code

or you can just use the java line code

ObjectAnimator oa = ObjectAnimator.ofFloat(menuFragmentContainer, "xFraction", 0, 0.25f);

希望它可以帮助你!

Hope it helps you!

奥利维尔,