我創建了一個類,它擴展了View
,我將在它的佈局中引用它以在屏幕上繪製。自定義視圖中的Android動畫矩形
該類僅僅代表一個矩形,我願把長方形的長一路下降到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();
}
}
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)。 – Subby
發佈您的logcat,因爲Rect對象沒有setWidth方法 – pskink