1

我正在学习如何使用 ObjectAnimator 在 android 上为对象设置动画,但我没有看到它更新了我的 setter 方法。假设我有一个自定义视图,它在显示器上绘制一个简单的文本,并且有一个 objectAnimator 将操作的私有变量(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 方法上设置断点,它永远不会被命中。

我错过了什么吗?

4

1 回答 1

5

正如开发人员指南中所说:

您正在制作动画的对象属性必须具有 set() 形式的 setter 函数(在骆驼情况下)。因为 ObjectAnimator 在动画过程中会自动更新属性,所以它必须能够使用这个 setter 方法来访问属性。

您正在制作动画的属性的 getter(如果需要)和 setter 方法必须在与您指定给 ObjectAnimator的起始值和结束值相同的类型上运行。

例如,您必须具有targetObject.setPropName(float)并且targetObject.getPropName(float)如果您构造以下内容ObjectAnimator

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

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

setCurnum(float f)
于 2014-03-29T20:15:20.233 回答