1

我有一個ImageView背景設置爲drawable.circle。圓圈的行程寬度很大。我希望將該筆畫寬度設置爲在指定的持續時間內將其設置爲1dp以縮小。如果這不能用drawable完成,我也有一個customView,該路徑是一個將paint.style設置爲Stroke的圓。我想將該動畫應用於drawable.circle或customView。如何動畫視圖的筆劃寬度

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); 
} 

}

感謝

+0

動畫矢量繪圖資源讓您動畫筆畫寬度。如果需要對動畫進行更精確的控制,您可以創建自定義繪圖併爲其創建動畫,則它將比自定義View更加分離。 – BladeCoder

回答

0

我會做這樣的事,然後就畫在你的onDraw法圓。並請絲毫不onDraw方法裏面的油漆全部初始化,因爲它總是需要一些時間和onDraw方法應該做盡可能少

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(); 
}