1

J'ai un ImageView avec l'arrière-plan défini sur un cercle drawable. Le cercle a une largeur de trait épaisse. Je souhaite animer cette largeur de trait pour la réduire à 1dp sur une durée spécifiée. Si cela ne peut pas être fait avec un drawable, j'ai aussi un customView ce chemin est un cercle avec un paint.style défini sur Stroke. Je voudrais appliquer cette animation à drawable.circle ou à customView.Comment animer la largeur de trait d'une vue

CustomCirleView:

public class CustomCircle extends View { 

private Path path; 
private Paint paint; 
float customStrokeWidth = 80; 


public float getCustomStrokeWidth() { 
    return customStrokeWidth; 
} 

public void setCustomStrokeWidth(float customStrokeWidth) { 
    this.customStrokeWidth = customStrokeWidth; 
} 


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

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    path = new Path(); 
    paint = new Paint(); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.parseColor("#FC6C2B")); 
    paint.setStrokeWidth(customStrokeWidth); 
    path.addCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, (getMeasuredWidth()/2)-customStrokeWidth, Path.Direction.CW); 

    canvas.drawPath(path, paint); 
} 

}

Merci

+0

animés vecteur dessinables vous permettent d'animer la largeur de course. Si vous avez besoin d'un contrôle plus précis sur l'animation, vous pouvez créer et animer un dessin personnalisable, il est plus découplé qu'une vue personnalisée. – BladeCoder

Répondre

0

je ferais quelque chose comme ça, et puis juste dessiner le cercle dans votre méthode onDraw. Et s'il vous plaît ne jamais faire tout l'initialisation de la peinture à l'intérieur de la méthode onDraw car il faut toujours un peu de temps et la méthode onDraw doit faire le moins possible

private void init() { 
    animator = ObjectAnimator.ofFloat(this, "animationProgress", startVal, endVal); 
    animator.setStartDelay(ANIMATION_START_DELAY); 
    animator.setDuration(ANIMATION_DURATION); 
    animator.setInterpolator(new FastOutSlowInInterpolator()); 

    path = new Path(); 
    paint = new Paint(); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.parseColor("#FC6C2B")); 
    paint.setStrokeWidth(customStrokeWidth); 
    path.addCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, (getMeasuredWidth()/2)-customStrokeWidth, Path.Direction.CW); 
} 

/** 
* Is called by the {@link #animator} after an animation update 
*/ 
protected void setAnimationProgress(float strokeWidth) { 
    this.strokeWidth = strokeWidth; 
    postInvalidateOnAnimation(); 
}