Android自定义视图invalidate()动画不会触发ondraw或draw

时间:2014-03-13 21:49:33

标签: android android-layout android-view android-drawable

我创建了一个自定义视图类,用于绘制饼图并为其设置动画。首先,我让类扩展了drawable,然后将drawable应用于视图,它工作正常。我改变了它,所以现在类扩展了相对布局,我可以在xml布局中使用它。方法animateDraw()是一个值动画师,应该使触发ondraw()的视图无效,但是从不触发绘制

public class PieView extends RelativeLayout {

public PieView(Context context) {
    super(context);
}

public PieView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public PieView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

private HashMap<Integer, Integer> mValues = new HashMap<Integer, Integer>();
private Paint paint;
private float totalArcElapsed;
public boolean animating;
private float dataSum = 0;

public void setValues(HashMap<Integer, Integer> values) {


    this.paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);

    for (Map.Entry<Integer, Integer> entry : mValues.entrySet()) {
        Integer key = entry.getKey();

        dataSum += (float) key;
    }
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub

    Log.d(MyApp.TAG, "on draw");
    super.onDraw(canvas);
}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    Log.d(MyApp.TAG, "draw");
    float startAngle = 0;

    RectF rectBounds = new RectF(getLeft(), getTop(), getRight(), getBottom());

    for (Map.Entry<Integer, Integer> entry : mValues.entrySet()) {

        float sliceValue = (float) entry.getKey();
        Integer colorRes = entry.getValue();
        paint.setColor(colorRes);

        float angleSweep = 360 * (sliceValue / dataSum);

        if ((startAngle + angleSweep) < totalArcElapsed) {

            canvas.drawArc(rectBounds, startAngle, angleSweep, true, paint);
        }
        else {

            float portion = angleSweep - ((startAngle + angleSweep) - totalArcElapsed);
            canvas.drawArc(rectBounds, startAngle, portion, true, paint);
            break;
        }

        startAngle += angleSweep;
    }
}

public void animateDraw() {
    ValueAnimator animation = ValueAnimator.ofInt(0, 360);
    animation.setDuration(2000);

    animation.addUpdateListener(new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float fraction = animation.getAnimatedFraction();
            int value = (Integer) animation.getAnimatedValue();
            totalArcElapsed = value;
            Log.d(MyApp.TAG, totalArcElapsed + " ");
                            invalidate();

        }
    });
    animation.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {

            animating = true;
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            animating = false;

        }

        @Override
        public void onAnimationCancel(Animator animation) {


        }
    });
    animation.start();

}

}

1 个答案:

答案 0 :(得分:0)

请参阅以下链接,了解可能适合您的详细答案和可能的解决方法:

Why onDraw is not called after invalidate()?