如何Android的ObjectAnimator工作? [英] How does Android ObjectAnimator work?

查看:138
本文介绍了如何Android的ObjectAnimator工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectAnimator是如何能够调用适当的方法 setX的如果属性 X 指定为一个字符串。我的意思是,什么技术是用来识别,我想我的动画视图的属性,旋转,并呼吁该视图的适当的方法setRotation?

how ObjectAnimator is able to call the appropriate method setX if the attribute x is specified as a string. What i mean is, what technique is used to recognize that i want to animate the attribute "rotation" of my view and call the appropriate method setRotation of that view?

我已经了解ObjectAnimator如何运作和管理,使用它,这是很简单的,我只是好奇的经营方针。

I already understood how ObjectAnimator works and managed to use it, it is quite simple, i am just curious about the operating principles.

对不起,我的英语,我从来没有使用它:)

Sorry for my english, i never use it :)

推荐答案

有多种方式,以动画视图的旋转:

There are a number of ways to animate the rotation of a view:

1 ObjectAnimator.ofFloat(看来,旋转,0F,90F)。开始();

这使用<一个href=\"http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful\">reflection调用 setRotation(浮点六)浮动getRotation()视图的方法。

This uses reflection to call the setRotation(float f) and float getRotation() methods of the view.

您可以使用此方法,只要动画类的任何财产作为类实现该属性的适当的getter和setter方法​​。

You can use this method to animate any property of a class as long as that class has implemented the appropriate getter and setter methods for that property.

但是反射是一个缓慢的动作,所以,不使用反射的第二方法。

But reflection is a slow operation, so there is a second method that doesn't use reflection.

2 ObjectAnimator.ofFloat(查看,View.ROTATION,0F,90F)。开始();

这是使用视图的旋转属性。 物业是一个抽象类,定义了的setValue (T) T获得()方法进而调用提供对象的实际getter和setter。例如,在查看类旋转属性使用以下code:

This uses the rotation Property of the view. Property is an abstract class that defines the setValue(T) and the T get() methods which in turn call the actual getter and setter of the supplied object. For example, the rotation property on the View class uses the following code:

public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
    @Override
    public void setValue(View object, float value) {
        object.setRotation(value);
    }

    @Override
    public Float get(View object) {
        return object.getRotation();
    }
};

如果你想动画对象的自定义属性,你可以实现自己的财产像上面的。

If you want to animate a custom property of an object, you can implement your own Property like the one above.

再有第三个方法,它也没有使用反射。

Then there is a third method, which also doesn't use reflection.

3 view.animate()旋转(90F);

这其中有一个流畅的界面,以便更容易使用。你也可以连接多个动画一起运行,例如: view.animate()旋转(90F).translationX(10F);

This one has a fluent interface so it's easier to use. You can also chain multiple animations to run together, for example: view.animate().rotation(90f).translationX(10f);

这种方法的缺点是,你只能一个动画视图上您自己的类标准属性,而不是自定义属性或属性。

The downside of this method is that you can only animate the standard properties of a View and not custom properties or properties on your own classes.

这篇关于如何Android的ObjectAnimator工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆