我正在學習如何使用ObjectAnimator在android上動畫,但是我沒有看到它更新了我的setter方法。假設我有一個在顯示屏上繪製一個簡單的文本,並有私有變量(curnum)的客戶視圖,一個objectAnimator將操作:爲什麼objectAnimator不調用setter方法
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);
}
}
現在我的活動課,我有啓動的動作欄項目動畫:
@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方法上放置斷點,它永遠不會被擊中。
我錯過了什麼嗎?
setCurnum()應該是setCurnum (浮點值)? – pskink
** invalidate()** setter函數內部是絕對必要的,以便動畫運行。 – Odaym