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