【发布时间】:2014-05-06 19:29:38
【问题描述】:
我创建了一个扩展 View 的类,我将在布局中引用它以便在屏幕上绘制它。
这个类只是表示一个矩形,我希望矩形的长度一直减小到 0。
构造函数:
public CardAnimationNew(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(getResources().getColor(R.color.card_grey_underline));
}
onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mRectangle == null) {
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mRectangle = new Rect(0, 0, mWidth, mHeight);
animateRectangle();
}
}
动画矩形:
private void animateRectangle() {
mObjectAnimator = ObjectAnimator.ofFloat(mRectangle, "width", 5);
mObjectAnimator.setDuration(1000);
mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
mObjectAnimator.start();
}
onDraw:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(mRectangle, mPaint);
}
问题是Rect 根本没有动画。它保持相同的长度。我哪里错了?
【问题讨论】:
-
更新问题以包含一个缺失的方法 (animateRectangle)。
-
发布你的 logcat,因为 Rect 对象没有 setWidth 方法
标签: android animation view rect