在画布上画圆圈模拟动画android

时间:2014-12-02 15:07:52

标签: android animation android-canvas

我需要绘制一个空白圆圈,边距为10像素。我遇到的问题是我需要在2秒钟内模拟圆圈的绘制,然后在其上面开始绘制另一个颜色的另一个。我正在使用自定义视图,我试图将我的逻辑实现到onDraw方法中,并且每50毫秒使视图无效。问题是我无法画圆圈...我只绘制了治疗数字。有人知道如何在不使用canvas.drawCircle方法的情况下绘制圆形,因为该方法直接绘制圆形而没有动画。

我当前的代码

 public class CustomAnimationView  extends View{

private Canvas canvas;
private int count = 0;
private Paint paint;
private int mLeft;
private int mRight;
private int mBottom;
private int mTop;


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

public CustomAnimationView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setAttributes(attrs);
}

private void setAttributes(AttributeSet attrs) {
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    this.canvas = canvas;

    if(paint == null){
        paint  = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);
    }

    if(count<150){
            drawFirstQuarter(count);
    }

    count++;
}

public void drawFirstQuarter(int count){
     RectF oval = new RectF(mLeft, mTop, mRight, mBottom);
     canvas.drawArc(oval, 90, 30, true, paint);
}


public void setRect(int top, int bottom, int left, int right){
    mBottom = bottom;
    mTop = top;
    mLeft = left;
    mRight = right;
}

}

现在我只想画一点圆圈。

1 个答案:

答案 0 :(得分:0)

感谢。我已经解决了。 这是一个代码示例

public class CustomAnimationView extends View{

    private Canvas canvas;
    private int mCount = 0;
    private Paint paint1;
    private Paint paint2;
    private RectF oval1;
    private Context context;
    private int mColorCount = 0;

    public CustomAnimationView(Context context) {
        super(context);
        this.context = context;
    }

    public CustomAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        setAttributes(attrs);
    }

    private void setAttributes(AttributeSet attrs) {
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;

        if(paint1 == null){
            paint1  = new Paint();
            paint1.setAntiAlias(true);
            paint1.setStyle(Style.STROKE);
            paint1.setStrokeWidth(10);
        }

        if(paint2 == null){
            paint2  = new Paint();
            paint2.setAntiAlias(true);
            paint2.setStyle(Style.STROKE);
            paint2.setStrokeWidth(10);
        }


        if(mCount % 360 == 0 ){
            mColorCount++;
        }

        if(mColorCount % 2 == 0){
            paint1.setColor(context.getResources().getColor(R.color.white));
            paint2.setColor(context.getResources().getColor(R.color.black));
        }else{
            paint2.setColor(context.getResources().getColor(R.color.white));
            paint1.setColor(context.getResources().getColor(R.color.black));
        }

        if(oval1 == null)
            oval1 = new RectF(5,5,canvas.getWidth()-5, canvas.getHeight()-5);

        drawFirstQuarter(mCount, oval1);

    }

    public void drawFirstQuarter(int count, RectF oval){
         canvas.drawArc(oval, 90, 360, false, paint2);
         canvas.drawArc(oval, 90, count, false, paint1);
         if(mCount == 330)
             mCount = 0;
         else
             mCount += 30;
    }

}