Domanda

I am learning how to animate an object on android by using ObjectAnimator but I don't see it update my setter method. Suppose I have a custom view which draw a simple text on the display and there is private variable (curnum) that an objectAnimator will operate:

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


    }
}

Now on my Activity class, I have an action bar item which start an animation:

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

But somehow if I put a breakpoint on the setter method, it is never hit.

Did I miss anything?

È stato utile?

Soluzione

As said in the 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.

For example, you must have targetObject.setPropName(float) and targetObject.getPropName(float) if you construct the following ObjectAnimator:

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

So you need to change your method to:

setCurnum(float f)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top