动画canvas.drawCircle

时间:2015-07-21 13:46:01

标签: android canvas

如何在坐标Y中移动canvas.drawCircle

我想让我的感觉就像我在屏幕上触摸的圆圈,但我不知道如何为它制作动画。

我的onDraw

 public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();
        anchoX = x;
        anchoY = y;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawPaint(paint);
        /*Texto*/
        paint.setColor(Color.BLACK);
        paint.setTextSize(80);
        canvas.drawText("CONECTA 4", 70, 130, paint);
        /*Separador*/
        paint.setColor(Color.parseColor("#5C5C5C"));
        canvas.drawRect(0, 200, 600, 210, paint);
        /*Tablero*/
        int radius = 25;
        for (int i = 0; i < Game.NFILAS; i++)
            for (int j = 0; j < Game.NCOLUMNAS; j++){
                if (game.estaVacio(i,j)){
                    color = Color.WHITE;
                    paint.setColor(color);
                    canvas.drawCircle(getPixelFromColumna(j), getPixelFromFila(i), radius, paint);
                } else if (game.estaJugador(i,j)){
                    paint.setColor(coloreado);
                    canvas.drawCircle(getPixelFromColumna(j), getPixelFromFila(i), radius, paint);
                } else {
                    color = Color.RED;
                    paint.setColor(color);
                    canvas.drawCircle(getPixelFromColumna(j),getPixelFromFila(i), radius, paint);
                }
            }
    }

1 个答案:

答案 0 :(得分:1)

你可以查看这个答案。这就是您要找的draw a circle with animation

为了创建圆圈,你可以像这样使用java文件

public class Circle extends View {

private static final int START_ANGLE_POINT = 90;

private final Paint paint;
private final RectF rect;

private float angle;

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

    final int strokeWidth = 40;

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);
    //Circle color
    paint.setColor(Color.RED);

    //size 200x200 example
    rect = new RectF(strokeWidth, strokeWidth, 200 + strokeWidth, 200 + strokeWidth);

    //Initial Angle (optional, it can be zero)
    angle = 120;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
}

public float getAngle() {
    return angle;
}

public void setAngle(float angle) {
    this.angle = angle;
}
}

和创建动画

public class CircleAngleAnimation extends Animation {

private Circle circle;

private float oldAngle;
private float newAngle;

public CircleAngleAnimation(Circle circle, int newAngle) {
    this.oldAngle = circle.getAngle();
    this.newAngle = newAngle;
    this.circle = circle;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
    float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);

    circle.setAngle(angle);
    circle.requestLayout();
}
}

你可以在xml中使用这样来定义

<com.package.Circle
android:id="@+id/circle"
android:layout_width="300dp"
android:layout_height="300dp" />

使用此示例代码

为您设置圆形
Circle circle = (Circle) findViewById(R.id.circle);

CircleAngleAnimation animation = new CircleAngleAnimation(circle, 240);
animation.setDuration(1000);
circle.startAnimation(animation);