M
M
MikkiMouse2015-12-09 16:51:51
Android
MikkiMouse, 2015-12-09 16:51:51

ObjectAnimator - is it possible to specify percentage values ​​in xml?

Good afternoon, I ran into a problem when translating animation to objectAnimator, it used to be like this:

<translate
        android:duration="400"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="0%p"
        android:toYDelta="100%p" />

Now it comes out like this:
<objectAnimator
        android:interpolator="@android:anim/accelerate_interpolator"
        android:propertyName="translationY"
        android:valueType="floatType"
        android:valueFrom="0.0"
        android:valueTo="1000"
        android:duration="400"/>

And something I really don’t like to specify specific values, I tried to specify from 0.0 to 1.0, but there was no expected effect. Is there any way to specify values ​​as percentages?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Goryachkin, 2015-12-12
@MikkiMouse

In general, ObjectAnimator eats percentage values, but for this you yourself will have to implement functions for setting and receiving values ​​for animation. For example, you need to change the FrameLayout length from 0 to 25% of the screen size, then there will be something like this:

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);
    }
}

After that, in xml-ke you can write like this:
<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"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question