2015-07-21 58 views
0

如何在座標Y,a canvas.drawCircle中移動?Animate 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); 
       } 
      } 
    } 
+0

可以通過更改正確的值來完成動畫製作,然後使其無效。也許你可以看看[ValueAnimator](http://developer.android.com/intl/zh-CN/reference/android/animation/ValueAnimator.html)。這從起始值變爲您的首選值。添加一個OnUpdateListener。在onUpdate: - 設置值 - 無效視圖 –

+0

你能給我一個方法或方法的例子嗎? –

+0

'int yCoordinate = [您的目標座標] ValueAnimator animator = ValueAnimator.ofInt(0,yCoordinate); animator.addUpdateListener(新ValueAnimator.AnimatorUpdateListener(){ @覆蓋 公共無效onAnimationUpdate(ValueAnimator動畫){ view.setYCoordinate(); //設置您的y座標和無效的觀點 } }); animator.setDuration([ANY DURATION]); animator.start();' –

回答

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); 
+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/15045663) –

+0

是的我認爲你是對的@JigneshAnsodariya –