android - 为什么objectAnimator不调用setter方法

标签 android objectanimator

我正在学习如何使用 ObjectAnimator 在 android 上为对象设置动画,但我没有看到它更新我的 setter 方法。假设我有一个自定义 View ,它在显示器上绘制一个简单的文本,并且有一个 objectAnimator 将操作的私有(private)变量 (curnum):

public class TempView extends View {

    private Float cur_num = new Float(0.0);
    private float curnum = 0f;

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

    public void setCurnum()
    {
        curnum++;
        cur_num = new Float(curnum);
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        Paint paint = new Paint();
        paint.setStrokeWidth(8);
        paint.setTextSize(100);

        canvas.drawText(cur_num.toString(), 150, 150, paint);


    }
}

现在在我的 Activity 类中,我有一个启动动画的操作栏项目:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.


    int id = item.getItemId();
    if (id == R.id.startanim) {

       TempView v = (TempView)findViewById(R.id.tempView);
       ObjectAnimator anim = ObjectAnimator.ofFloat(v, "curnum", 0f, 1f);
       anim.setDuration(1000L);
       anim.start();
    }

    return super.onOptionsItemSelected(item);
}

但不知何故,如果我在 setter 方法上放置断点,它永远不会命中。

我错过了什么吗?

最佳答案

developers guide 中所述:

The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method.

The getter (if needed) and setter methods of the property that you are animating must operate on the same type as the starting and ending values that you specify to ObjectAnimator.

例如,如果构造以下 ObjectAnimator,则必须具有 targetObject.setPropName(float)targetObject.getPropName(float):

ObjectAnimator.ofFloat(targetObject, "propName", 1f)

因此您需要将方法更改为:

setCurnum(float f)

关于android - 为什么objectAnimator不调用setter方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22736147/

相关文章:

android - RecyclerView 内的 CardView 有额外的边距

android - sdkmanager 打不开 : I get only [=========] 100% Computing updates

java - Android图像淡入淡出动画

android - 没有 xml 的动画 fragment

java - Android属性动画: how to increase view height?

android - 如何使用 Object Animator 制作放大动画

Android 对象动画不能正常工作

android - 无法解析在具有兼容包 v4 的 Android 2.3.3 上运行的 FragmentActivity

android - Sqlite 数据库在 Samsung Android Oreo 8.0 设备中无法正常运行

android - 确定 View 测量高度的准确方法,甚至在它从 GONE 变为 VISIBLE 之前