Android ObjectAnimator如何识别属性设置器方法? [英] How does Android ObjectAnimator recognize the attributes setter methods?

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

问题描述

如果将属性x指定为字符串,ObjectAnimator如何能够调用适当的方法setX?我的意思是,用什么技术可以识别我要为视图的属性rotation设置动画并调用该视图的适当方法setRotation?

How is ObjectAnimator 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've already understood how ObjectAnimator works and have managed to use it, it is quite simple, I am just curious about the operating principles.

推荐答案

可以通过多种方法来动画化视图的旋转:

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

1. ObjectAnimator.ofFloat(view, "rotation", 0f, 90f).start();

这使用 reflection 来调用setRotation(float f)和视图的float 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, View.ROTATION, 0f, 90f).start();

这使用视图的旋转属性. 属性是定义setValue(T)方法,这些方法依次调用所提供对象的实际getter和setter.例如,View类上的rotation属性使用以下代码:

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().rotation(90f);

该软件具有流畅的界面,因此更易于使用.您还可以链接多个动画以使其一起运行,例如:view.animate().rotation(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);

此方法的缺点是,您只能为View的标准属性设置动画,而不能为自己的类创建自定义属性或动画.

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天全站免登陆